Skip to content
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

Implement getFeatureVariable and create tests #298

Merged
merged 13 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 46 additions & 19 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,29 +647,56 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
}
};

/**
* Returns dynamically-typed value of the variable attached to the given
* feature flag. Returns null if the feature key or variable key is invalid.
*
* @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 {string|boolean|number|null} Value of the variable cast to the appropriate
* type, or null if the feature key is invalid or
* the variable key is invalid
*/

Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, userId, attributes) {
brandonhudavid marked this conversation as resolved.
Show resolved Hide resolved
try {
return this._getFeatureVariableForType(featureKey, variableKey, null, userId, attributes);
} catch (e) {
this.logger.log(LOG_LEVEL.ERROR, e.message);
this.errorHandler.handleError(e);
return null;
}
};

/**
* Helper method to get the value for a variable of a certain type attached to a
* feature flag. Returns null if the feature key is invalid, the variable key is
* invalid, the given variable type does not match the variable's actual type,
* or the variable value cannot be cast to the required type.
* or the variable value cannot be cast to the required type. If the given variable
* type is null, the value of the variable cast to the appropriate type is returned.
*
* @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} variableType Type of the variable whose value is being
* accessed (must be one of FEATURE_VARIABLE_TYPES
* in lib/utils/enums/index.js)
* @param {string} userId ID for the user
* @param {Object} attributes Optional user attributes
* @return {*} Value of the variable cast to the appropriate
* type, or null if the feature key is invalid, the
* variable key is invalid, or there is a mismatch
* with the type of the variable
* @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|null} variableType Type of the variable whose value is being
* accessed (must be one of FEATURE_VARIABLE_TYPES
* in lib/utils/enums/index.js), or null to return the
* value of the variable cast to the appropriate type
* @param {string} userId ID for the user
* @param {Object} attributes Optional user attributes
* @return {string|boolean|number|null} Value of the variable cast to the appropriate
* type, 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._getFeatureVariableForType = function(featureKey, variableKey, variableType, userId, attributes) {
brandonhudavid marked this conversation as resolved.
Show resolved Hide resolved
if (!this.__isValidInstance()) {
var apiName = 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1);
var apiName = (variableType) ? 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1) : 'getFeatureVariable';
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
return null;
}
Expand All @@ -692,8 +719,8 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
if (!variable) {
return null;
}

if (variable.type !== variableType) {
if (variableType && variable.type !== variableType) {
this.logger.log(
LOG_LEVEL.WARNING,
sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)
Expand Down Expand Up @@ -732,7 +759,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
}
}

var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variableType, this.logger);
var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variable.type, this.logger);
brandonhudavid marked this conversation as resolved.
Show resolved Hide resolved
this.notificationCenter.sendNotifications(
NOTIFICATION_TYPES.DECISION,
{
Expand All @@ -745,7 +772,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
source: decision.decisionSource,
variableKey: variableKey,
variableValue: typeCastedValue,
variableType: variableType,
variableType: variable.type,
sourceInfo: sourceInfo,
}
}
Expand Down
Loading