Skip to content

Commit

Permalink
Use fallback value if client is not initialized
Browse files Browse the repository at this point in the history
If no defaultClient is initialized, we should return the provided
fallback value instead of `false`.
  • Loading branch information
Lucaber committed Aug 23, 2023
1 parent b18d777 commit 786c937
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
11 changes: 1 addition & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion unleash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
7 changes: 7 additions & 0 deletions unleash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 786c937

Please sign in to comment.