Skip to content

feat: Add datafile accessor #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

header update? 2020

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use private set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abstract properties can not have private accessors.


/// <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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't used. Any reason to keep it?

{
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();

}
}