Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding simplified evaluation methods #263

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions openfeature/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type IClient interface {
FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (FloatEvaluationDetails, error)
IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (IntEvaluationDetails, error)
ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error)

Boolean(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) bool
String(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) string
Float(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) float64
Int(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) int64
Object(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) interface{}
}

// ClientMetadata provides a client's metadata
Expand Down Expand Up @@ -57,6 +63,9 @@ type Client struct {
logger func() logr.Logger
}

// interface guard to ensure that Client implements IClient
var _ IClient = (*Client)(nil)

// NewClient returns a new Client. Name is a unique identifier for this client
func NewClient(name string) *Client {
return &Client{
Expand Down Expand Up @@ -580,6 +589,86 @@ func (c *Client) ObjectValueDetails(ctx context.Context, flag string, defaultVal
return c.evaluate(ctx, flag, Object, defaultValue, evalCtx, *evalOptions)
}

// Boolean performs a flag evaluation that returns a boolean. Any error
// encountered during the evaluation will result in the default value being
// returned. To explicitly handle errors, use [BooleanValue] or [BooleanValueDetails]
//
// 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 e.g. WithHooks & WithHookHints
func (c *Client) Boolean(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) bool {
value, _ := c.BooleanValue(ctx, flag, defaultValue, evalCtx, options...)

return value
}

// String performs a flag evaluation that returns a string. Any error
// encountered during the evaluation will result in the default value being
// returned. To explicitly handle errors, use [StringValue] or [StringValueDetails]
//
// 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 e.g. WithHooks & WithHookHints
func (c *Client) String(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) string {
value, _ := c.StringValue(ctx, flag, defaultValue, evalCtx, options...)

return value
}

// Float performs a flag evaluation that returns a float64. Any error
// encountered during the evaluation will result in the default value being
// returned. To explicitly handle errors, use [FloatValue] or [FloatValueDetails]
//
// 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 e.g. WithHooks & WithHookHints
func (c *Client) Float(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) float64 {
value, _ := c.FloatValue(ctx, flag, defaultValue, evalCtx, options...)

return value
}

// Int performs a flag evaluation that returns an int64. Any error
// encountered during the evaluation will result in the default value being
// returned. To explicitly handle errors, use [IntValue] or [IntValueDetails]
//
// 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 e.g. WithHooks & WithHookHints
func (c *Client) Int(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) int64 {
value, _ := c.IntValue(ctx, flag, defaultValue, evalCtx, options...)

return value
}

// Object performs a flag evaluation that returns an object. Any error
// encountered during the evaluation will result in the default value being
// returned. To explicitly handle errors, use [ObjectValue] or [ObjectValueDetails]
//
// 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 e.g. WithHooks & WithHookHints
func (c *Client) Object(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) interface{} {
value, _ := c.ObjectValue(ctx, flag, defaultValue, evalCtx, options...)

return value
}

func (c *Client) evaluate(
ctx context.Context, flag string, flagType Type, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions,
) (InterfaceEvaluationDetails, error) {
Expand Down
49 changes: 49 additions & 0 deletions openfeature/client_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,52 @@ func ExampleClient_ObjectValue() {
fmt.Printf("test-flag value: %v", string(str))
// Output: test-flag value: {"foo":"bar"}
}

func ExampleClient_Boolean() {
ctx := context.Background()
client := openfeature.NewClient("example-client")

if client.Boolean(ctx, "myflag", true, openfeature.EvaluationContext{}) {
fmt.Println("myflag is true")
} else {
fmt.Println("myflag is false")
}

// Output: myflag is true
}

func ExampleClient_String() {
ctx := context.Background()
client := openfeature.NewClient("example-client")

fmt.Println(client.String(ctx, "myflag", "default", openfeature.EvaluationContext{}))

// Output: default
}

func ExampleClient_Float() {
ctx := context.Background()
client := openfeature.NewClient("example-client")

fmt.Println(client.Float(ctx, "myflag", 0.5, openfeature.EvaluationContext{}))

// Output: 0.5
}

func ExampleClient_Int() {
ctx := context.Background()
client := openfeature.NewClient("example-client")

fmt.Println(client.Int(ctx, "myflag", 5, openfeature.EvaluationContext{}))

// Output: 5
}

func ExampleClient_Object() {
ctx := context.Background()
client := openfeature.NewClient("example-client")

fmt.Println(client.Object(ctx, "myflag", map[string]string{"foo": "bar"}, openfeature.EvaluationContext{}))

// Output: map[foo:bar]
}
Loading