diff --git a/client.go b/client.go index f725bd7..f03a45e 100644 --- a/client.go +++ b/client.go @@ -267,17 +267,8 @@ func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.Strate } if f == nil { - if opts.fallbackFunc != nil { - return api.StrategyResult{ - Enabled: opts.fallbackFunc(feature, ctx), - } - } else if opts.fallback != nil { - return api.StrategyResult{ - Enabled: *opts.fallback, - } - } return api.StrategyResult{ - Enabled: false, + Enabled: opts.FallbackForFeature(feature, ctx), } } diff --git a/config.go b/config.go index 75e1948..6247e5d 100644 --- a/config.go +++ b/config.go @@ -159,6 +159,16 @@ type featureOption struct { resolver FeatureResolver } +func (fo featureOption) FallbackForFeature(feature string, ctx *context.Context) bool { + if fo.fallbackFunc != nil { + return fo.fallbackFunc(feature, ctx) + } + if fo.fallback != nil { + return *fo.fallback + } + return false +} + // FeatureOption provides options for querying if a feature is enabled or not. type FeatureOption func(*featureOption) diff --git a/unleash.go b/unleash.go index f41acd6..f06bac3 100644 --- a/unleash.go +++ b/unleash.go @@ -38,7 +38,11 @@ type RepositoryListener interface { // IsEnabled queries the default client whether or not the specified feature is enabled or not. func IsEnabled(feature string, options ...FeatureOption) bool { if defaultClient == nil { - return false + var opts featureOption + for _, o := range options { + o(&opts) + } + return opts.FallbackForFeature(feature, opts.ctx) } return defaultClient.IsEnabled(feature, options...) } diff --git a/unleash_test.go b/unleash_test.go index f4d8503..2f4d1e2 100644 --- a/unleash_test.go +++ b/unleash_test.go @@ -64,3 +64,10 @@ func Test_withVariantsAndANonExistingStrategyName(t *testing.T) { t.Fatalf("Expected feature to be disabled because Environment does not exist as strategy") } } + +func Test_IsEnabledWithUninitializedClient(t *testing.T) { + result := unleash.IsEnabled("foo", unleash.WithFallback(true)) + if !result { + t.Fatalf("Expected true") + } +}