Skip to content

Commit

Permalink
feat!: replaced EvaluationOptions with variadic option setter in clie…
Browse files Browse the repository at this point in the history
…nt functions (#77)

* feat!: replaced EvaluationOptions with variadic option setter in client functions

Signed-off-by: Skye Gill <[email protected]>
  • Loading branch information
skyerus authored Sep 23, 2022
1 parent d850c88 commit fc4b871
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 134 deletions.
147 changes: 107 additions & 40 deletions pkg/openfeature/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ type IClient interface {
AddHooks(hooks ...Hook)
SetEvaluationContext(evalCtx EvaluationContext)
EvaluationContext() EvaluationContext
BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options EvaluationOptions) (bool, error)
StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options EvaluationOptions) (string, error)
FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options EvaluationOptions) (float64, error)
IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options EvaluationOptions) (int64, error)
ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions) (interface{}, error)
BooleanValueDetails(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
StringValueDetails(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (bool, error)
StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (string, error)
FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (float64, error)
IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (int64, error)
ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (interface{}, error)
BooleanValueDetails(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error)
StringValueDetails(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error)
FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error)
IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error)
ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error)
}

// ClientMetadata provides a client's metadata
Expand Down Expand Up @@ -113,16 +113,38 @@ type EvaluationDetails struct {
InterfaceResolutionDetail
}

// Option applies a change to EvaluationOptions
type Option func(*EvaluationOptions)

// WithHooks applies provided hooks.
func WithHooks(hooks ...Hook) Option {
return func(options *EvaluationOptions) {
options.hooks = hooks
}
}

// WithHookHints applies provided hook hints.
func WithHookHints(hookHints HookHints) Option {
return func(options *EvaluationOptions) {
options.hookHints = hookHints
}
}

