@@ -30,7 +30,10 @@ import (
30
30
"github.com/optimizely/go-sdk/pkg/event"
31
31
"github.com/optimizely/go-sdk/pkg/logging"
32
32
"github.com/optimizely/go-sdk/pkg/notification"
33
+ "github.com/optimizely/go-sdk/pkg/optimizelyjson"
33
34
"github.com/optimizely/go-sdk/pkg/utils"
35
+
36
+ "github.com/hashicorp/go-multierror"
34
37
)
35
38
36
39
// OptimizelyClient is the entry point to the Optimizely SDK
@@ -215,6 +218,22 @@ func (o *OptimizelyClient) GetFeatureVariableString(featureKey, variableKey stri
215
218
return value , err
216
219
}
217
220
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
+
218
237
// GetFeatureVariable returns feature variable as a string along with it's associated type.
219
238
func (o * OptimizelyClient ) GetFeatureVariable (featureKey , variableKey string , userContext entities.UserContext ) (value string , valueType entities.VariableType , err error ) {
220
239
@@ -234,8 +253,8 @@ func (o *OptimizelyClient) GetFeatureVariable(featureKey, variableKey string, us
234
253
return variable .DefaultValue , variable .Type , err
235
254
}
236
255
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 ) {
239
258
240
259
variableMap = make (map [string ]interface {})
241
260
decisionContext , featureDecision , err := o .getFeatureDecision (featureKey , "" , userContext )
@@ -254,6 +273,8 @@ func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext
254
273
return enabled , variableMap , nil
255
274
}
256
275
276
+ errs := new (multierror.Error )
277
+
257
278
for _ , v := range feature .VariableMap {
258
279
val := v .DefaultValue
259
280
@@ -268,10 +289,17 @@ func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext
268
289
switch varType := v .Type ; varType {
269
290
case entities .Boolean :
270
291
out , err = strconv .ParseBool (val )
292
+ errs = multierror .Append (errs , err )
271
293
case entities .Double :
272
294
out , err = strconv .ParseFloat (val , 64 )
295
+ errs = multierror .Append (errs , err )
273
296
case entities .Integer :
274
297
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 )
275
303
case entities .String :
276
304
default :
277
305
o .logger .Warning (fmt .Sprintf (`type "%s" is unknown, returning string` , varType ))
@@ -280,7 +308,17 @@ func (o *OptimizelyClient) GetAllFeatureVariables(featureKey string, userContext
280
308
variableMap [v .Key ] = out
281
309
}
282
310
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
284
322
}
285
323
286
324
// GetVariation returns the key of the variation the user is bucketed into. Does not generate impression events.
0 commit comments