Skip to content

Commit b5b5433

Browse files
author
Mike Davis
authored
Return variables as an interface{} from GetAllFeatureVariables. (#235)
1 parent f3e3d3e commit b5b5433

File tree

2 files changed

+71
-19
lines changed

2 files changed

+71
-19
lines changed

pkg/client/client.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ func (o *OptimizelyClient) GetFeatureVariable(featureKey, variableKey string, us
236236
}
237237

238238
// GetAllFeatureVariables returns all the variables for a given feature along with the enabled state.
239-
func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext entities.UserContext) (enabled bool, variableMap map[string]string, err error) {
239+
func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext entities.UserContext) (enabled bool, variableMap map[string]interface{}, err error) {
240240

241-
variableMap = make(map[string]string)
241+
variableMap = make(map[string]interface{})
242242
decisionContext, featureDecision, err := o.getFeatureDecision(featureKey, "", userContext)
243243
if err != nil {
244244
logger.Error("Optimizely SDK tracking error", err)
@@ -256,13 +256,29 @@ func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext
256256
}
257257

258258
for _, v := range feature.VariableMap {
259-
variableMap[v.Key] = v.DefaultValue
259+
val := v.DefaultValue
260260

261261
if enabled {
262262
if variable, ok := featureDecision.Variation.Variables[v.ID]; ok {
263-
variableMap[v.Key] = variable.Value
263+
val = variable.Value
264264
}
265265
}
266+
267+
var out interface{}
268+
out = val
269+
switch varType := v.Type; varType {
270+
case entities.Boolean:
271+
out, err = strconv.ParseBool(val)
272+
case entities.Double:
273+
out, err = strconv.ParseFloat(val, 64)
274+
case entities.Integer:
275+
out, err = strconv.Atoi(val)
276+
case entities.String:
277+
default:
278+
logger.Warning(fmt.Sprintf(`type "%s" is unknown, returning string`, varType))
279+
}
280+
281+
variableMap[v.Key] = out
266282
}
267283

268284
return enabled, variableMap, err

pkg/client/client_test.go

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"strconv"
2324
"sync"
2425
"testing"
2526

@@ -1442,28 +1443,60 @@ func TestGetFeatureDecisionErrFeatureDecision(t *testing.T) {
14421443

14431444
func TestGetAllFeatureVariables(t *testing.T) {
14441445
testFeatureKey := "test_feature_key"
1445-
testVariableKey := "test_feature_flag_key"
1446-
testVariableValue := "teststring"
14471446
testUserContext := entities.UserContext{ID: "test_user_1"}
1448-
testVariationVariable := entities.VariationVariable{
1449-
ID: "1",
1450-
Value: testVariableValue,
1447+
1448+
type variable struct {
1449+
key string
1450+
defaultVal string
1451+
varVal string
1452+
varType entities.VariableType
1453+
expected interface{}
14511454
}
1452-
testVariable := entities.Variable{
1453-
DefaultValue: "defaultString",
1454-
ID: "1",
1455-
Key: testVariableKey,
1456-
Type: entities.String,
1455+
1456+
variables := []variable{
1457+
{key: "var_str", defaultVal: "default", varVal: "var", varType: entities.String, expected: "var"},
1458+
{key: "var_bool", defaultVal: "false", varVal: "true", varType: entities.Boolean, expected: true},
1459+
{key: "var_int", defaultVal: "10", varVal: "20", varType: entities.Integer, expected: 20},
1460+
{key: "var_double", defaultVal: "1.0", varVal: "2.0", varType: entities.Double, expected: 2.0},
14571461
}
1458-
testVariation := getTestVariationWithFeatureVariable(false, testVariationVariable)
1462+
1463+
mockConfig := new(MockProjectConfig)
1464+
variableMap := make(map[string]entities.Variable)
1465+
varVariableMap := make(map[string]entities.VariationVariable)
1466+
1467+
for i, v := range variables {
1468+
id := strconv.Itoa(i)
1469+
varVariableMap[id] = entities.VariationVariable{
1470+
ID: id,
1471+
Value: v.varVal,
1472+
}
1473+
1474+
variableMap[id] = entities.Variable{
1475+
DefaultValue: v.defaultVal,
1476+
ID: id,
1477+
Key: v.key,
1478+
Type: v.varType,
1479+
}
1480+
1481+
mockConfig.On("GetVariableByKey", testFeatureKey, v.key).Return(v.varVal, nil)
1482+
}
1483+
1484+
testVariation := entities.Variation{
1485+
ID: "22222",
1486+
Key: "22222",
1487+
FeatureEnabled: true,
1488+
Variables: varVariableMap,
1489+
}
1490+
14591491
testVariation.FeatureEnabled = true
14601492
testExperiment := entities.Experiment{
14611493
ID: "111111",
14621494
Variations: map[string]entities.Variation{"22222": testVariation},
14631495
}
14641496
testFeature := getTestFeature(testFeatureKey, testExperiment)
1465-
testFeature.VariableMap = map[string]entities.Variable{testVariable.Key: testVariable}
1466-
mockConfig := getMockConfig(testFeatureKey, testVariableKey, testFeature, testVariable)
1497+
testFeature.VariableMap = variableMap
1498+
mockConfig.On("GetFeatureByKey", testFeatureKey).Return(testFeature, nil)
1499+
14671500
mockConfigManager := new(MockProjectConfigManager)
14681501
mockConfigManager.On("GetConfig").Return(mockConfig, nil)
14691502

@@ -1482,9 +1515,12 @@ func TestGetAllFeatureVariables(t *testing.T) {
14821515
}
14831516

14841517
enabled, variationMap, err := client.GetAllFeatureVariables(testFeatureKey, testUserContext)
1518+
assert.NoError(t, err)
14851519
assert.True(t, enabled)
1486-
assert.Equal(t, testVariableValue, variationMap[testVariableKey])
1487-
assert.Nil(t, err)
1520+
1521+
for _, v := range variables {
1522+
assert.Equal(t, v.expected, variationMap[v.key])
1523+
}
14881524
}
14891525

14901526
func TestGetAllFeatureVariablesWithError(t *testing.T) {

0 commit comments

Comments
 (0)