Skip to content

Commit 0fbb714

Browse files
committed
Revert WithDefaultFlagHandler
1 parent 78027a2 commit 0fbb714

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type Client struct {
5656
// flagsmithClient := flagsmith.NewClient(
5757
// os.Getenv("FLAGSMITH_SDK_KEY"),
5858
// flagsmith.WithLocalEvaluation(context.Background()),
59-
// flagsmith.WithDefaultFlagHandler(GetDefaultFlag),
59+
// flagsmith.WithDefaultHandler(GetDefaultFlag),
6060
// )
6161
func NewClient(apiKey string, options ...Option) *Client {
6262
c := &Client{
@@ -119,7 +119,7 @@ func NewClient(apiKey string, options ...Option) *Client {
119119
// GetFlags evaluates the feature flags within an [EvaluationContext].
120120
//
121121
// When flag evaluation fails, the return value is determined by the default flag handler from
122-
// [WithDefaultFlagHandler], if one was provided.
122+
// [WithDefaultHandler], if one was provided.
123123
//
124124
// Flags are evaluated remotely by the Flagsmith API by default.
125125
// To evaluate flags locally, instantiate a client using [WithLocalEvaluation].

client_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestClientErrorsIfDefaultHandlerAndOfflineHandlerAreBothSet(t *testing.T) {
7979
// Trigger panic
8080
_ = flagsmith.NewClient("key",
8181
flagsmith.WithOfflineHandler(offlineHandler),
82-
flagsmith.WithDefaultFlagHandler(func(featureName string) (flagsmith.Flag, error) {
82+
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
8383
return flagsmith.Flag{}, nil
8484
}))
8585
}
@@ -343,7 +343,7 @@ func TestGetEnvironmentFlagsCallsAPIWhenLocalEnvironmentNotAvailable(t *testing.
343343

344344
// When
345345
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
346-
flagsmith.WithDefaultFlagHandler(func(featureName string) (flagsmith.Flag, error) {
346+
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
347347
return flagsmith.Flag{}, nil
348348
}))
349349

@@ -501,7 +501,7 @@ func TestDefaultHandlerIsUsedWhenNoMatchingEnvironmentFlagReturned(t *testing.T)
501501

502502
// When
503503
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
504-
flagsmith.WithDefaultFlagHandler(func(featureName string) (flagsmith.Flag, error) {
504+
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
505505
return flagsmith.Flag{}, nil
506506
}))
507507

@@ -534,7 +534,7 @@ func TestDefaultHandlerIsUsedWhenTimeout(t *testing.T) {
534534
// When
535535
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
536536
flagsmith.WithRequestTimeout(10*time.Millisecond),
537-
flagsmith.WithDefaultFlagHandler(func(featureName string) (flagsmith.Flag, error) {
537+
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
538538
return flagsmith.Flag{}, nil
539539
}))
540540

@@ -556,7 +556,7 @@ func TestDefaultHandlerIsUsedWhenRequestFails(t *testing.T) {
556556

557557
// When
558558
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
559-
flagsmith.WithDefaultFlagHandler(func(featureName string) (flagsmith.Flag, error) {
559+
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
560560
return flagsmith.Flag{}, nil
561561
}))
562562

options.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,6 @@ func WithCustomHeaders(headers map[string]string) Option {
7676
}
7777

7878
// WithDefaultHandler sets a handler function used to return fallback values when [Client.GetFlags] would have normally
79-
// returned an error.
80-
//
81-
// Deprecated: Use WithDefaultFlagHandler instead.
82-
func WithDefaultHandler(handler func(string) (Flag, error)) Option {
83-
return func(c *Client) {
84-
c.defaultFlagHandler = handler
85-
}
86-
}
87-
88-
// WithDefaultFlagHandler sets a handler function used to return fallback values when [Client.GetFlags] would have normally
8979
// returned an error. For example, this handler makes all flags be disabled by default:
9080
//
9181
// func handler(flagKey string) (Flag, error) {
@@ -94,20 +84,16 @@ func WithDefaultHandler(handler func(string) (Flag, error)) Option {
9484
// Enabled: false,
9585
// }, nil
9686
// }
97-
func WithDefaultFlagHandler(handler func(string) (Flag, error)) Option {
98-
f := func(flagKey string) (Flag, error) {
99-
defaultFlag, err := handler(flagKey)
100-
if err != nil {
101-
return Flag{}, err
102-
}
103-
return Flag{
104-
IsDefault: true,
105-
FeatureName: flagKey,
106-
Enabled: defaultFlag.Enabled,
107-
Value: defaultFlag.Value,
108-
}, nil
109-
}
110-
return WithDefaultHandler(f)
87+
func WithDefaultHandler(handler func(string) (Flag, error)) Option {
88+
f := func(flagKey string) (flag Flag, err error) {
89+
flag, err = handler(flagKey)
90+
flag.IsDefault = true
91+
flag.FeatureName = flagKey
92+
return flag, err
93+
}
94+
return func(c *Client) {
95+
c.defaultFlagHandler = f
96+
}
11197
}
11298

11399
// WithLogger sets a custom [slog.Logger] for the [Client].

0 commit comments

Comments
 (0)