Skip to content

feat: Use enum to define type in getTypeCastValue #695

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 2 commits into from
Jul 27, 2021
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
7 changes: 4 additions & 3 deletions packages/optimizely-sdk/lib/core/project_config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
FeatureVariable,
Variation,
OptimizelyVariation,
VariationVariable,
VariableType,
VariationVariable
} from '../../shared_types';

interface TryCreatingProjectConfigConfig {
Expand Down Expand Up @@ -203,7 +204,7 @@ export const createProjectConfig = function(
// Converting it to a first-class json type while creating Project Config
feature.variables.forEach((variable) => {
if (variable.type === FEATURE_VARIABLE_TYPES.STRING && variable.subType === FEATURE_VARIABLE_TYPES.JSON) {
variable.type = FEATURE_VARIABLE_TYPES.JSON;
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this explicit cast needed for the json value but it works otherwise for other values without casting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe there is no otherwise cases of setting a value of variable.type besides this one. The FEATURE_VARIABLE_TYPES.JSON is type string and variable.type is expected to be VariableType so we needed to case it here

variable.type = FEATURE_VARIABLE_TYPES.JSON as VariableType;
delete variable.subType;
}
});
Expand Down Expand Up @@ -584,7 +585,7 @@ export const getVariableValueForVariation = function(
*/
export const getTypeCastValue = function(
variableValue: string,
variableType: string,
variableType: VariableType,
logger: LogHandler
): unknown {
let castValue;
Expand Down
10 changes: 9 additions & 1 deletion packages/optimizely-sdk/lib/shared_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,16 @@ export interface Experiment {
forcedVariations?: { [key: string]: string };
}

export enum VariableType {
BOOLEAN = 'boolean',
DOUBLE = 'double',
INTEGER = 'integer',
STRING = 'string',
JSON = 'json',
}

export interface FeatureVariable {
type: string;
type: VariableType;
key: string;
id: string;
defaultValue: string;
Expand Down