Skip to content

Commit 4536a32

Browse files
feat: added GetFeatureVariableJSON and GetAllFeatureVariables (#251)
* feat: added GetFeatureVariableJSON and GetAllFeatureVariables
1 parent 8a44852 commit 4536a32

File tree

8 files changed

+363
-88
lines changed

8 files changed

+363
-88
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ jobs:
5555
# Need to download packages explicitly
5656
- mkdir $GOPATH/src/github.com/twmb && cd $GOPATH/src/github.com/twmb && git clone https://github.com/twmb/murmur3.git && cd $TRAVIS_BUILD_DIR
5757
- pushd $GOPATH/src/github.com/twmb/murmur3 && git checkout v1.0.0 && popd
58+
- mkdir $GOPATH/src/github.com/hashicorp && cd $GOPATH/src/github.com/hashicorp && git clone https://github.com/hashicorp/go-multierror.git && cd $TRAVIS_BUILD_DIR
59+
- pushd $GOPATH/src/github.com/hashicorp/go-multierror && git checkout v1.0.0 && popd
5860
- go get -v -d ./...
5961
# This pkg not in go 1.10
6062
- go get github.com/stretchr/testify

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.12
44

55
require (
66
github.com/google/uuid v1.1.1
7+
github.com/hashicorp/go-multierror v1.1.0
78
github.com/json-iterator/go v1.1.7
89
github.com/pkg/errors v0.8.1
910
github.com/pkg/profile v1.3.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
1111
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
1212
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
1313
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
14+
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
15+
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
16+
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
17+
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
1418
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1519
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
1620
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

pkg/client/client.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import (
3030
"github.com/optimizely/go-sdk/pkg/event"
3131
"github.com/optimizely/go-sdk/pkg/logging"
3232
"github.com/optimizely/go-sdk/pkg/notification"
33+
"github.com/optimizely/go-sdk/pkg/optimizelyjson"
3334
"github.com/optimizely/go-sdk/pkg/utils"
35+
36+
"github.com/hashicorp/go-multierror"
3437
)
3538

3639
// OptimizelyClient is the entry point to the Optimizely SDK
@@ -215,6 +218,22 @@ func (o *OptimizelyClient) GetFeatureVariableString(featureKey, variableKey stri
215218
return value, err
216219
}
217220

221+
// GetFeatureVariableJSON returns the feature variable value of type json associated with the given feature and variable keys.
222+
func (o *OptimizelyClient) GetFeatureVariableJSON(featureKey, variableKey string, userContext entities.UserContext) (value *optimizelyjson.OptimizelyJSON, err error) {
223+
224+
val, valueType, err := o.GetFeatureVariable(featureKey, variableKey, userContext)
225+
if err != nil {
226+
return value, err
227+
}
228+
229+
value, err = optimizelyjson.NewOptimizelyJSONfromString(val)
230+
if err != nil || valueType != entities.JSON {
231+
return nil, fmt.Errorf("variable value for key %s is invalid or wrong type", variableKey)
232+
}
233+
234+
return value, err
235+
}
236+
218237
// GetFeatureVariable returns feature variable as a string along with it's associated type.
219238
func (o *OptimizelyClient) GetFeatureVariable(featureKey, variableKey string, userContext entities.UserContext) (value string, valueType entities.VariableType, err error) {
220239

@@ -234,8 +253,8 @@ func (o *OptimizelyClient) GetFeatureVariable(featureKey, variableKey string, us
234253
return variable.DefaultValue, variable.Type, err
235254
}
236255

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

240259
variableMap = make(map[string]interface{})
241260
decisionContext, featureDecision, err := o.getFeatureDecision(featureKey, "", userContext)
@@ -254,6 +273,8 @@ func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext
254273
return enabled, variableMap, nil
255274
}
256275

276+
errs := new(multierror.Error)
277+
257278
for _, v := range feature.VariableMap {
258279
val := v.DefaultValue
259280

@@ -268,10 +289,17 @@ func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext
268289
switch varType := v.Type; varType {
269290
case entities.Boolean:
270291
out, err = strconv.ParseBool(val)
292+
errs = multierror.Append(errs, err)
271293
case entities.Double:
272294
out, err = strconv.ParseFloat(val, 64)
295+
errs = multierror.Append(errs, err)
273296
case entities.Integer:
274297
out, err = strconv.Atoi(val)
298+
errs = multierror.Append(errs, err)
299+
case entities.JSON:
300+
var optlyJSON, err = optimizelyjson.NewOptimizelyJSONfromString(val)
301+
out = optlyJSON.ToMap()
302+
errs = multierror.Append(errs, err)
275303
case entities.String:
276304
default:
277305
o.logger.Warning(fmt.Sprintf(`type "%s" is unknown, returning string`, varType))
@@ -280,7 +308,17 @@ func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext
280308
variableMap[v.Key] = out
281309
}
282310

283-
return enabled, variableMap, err
311+
return enabled, variableMap, errs.ErrorOrNil()
312+
}
313+
314+
// GetAllFeatureVariables returns all the variables as OptimizelyJSON object for a given feature.
315+
func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext entities.UserContext) (optlyJSON *optimizelyjson.OptimizelyJSON, err error) {
316+
_, variableMap, err := o.GetAllFeatureVariablesWithDecision(featureKey, userContext)
317+
if err != nil {
318+
return optlyJSON, err
319+
}
320+
optlyJSON = optimizelyjson.NewOptimizelyJSONfromMap(variableMap)
321+
return optlyJSON, nil
284322
}
285323

286324
// GetVariation returns the key of the variation the user is bucketed into. Does not generate impression events.

0 commit comments

Comments
 (0)