@@ -17,7 +17,15 @@ const (
17
17
errorWrongVariation = "wrong variation used for flag %v"
18
18
)
19
19
20
- var offlineVariationResult = model.VariationResult {VariationType : flag .VariationSDKDefault , Failed : true }
20
+ var (
21
+ offlineVariationResult = model.VariationResult {VariationType : flag .VariationSDKDefault , Failed : false }
22
+ notInitVariationResult = model.VariationResult {
23
+ VariationType : flag .VariationSDKDefault ,
24
+ Failed : true ,
25
+ Reason : flag .ReasonError ,
26
+ ErrorCode : flag .ErrorCodeProviderNotReady ,
27
+ }
28
+ )
21
29
22
30
// BoolVariation return the value of the flag in boolean.
23
31
// An error is return if you don't have init the library before calling the function.
@@ -143,6 +151,10 @@ func (g *GoFeatureFlag) JSONVariation(
143
151
// AllFlagsState return a flagstate.AllFlags that contains all the flags for a specific user.
144
152
func (g * GoFeatureFlag ) AllFlagsState (user ffuser.User ) flagstate.AllFlags {
145
153
flags := map [string ]flag.Flag {}
154
+ if g == nil {
155
+ // empty AllFlags will set valid to false
156
+ return flagstate.AllFlags {}
157
+ }
146
158
147
159
if ! g .config .Offline {
148
160
var err error
@@ -211,9 +223,14 @@ func (g *GoFeatureFlag) GetFlagsFromCache() (map[string]flag.Flag, error) {
211
223
}
212
224
213
225
// boolVariation is the internal func that handle the logic of a variation with a bool value
214
- // the result will always contains a valid model.BoolVarResult
226
+ // the result will always contain a valid model.BoolVarResult
215
227
func (g * GoFeatureFlag ) boolVariation (flagKey string , user ffuser.User , sdkDefaultValue bool ,
216
228
) (model.BoolVarResult , error ) {
229
+ if g == nil {
230
+ return model.BoolVarResult {Value : sdkDefaultValue , VariationResult : notInitVariationResult },
231
+ fmt .Errorf ("go-feature-flag is not initialised, default value is used" )
232
+ }
233
+
217
234
if g .config .Offline {
218
235
return model.BoolVarResult {Value : sdkDefaultValue , VariationResult : offlineVariationResult }, nil
219
236
}
@@ -254,6 +271,11 @@ func (g *GoFeatureFlag) boolVariation(flagKey string, user ffuser.User, sdkDefau
254
271
// the result will always contain a valid model.IntVarResult
255
272
func (g * GoFeatureFlag ) intVariation (flagKey string , user ffuser.User , sdkDefaultValue int ,
256
273
) (model.IntVarResult , error ) {
274
+ if g == nil {
275
+ return model.IntVarResult {Value : sdkDefaultValue , VariationResult : notInitVariationResult },
276
+ fmt .Errorf ("go-feature-flag is not initialised, default value is used" )
277
+ }
278
+
257
279
if g .config .Offline {
258
280
return model.IntVarResult {Value : sdkDefaultValue , VariationResult : offlineVariationResult }, nil
259
281
}
@@ -298,9 +320,14 @@ func (g *GoFeatureFlag) intVariation(flagKey string, user ffuser.User, sdkDefaul
298
320
}
299
321
300
322
// float64Variation is the internal func that handle the logic of a variation with a float64 value
301
- // the result will always contains a valid model.Float64VarResult
323
+ // the result will always contain a valid model.Float64VarResult
302
324
func (g * GoFeatureFlag ) float64Variation (flagKey string , user ffuser.User , sdkDefaultValue float64 ,
303
325
) (model.Float64VarResult , error ) {
326
+ if g == nil {
327
+ return model.Float64VarResult {Value : sdkDefaultValue , VariationResult : notInitVariationResult },
328
+ fmt .Errorf ("go-feature-flag is not initialised, default value is used" )
329
+ }
330
+
304
331
if g .config .Offline {
305
332
return model.Float64VarResult {Value : sdkDefaultValue , VariationResult : offlineVariationResult }, nil
306
333
}
@@ -337,9 +364,14 @@ func (g *GoFeatureFlag) float64Variation(flagKey string, user ffuser.User, sdkDe
337
364
}
338
365
339
366
// stringVariation is the internal func that handle the logic of a variation with a string value
340
- // the result will always contains a valid model.StringVarResult
367
+ // the result will always contain a valid model.StringVarResult
341
368
func (g * GoFeatureFlag ) stringVariation (flagKey string , user ffuser.User , sdkDefaultValue string ,
342
369
) (model.StringVarResult , error ) {
370
+ if g == nil {
371
+ return model.StringVarResult {Value : sdkDefaultValue , VariationResult : notInitVariationResult },
372
+ fmt .Errorf ("go-feature-flag is not initialised, default value is used" )
373
+ }
374
+
343
375
if g .config .Offline {
344
376
return model.StringVarResult {Value : sdkDefaultValue , VariationResult : offlineVariationResult }, nil
345
377
}
@@ -376,9 +408,14 @@ func (g *GoFeatureFlag) stringVariation(flagKey string, user ffuser.User, sdkDef
376
408
}
377
409
378
410
// jsonArrayVariation is the internal func that handle the logic of a variation with a json value
379
- // the result will always contains a valid model.JSONArrayVarResult
411
+ // the result will always contain a valid model.JSONArrayVarResult
380
412
func (g * GoFeatureFlag ) jsonArrayVariation (flagKey string , user ffuser.User , sdkDefaultValue []interface {},
381
413
) (model.JSONArrayVarResult , error ) {
414
+ if g == nil {
415
+ return model.JSONArrayVarResult {Value : sdkDefaultValue , VariationResult : notInitVariationResult },
416
+ fmt .Errorf ("go-feature-flag is not initialised, default value is used" )
417
+ }
418
+
382
419
if g .config .Offline {
383
420
return model.JSONArrayVarResult {Value : sdkDefaultValue , VariationResult : offlineVariationResult }, nil
384
421
}
@@ -415,9 +452,14 @@ func (g *GoFeatureFlag) jsonArrayVariation(flagKey string, user ffuser.User, sdk
415
452
}
416
453
417
454
// jsonVariation is the internal func that handle the logic of a variation with a json value
418
- // the result will always contains a valid model.JSONVarResult
455
+ // the result will always contain a valid model.JSONVarResult
419
456
func (g * GoFeatureFlag ) jsonVariation (flagKey string , user ffuser.User , sdkDefaultValue map [string ]interface {},
420
457
) (model.JSONVarResult , error ) {
458
+ if g == nil {
459
+ return model.JSONVarResult {Value : sdkDefaultValue , VariationResult : notInitVariationResult },
460
+ fmt .Errorf ("go-feature-flag is not initialised, default value is used" )
461
+ }
462
+
421
463
if g .config .Offline {
422
464
return model.JSONVarResult {Value : sdkDefaultValue , VariationResult : offlineVariationResult }, nil
423
465
}
@@ -477,6 +519,11 @@ func computeVariationResult(flag flag.Flag, resolutionDetails flag.ResolutionDet
477
519
// Note: Use this function only if you are using multiple go-feature-flag instances.
478
520
func (g * GoFeatureFlag ) RawVariation (flagKey string , user ffuser.User , sdkDefaultValue interface {},
479
521
) (model.RawVarResult , error ) {
522
+ if g == nil {
523
+ return model.RawVarResult {Value : sdkDefaultValue , VariationResult : notInitVariationResult },
524
+ fmt .Errorf ("go-feature-flag is not initialised, default value is used" )
525
+ }
526
+
480
527
if g .config .Offline {
481
528
return model.RawVarResult {Value : sdkDefaultValue , VariationResult : offlineVariationResult }, nil
482
529
}
0 commit comments