Skip to content

Commit

Permalink
feat: Add datafile accessor (#240)
Browse files Browse the repository at this point in the history
* Added datafile accessor in optimizelyConfig and projectConfig
Co-authored-by: Sohail Hussain <mirza.sohailhussain@gmail.com>
  • Loading branch information
mnoman09 authored Aug 26, 2020
1 parent 7406d8f commit 2df2bfb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
8 changes: 8 additions & 0 deletions OptimizelySDK.Tests/OptimizelyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,14 @@ public void TestGetOptimizelyConfigNullConfig()
Assert.IsNull(optimizelyConfig);
}

// Test that OptimizelyConfig.Datafile returns the expected datafile, which was used to generate project config
[Test]
public void TestGetOptimizelyConfigDatafile()
{
var optimizelyConfig = Optimizely.GetOptimizelyConfig();
Assert.AreEqual(optimizelyConfig.GetDatafile(), TestData.Datafile);
}

#endregion


Expand Down
11 changes: 10 additions & 1 deletion OptimizelySDK.Tests/ProjectConfigTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019, Optimizely
* Copyright 2017-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -645,6 +645,15 @@ public void TempProjectConfigTest()
Assert.AreEqual("1592310167", config.AccountId);
}

// Test that getDatafile returns the expected datafile.
[Test]
public void TestProjectConfigDatafileIsSame()
{
ProjectConfig config = DatafileProjectConfig.Create(TestData.Datafile, new Mock<ILogger>().Object, new DefaultErrorHandler());
Assert.AreEqual(config.ToDatafile(), TestData.Datafile);
}


// test set/get forced variation for the following cases:
// - valid and invalid user ID
// - valid and invalid experiment key
Expand Down
19 changes: 18 additions & 1 deletion OptimizelySDK/Config/DatafileProjectConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019, Optimizely
* Copyright 2019-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,8 @@ namespace OptimizelySDK.Config
/// </summary>
public class DatafileProjectConfig : ProjectConfig
{
private string _datafile;

/// <summary>
/// Datafile versions.
/// </summary>
Expand Down Expand Up @@ -77,6 +79,11 @@ public enum OPTLYSDKVersion
/// </summary>
public bool? BotFiltering { get; set; }

/// <summary>
/// Raw datafile
/// </summary>
public string Datafile { get; set; }

/// <summary>
/// Supported datafile versions list.
/// </summary>
Expand Down Expand Up @@ -337,6 +344,7 @@ private static DatafileProjectConfig GetConfig(string configData)
throw new ConfigParseException("Unable to parse empty datafile.");

var config = JsonConvert.DeserializeObject<DatafileProjectConfig>(configData);
config._datafile = configData;

if (SupportedVersions.TrueForAll((supportedVersion) => !(((int)supportedVersion).ToString() == config.Version)))
throw new ConfigParseException($@"This version of the C# SDK does not support the given datafile version: {config.Version}");
Expand Down Expand Up @@ -558,5 +566,14 @@ public bool IsFeatureExperiment(string experimentId)
{
return ExperimentFeatureMap.ContainsKey(experimentId);
}

/// <summary>
///Returns the datafile corresponding to ProjectConfig
/// </summary>
/// <returns>the datafile string corresponding to ProjectConfig</returns>
public string ToDatafile()
{
return _datafile;
}
}
}
16 changes: 14 additions & 2 deletions OptimizelySDK/OptlyConfig/OptimizelyConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019, Optimizely
* Copyright 2019-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,11 +24,23 @@ public class OptimizelyConfig
public IDictionary<string, OptimizelyExperiment> ExperimentsMap { get; private set; }
public IDictionary<string, OptimizelyFeature> FeaturesMap { get; private set; }

public OptimizelyConfig(string revision, IDictionary<string, OptimizelyExperiment> experimentsMap, IDictionary<string, OptimizelyFeature> featuresMap)
private string _datafile;

public OptimizelyConfig(string revision, IDictionary<string, OptimizelyExperiment> experimentsMap, IDictionary<string, OptimizelyFeature> featuresMap, string datafile = null)
{
Revision = revision;
ExperimentsMap = experimentsMap;
FeaturesMap = featuresMap;
_datafile = datafile;
}

/// <summary>
/// Get the datafile associated with OptimizelyConfig.
/// </summary>
/// <returns>the datafile string associated with OptimizelyConfig.</returns>
public string GetDatafile()
{
return _datafile;
}
}
}
5 changes: 3 additions & 2 deletions OptimizelySDK/OptlyConfig/OptimizelyConfigService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019, Optimizely
* Copyright 2019-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,7 +35,8 @@ public OptimizelyConfigService(ProjectConfig projectConfig)
var featureMap = GetFeaturesMap(projectConfig, experimentMap);
OptimizelyConfig = new OptimizelyConfig(projectConfig.Revision,
experimentMap,
featureMap);
featureMap,
projectConfig.ToDatafile());
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion OptimizelySDK/ProjectConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019, Optimizely
* Copyright 2019-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,6 +54,7 @@ public interface ProjectConfig
/// </summary>
bool? BotFiltering { get; set; }


//========================= Mappings ===========================

/// <summary>
Expand Down Expand Up @@ -245,5 +246,11 @@ public interface ProjectConfig
/// <param name="experimentId">Experiment Id</param>
/// <returns>List| Feature flag ids list, null otherwise</returns>
List<string> GetExperimentFeatureList(string experimentId);

/// <summary>
/// Returns the datafile corresponding to ProjectConfig
/// </summary>
string ToDatafile();

}
}

0 comments on commit 2df2bfb

Please sign in to comment.