Skip to content

feat: Added a new getFeatureVariableJson Api #467

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 9 commits into from
Apr 30, 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
64 changes: 64 additions & 0 deletions packages/optimizely-sdk/lib/core/decision_service/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,10 @@ describe('lib/core/decision_service', function() {
id: '6199684360044544',
value: '20.25',
},
{
id: '1547854156498475',
value: '{ "num_buttons": 1, "text": "first variation"}',
},
],
featureEnabled: true,
key: 'variation',
Expand All @@ -1170,6 +1174,10 @@ describe('lib/core/decision_service', function() {
id: '6199684360044544',
value: '50.55',
},
{
id: '1547854156498475',
value: '{ "num_buttons": 2, "text": "second variation"}',
},
],
featureEnabled: true,
key: 'control',
Expand All @@ -1193,6 +1201,10 @@ describe('lib/core/decision_service', function() {
id: '6199684360044544',
value: '99.99',
},
{
id: '1547854156498475',
value: '{ "num_buttons": 3, "text": "third variation"}',
},
],
featureEnabled: false,
key: 'variation2',
Expand Down Expand Up @@ -1224,6 +1236,10 @@ describe('lib/core/decision_service', function() {
id: '6199684360044544',
value: '50.55',
},
{
id: '1547854156498475',
value: '{ "num_buttons": 2, "text": "second variation"}',
},
],
featureEnabled: true,
key: 'control',
Expand All @@ -1247,6 +1263,10 @@ describe('lib/core/decision_service', function() {
id: '6199684360044544',
value: '20.25',
},
{
id: '1547854156498475',
value: '{ "num_buttons": 1, "text": "first variation"}',
},
],
featureEnabled: true,
key: 'variation',
Expand All @@ -1270,6 +1290,10 @@ describe('lib/core/decision_service', function() {
id: '6199684360044544',
value: '99.99',
},
{
id: '1547854156498475',
value: '{ "num_buttons": 3, "text": "third variation"}',
},
],
featureEnabled: false,
key: 'variation2',
Expand All @@ -1295,6 +1319,10 @@ describe('lib/core/decision_service', function() {
id: '6199684360044544',
value: '20.25',
},
{
id: '1547854156498475',
value: '{ "num_buttons": 1, "text": "first variation"}',
},
],
featureEnabled: true,
key: 'variation',
Expand Down Expand Up @@ -1504,6 +1532,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello audience',
},
{
id: "8765345281230956",
value: '{ "count": 2, "message": "Hello audience" }',
},
],
featureEnabled: true,
key: '594032',
Expand All @@ -1529,6 +1561,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello audience',
},
{
id: "8765345281230956",
value: '{ "count": 2, "message": "Hello audience" }',
},
],
featureEnabled: true,
key: '594032',
Expand Down Expand Up @@ -1557,6 +1593,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello audience',
},
{
id: "8765345281230956",
value: '{ "count": 2, "message": "Hello audience" }',
},
],
featureEnabled: true,
key: '594032',
Expand Down Expand Up @@ -1616,6 +1656,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello',
},
{
id: '8765345281230956',
value: '{ "count": 1, "message": "Hello" }',
},
],
featureEnabled: false,
key: '594038',
Expand Down Expand Up @@ -1644,6 +1688,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello',
},
{
id: '8765345281230956',
value: '{ "count": 1, "message": "Hello" }',
},
],
featureEnabled: false,
key: '594038',
Expand All @@ -1669,6 +1717,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello',
},
{
id: '8765345281230956',
value: '{ "count": 1, "message": "Hello" }',
},
],
featureEnabled: false,
key: '594038',
Expand Down Expand Up @@ -1761,6 +1813,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello',
},
{
id: '8765345281230956',
value: '{ "count": 1, "message": "Hello" }',
},
],
featureEnabled: false,
key: '594038',
Expand Down Expand Up @@ -1789,6 +1845,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello',
},
{
id: '8765345281230956',
value: '{ "count": 1, "message": "Hello" }',
},
],
featureEnabled: false,
key: '594038',
Expand All @@ -1814,6 +1874,10 @@ describe('lib/core/decision_service', function() {
id: '6327227708866560',
value: 'Hello',
},
{
id: '8765345281230956',
value: '{ "count": 1, "message": "Hello" }',
},
],
featureEnabled: false,
key: '594038',
Expand Down
25 changes: 19 additions & 6 deletions packages/optimizely-sdk/lib/core/optimizely_config/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ describe('lib/core/optimizely_config', function() {
});
var variablesMap = featuresMap[featureFlag.key].variablesMap;
featureFlag.variables.forEach(function(variable) {
// json is represented as sub type of string to support backwards compatibility in datafile.
// project config treats it as a first-class type.
var expectedVariableType = (variable.type === "string" && variable.subType === "json") ? "json" : variable.type;
assert.include(variablesMap[variable.key], {
id: variable.id,
key: variable.key,
type: variable.type,
type: expectedVariableType,
value: variable.defaultValue,
});
});
Expand All @@ -107,11 +110,21 @@ describe('lib/core/optimizely_config', function() {
variations.forEach(function(variation) {
featureFlag.variables.forEach(function(variable) {
var variableToAssert = variationsMap[variation.key].variablesMap[variable.key];
assert.include(variable, {
id: variableToAssert.id,
key: variableToAssert.key,
type: variableToAssert.type,
});
// json is represented as sub type of string to support backwards compatibility in datafile.
// project config treats it as a first-class type.
var expectedVariableType = (variable.type === "string" && variable.subType === "json") ? "json" : variable.type;
assert.include(
{
id: variable.id,
key: variable.key,
type: expectedVariableType,
},
{
id: variableToAssert.id,
key: variableToAssert.key,
type: variableToAssert.type,
}
);
if (!variation.featureEnabled) {
assert.equal(variable.defaultValue, variableToAssert.value);
}
Expand Down
21 changes: 21 additions & 0 deletions packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ export var createProjectConfig = function(datafile) {

projectConfig.featureKeyMap = fns.keyBy(projectConfig.featureFlags || [], 'key');
objectValues(projectConfig.featureKeyMap || {}).forEach(function(feature) {
// Json type is represented in datafile as a subtype of string for the sake of backwards compatibility.
// Converting it to a first-class json type while creating Project Config
feature.variables.forEach(function(variable) {
if (variable.type === FEATURE_VARIABLE_TYPES.STRING && variable.subType === FEATURE_VARIABLE_TYPES.JSON) {
variable.type = FEATURE_VARIABLE_TYPES.JSON;
delete variable.subType;
}
});

feature.variableKeyMap = fns.keyBy(feature.variables, 'key');
(feature.experimentIds || []).forEach(function(experimentId) {
// Add this experiment in experiment-feature map.
Expand Down Expand Up @@ -479,6 +488,18 @@ export var getTypeCastValue = function(variableValue, variableType, logger) {
}
break;

case FEATURE_VARIABLE_TYPES.JSON:
try {
castValue = JSON.parse(variableValue);
} catch (e) {
logger.log(
LOG_LEVEL.ERROR,
sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType)
);
castValue = null;
}
break;

default:
// type is STRING
castValue = variableValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ describe('lib/core/project_config', function() {
{ value: '395', id: '5482802778734592' },
{ value: '4.99', id: '6045752732155904' },
{ value: 'Hello audience', id: '6327227708866560' },
{ value: '{ "count": 2, "message": "Hello audience" }', id: '8765345281230956' },
],
featureEnabled: true,
key: '594032',
Expand All @@ -221,6 +222,7 @@ describe('lib/core/project_config', function() {
{ value: '400', id: '5482802778734592' },
{ value: '14.99', id: '6045752732155904' },
{ value: 'Hello', id: '6327227708866560' },
{ value: '{ "count": 1, "message": "Hello" }', id: '8765345281230956' },
],
featureEnabled: false,
key: '594038',
Expand Down
24 changes: 24 additions & 0 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,30 @@ Optimizely.prototype.getFeatureVariableString = function(featureKey, variableKey
}
};

/**
* Returns value for the given json variable attached to the given feature
* flag.
* @param {string} featureKey Key of the feature whose variable's value is
* being accessed
* @param {string} variableKey Key of the variable whose value is being
* accessed
* @param {string} userId ID for the user
* @param {Object} attributes Optional user attributes
* @return {object|null} Object value of the variable, or null if the
* feature key is invalid, the variable key is
* invalid, or there is a mismatch with the type
* of the variable
*/
Optimizely.prototype.getFeatureVariableJson = function(featureKey, variableKey, userId, attributes) {
try {
return this._getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.JSON, userId, attributes);
} catch (e) {
this.logger.log(LOG_LEVEL.ERROR, e.message);
this.errorHandler.handleError(e);
return null;
}
};

/**
* Returns OptimizelyConfig object containing experiments and features data
* @return {Object}
Expand Down
Loading