Skip to content

Commit 2df2bfb

Browse files
feat: Add datafile accessor (#240)
* Added datafile accessor in optimizelyConfig and projectConfig Co-authored-by: Sohail Hussain <mirza.sohailhussain@gmail.com>
1 parent 7406d8f commit 2df2bfb

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-7
lines changed

OptimizelySDK.Tests/OptimizelyTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,6 +3884,14 @@ public void TestGetOptimizelyConfigNullConfig()
38843884
Assert.IsNull(optimizelyConfig);
38853885
}
38863886

3887+
// Test that OptimizelyConfig.Datafile returns the expected datafile, which was used to generate project config
3888+
[Test]
3889+
public void TestGetOptimizelyConfigDatafile()
3890+
{
3891+
var optimizelyConfig = Optimizely.GetOptimizelyConfig();
3892+
Assert.AreEqual(optimizelyConfig.GetDatafile(), TestData.Datafile);
3893+
}
3894+
38873895
#endregion
38883896

38893897

OptimizelySDK.Tests/ProjectConfigTest.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019, Optimizely
2+
* Copyright 2017-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -645,6 +645,15 @@ public void TempProjectConfigTest()
645645
Assert.AreEqual("1592310167", config.AccountId);
646646
}
647647

648+
// Test that getDatafile returns the expected datafile.
649+
[Test]
650+
public void TestProjectConfigDatafileIsSame()
651+
{
652+
ProjectConfig config = DatafileProjectConfig.Create(TestData.Datafile, new Mock<ILogger>().Object, new DefaultErrorHandler());
653+
Assert.AreEqual(config.ToDatafile(), TestData.Datafile);
654+
}
655+
656+
648657
// test set/get forced variation for the following cases:
649658
// - valid and invalid user ID
650659
// - valid and invalid experiment key

OptimizelySDK/Config/DatafileProjectConfig.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@ namespace OptimizelySDK.Config
2929
/// </summary>
3030
public class DatafileProjectConfig : ProjectConfig
3131
{
32+
private string _datafile;
33+
3234
/// <summary>
3335
/// Datafile versions.
3436
/// </summary>
@@ -77,6 +79,11 @@ public enum OPTLYSDKVersion
7779
/// </summary>
7880
public bool? BotFiltering { get; set; }
7981

82+
/// <summary>
83+
/// Raw datafile
84+
/// </summary>
85+
public string Datafile { get; set; }
86+
8087
/// <summary>
8188
/// Supported datafile versions list.
8289
/// </summary>
@@ -337,6 +344,7 @@ private static DatafileProjectConfig GetConfig(string configData)
337344
throw new ConfigParseException("Unable to parse empty datafile.");
338345

339346
var config = JsonConvert.DeserializeObject<DatafileProjectConfig>(configData);
347+
config._datafile = configData;
340348

341349
if (SupportedVersions.TrueForAll((supportedVersion) => !(((int)supportedVersion).ToString() == config.Version)))
342350
throw new ConfigParseException($@"This version of the C# SDK does not support the given datafile version: {config.Version}");
@@ -558,5 +566,14 @@ public bool IsFeatureExperiment(string experimentId)
558566
{
559567
return ExperimentFeatureMap.ContainsKey(experimentId);
560568
}
569+
570+
/// <summary>
571+
///Returns the datafile corresponding to ProjectConfig
572+
/// </summary>
573+
/// <returns>the datafile string corresponding to ProjectConfig</returns>
574+
public string ToDatafile()
575+
{
576+
return _datafile;
577+
}
561578
}
562579
}

OptimizelySDK/OptlyConfig/OptimizelyConfig.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,11 +24,23 @@ public class OptimizelyConfig
2424
public IDictionary<string, OptimizelyExperiment> ExperimentsMap { get; private set; }
2525
public IDictionary<string, OptimizelyFeature> FeaturesMap { get; private set; }
2626

27-
public OptimizelyConfig(string revision, IDictionary<string, OptimizelyExperiment> experimentsMap, IDictionary<string, OptimizelyFeature> featuresMap)
27+
private string _datafile;
28+
29+
public OptimizelyConfig(string revision, IDictionary<string, OptimizelyExperiment> experimentsMap, IDictionary<string, OptimizelyFeature> featuresMap, string datafile = null)
2830
{
2931
Revision = revision;
3032
ExperimentsMap = experimentsMap;
3133
FeaturesMap = featuresMap;
34+
_datafile = datafile;
35+
}
36+
37+
/// <summary>
38+
/// Get the datafile associated with OptimizelyConfig.
39+
/// </summary>
40+
/// <returns>the datafile string associated with OptimizelyConfig.</returns>
41+
public string GetDatafile()
42+
{
43+
return _datafile;
3244
}
3345
}
3446
}

OptimizelySDK/OptlyConfig/OptimizelyConfigService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,8 @@ public OptimizelyConfigService(ProjectConfig projectConfig)
3535
var featureMap = GetFeaturesMap(projectConfig, experimentMap);
3636
OptimizelyConfig = new OptimizelyConfig(projectConfig.Revision,
3737
experimentMap,
38-
featureMap);
38+
featureMap,
39+
projectConfig.ToDatafile());
3940
}
4041

4142
/// <summary>

OptimizelySDK/ProjectConfig.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@ public interface ProjectConfig
5454
/// </summary>
5555
bool? BotFiltering { get; set; }
5656

57+
5758
//========================= Mappings ===========================
5859

5960
/// <summary>
@@ -245,5 +246,11 @@ public interface ProjectConfig
245246
/// <param name="experimentId">Experiment Id</param>
246247
/// <returns>List| Feature flag ids list, null otherwise</returns>
247248
List<string> GetExperimentFeatureList(string experimentId);
249+
250+
/// <summary>
251+
/// Returns the datafile corresponding to ProjectConfig
252+
/// </summary>
253+
string ToDatafile();
254+
248255
}
249256
}

0 commit comments

Comments
 (0)