@@ -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.
219238func (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