// BooleanValue performs a flag evaluation that returns a boolean.
//
// Parameters:
// - ctx is the standard go context struct used to manage requests (e.g. timeouts)
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options EvaluationOptions) (bool, error) {
evalDetails, err := c.evaluate(ctx, flag, Boolean, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (bool, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

evalDetails, err := c.evaluate(ctx, flag, Boolean, defaultValue, evalCtx, *evalOptions)
if err != nil {
return defaultValue, err
}
Expand All @@ -147,9 +169,14 @@ func (c Client) BooleanValue(ctx context.Context, flag string, defaultValue bool
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options EvaluationOptions) (string, error) {
evalDetails, err := c.evaluate(ctx, flag, String, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (string, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

evalDetails, err := c.evaluate(ctx, flag, String, defaultValue, evalCtx, *evalOptions)
if err != nil {
return defaultValue, err
}
Expand All @@ -174,9 +201,14 @@ func (c Client) StringValue(ctx context.Context, flag string, defaultValue strin
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options EvaluationOptions) (float64, error) {
evalDetails, err := c.evaluate(ctx, flag, Float, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (float64, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

evalDetails, err := c.evaluate(ctx, flag, Float, defaultValue, evalCtx, *evalOptions)
if err != nil {
return defaultValue, err
}
Expand All @@ -201,9 +233,14 @@ func (c Client) FloatValue(ctx context.Context, flag string, defaultValue float6
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options EvaluationOptions) (int64, error) {
evalDetails, err := c.evaluate(ctx, flag, Int, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (int64, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

evalDetails, err := c.evaluate(ctx, flag, Int, defaultValue, evalCtx, *evalOptions)
if err != nil {
return defaultValue, err
}
Expand All @@ -228,9 +265,14 @@ func (c Client) IntValue(ctx context.Context, flag string, defaultValue int64, e
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions) (interface{}, error) {
evalDetails, err := c.evaluate(ctx, flag, Object, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (interface{}, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

evalDetails, err := c.evaluate(ctx, flag, Object, defaultValue, evalCtx, *evalOptions)
return evalDetails.Value, err
}

Expand All @@ -241,9 +283,14 @@ func (c Client) ObjectValue(ctx context.Context, flag string, defaultValue inter
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) BooleanValueDetails(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error) {
return c.evaluate(ctx, flag, Boolean, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) BooleanValueDetails(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

return c.evaluate(ctx, flag, Boolean, defaultValue, evalCtx, *evalOptions)
}

// StringValueDetails performs a flag evaluation that returns an evaluation details struct.
Expand All @@ -253,9 +300,14 @@ func (c Client) BooleanValueDetails(ctx context.Context, flag string, defaultVal
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) StringValueDetails(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error) {
return c.evaluate(ctx, flag, String, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) StringValueDetails(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

return c.evaluate(ctx, flag, String, defaultValue, evalCtx, *evalOptions)
}

// FloatValueDetails performs a flag evaluation that returns an evaluation details struct.
Expand All @@ -265,9 +317,14 @@ func (c Client) StringValueDetails(ctx context.Context, flag string, defaultValu
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error) {
return c.evaluate(ctx, flag, Float, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

return c.evaluate(ctx, flag, Float, defaultValue, evalCtx, *evalOptions)
}

// IntValueDetails performs a flag evaluation that returns an evaluation details struct.
Expand All @@ -277,9 +334,14 @@ func (c Client) FloatValueDetails(ctx context.Context, flag string, defaultValue
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error) {
return c.evaluate(ctx, flag, Int, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

return c.evaluate(ctx, flag, Int, defaultValue, evalCtx, *evalOptions)
}

// ObjectValueDetails performs a flag evaluation that returns an evaluation details struct.
Expand All @@ -289,9 +351,14 @@ func (c Client) IntValueDetails(ctx context.Context, flag string, defaultValue i
// - flag is the key that uniquely identifies a particular flag
// - defaultValue is returned if an error occurs
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
// - options are optional additional evaluation options
func (c Client) ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error) {
return c.evaluate(ctx, flag, Object, defaultValue, evalCtx, options)
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (c Client) ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (EvaluationDetails, error) {
evalOptions := &EvaluationOptions{}
for _, option := range options {
option(evalOptions)
}

return c.evaluate(ctx, flag, Object, defaultValue, evalCtx, *evalOptions)
}

func (c Client) evaluate(
Expand Down
10 changes: 5 additions & 5 deletions pkg/openfeature/client_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ExampleNewClient() {
func ExampleClient_BooleanValue() {
client := openfeature.NewClient("example-client")
value, err := client.BooleanValue(
context.Background(), "test-flag", true, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
context.Background(), "test-flag", true, openfeature.EvaluationContext{},
)
if err != nil {
log.Fatal("error while getting boolean value : ", err)
Expand All @@ -31,7 +31,7 @@ func ExampleClient_BooleanValue() {
func ExampleClient_StringValue() {
client := openfeature.NewClient("example-client")
value, err := client.StringValue(
context.Background(), "test-flag", "openfeature", openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
context.Background(), "test-flag", "openfeature", openfeature.EvaluationContext{},
)
if err != nil {
log.Fatal("error while getting string value : ", err)
Expand All @@ -44,7 +44,7 @@ func ExampleClient_StringValue() {
func ExampleClient_FloatValue() {
client := openfeature.NewClient("example-client")
value, err := client.FloatValue(
context.Background(), "test-flag", 0.55, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
context.Background(), "test-flag", 0.55, openfeature.EvaluationContext{},
)
if err != nil {
log.Fatalf("error while getting float value: %v", err)
Expand All @@ -57,7 +57,7 @@ func ExampleClient_FloatValue() {
func ExampleClient_IntValue() {
client := openfeature.NewClient("example-client")
value, err := client.IntValue(
context.Background(), "test-flag", 3, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
context.Background(), "test-flag", 3, openfeature.EvaluationContext{},
)
if err != nil {
log.Fatalf("error while getting int value: %v", err)
Expand All @@ -70,7 +70,7 @@ func ExampleClient_IntValue() {
func ExampleClient_ObjectValue() {
client := openfeature.NewClient("example-client")
value, err := client.ObjectValue(
context.Background(), "test-flag", map[string]string{"foo": "bar"}, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
context.Background(), "test-flag", map[string]string{"foo": "bar"}, openfeature.EvaluationContext{},
)
if err != nil {
log.Fatal("error while getting object value : ", err)
Expand Down
Loading

0 comments on commit fc4b871

Please sign in to comment.