Skip to content

Commit 7b610c7

Browse files
feat: Adding simplified evaluation methods (#263)
1 parent b624a43 commit 7b610c7

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

openfeature/client.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ type IClient interface {
2828
FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (FloatEvaluationDetails, error)
2929
IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (IntEvaluationDetails, error)
3030
ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error)
31+
32+
Boolean(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) bool
33+
String(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) string
34+
Float(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) float64
35+
Int(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) int64
36+
Object(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) interface{}
3137
}
3238

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

66+
// interface guard to ensure that Client implements IClient
67+
var _ IClient = (*Client)(nil)
68+
6069
// NewClient returns a new Client. Name is a unique identifier for this client
6170
func NewClient(name string) *Client {
6271
return &Client{
@@ -580,6 +589,86 @@ func (c *Client) ObjectValueDetails(ctx context.Context, flag string, defaultVal
580589
return c.evaluate(ctx, flag, Object, defaultValue, evalCtx, *evalOptions)
581590
}
582591

592+
// Boolean performs a flag evaluation that returns a boolean. Any error
593+
// encountered during the evaluation will result in the default value being
594+
// returned. To explicitly handle errors, use [BooleanValue] or [BooleanValueDetails]
595+
//
596+
// Parameters:
597+
// - ctx is the standard go context struct used to manage requests (e.g. timeouts)
598+
// - flag is the key that uniquely identifies a particular flag
599+
// - defaultValue is returned if an error occurs
600+
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
601+
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
602+
func (c *Client) Boolean(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) bool {
603+
value, _ := c.BooleanValue(ctx, flag, defaultValue, evalCtx, options...)
604+
605+
return value
606+
}
607+
608+
// String performs a flag evaluation that returns a string. Any error
609+
// encountered during the evaluation will result in the default value being
610+
// returned. To explicitly handle errors, use [StringValue] or [StringValueDetails]
611+
//
612+
// Parameters:
613+
// - ctx is the standard go context struct used to manage requests (e.g. timeouts)
614+
// - flag is the key that uniquely identifies a particular flag
615+
// - defaultValue is returned if an error occurs
616+
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
617+
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
618+
func (c *Client) String(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) string {
619+
value, _ := c.StringValue(ctx, flag, defaultValue, evalCtx, options...)
620+
621+
return value
622+
}
623+
624+
// Float performs a flag evaluation that returns a float64. Any error
625+
// encountered during the evaluation will result in the default value being
626+
// returned. To explicitly handle errors, use [FloatValue] or [FloatValueDetails]
627+
//
628+
// Parameters:
629+
// - ctx is the standard go context struct used to manage requests (e.g. timeouts)
630+
// - flag is the key that uniquely identifies a particular flag
631+
// - defaultValue is returned if an error occurs
632+
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
633+
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
634+
func (c *Client) Float(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) float64 {
635+
value, _ := c.FloatValue(ctx, flag, defaultValue, evalCtx, options...)
636+
637+
return value
638+
}
639+
640+
// Int performs a flag evaluation that returns an int64. Any error
641+
// encountered during the evaluation will result in the default value being
642+
// returned. To explicitly handle errors, use [IntValue] or [IntValueDetails]
643+
//
644+
// Parameters:
645+
// - ctx is the standard go context struct used to manage requests (e.g. timeouts)
646+
// - flag is the key that uniquely identifies a particular flag
647+
// - defaultValue is returned if an error occurs
648+
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
649+
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
650+
func (c *Client) Int(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) int64 {
651+
value, _ := c.IntValue(ctx, flag, defaultValue, evalCtx, options...)
652+
653+
return value
654+
}
655+
656+
// Object performs a flag evaluation that returns an object. Any error
657+
// encountered during the evaluation will result in the default value being
658+
// returned. To explicitly handle errors, use [ObjectValue] or [ObjectValueDetails]
659+
//
660+
// Parameters:
661+
// - ctx is the standard go context struct used to manage requests (e.g. timeouts)
662+
// - flag is the key that uniquely identifies a particular flag
663+
// - defaultValue is returned if an error occurs
664+
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
665+
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
666+
func (c *Client) Object(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) interface{} {
667+
value, _ := c.ObjectValue(ctx, flag, defaultValue, evalCtx, options...)
668+
669+
return value
670+
}
671+
583672
func (c *Client) evaluate(
584673
ctx context.Context, flag string, flagType Type, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions,
585674
) (InterfaceEvaluationDetails, error) {

openfeature/client_example_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,52 @@ func ExampleClient_ObjectValue() {
8080
fmt.Printf("test-flag value: %v", string(str))
8181
// Output: test-flag value: {"foo":"bar"}
8282
}
83+
84+
func ExampleClient_Boolean() {
85+
ctx := context.Background()
86+
client := openfeature.NewClient("example-client")
87+
88+
if client.Boolean(ctx, "myflag", true, openfeature.EvaluationContext{}) {
89+
fmt.Println("myflag is true")
90+
} else {
91+
fmt.Println("myflag is false")
92+
}
93+
94+
// Output: myflag is true
95+
}
96+
97+
func ExampleClient_String() {
98+
ctx := context.Background()
99+
client := openfeature.NewClient("example-client")
100+
101+
fmt.Println(client.String(ctx, "myflag", "default", openfeature.EvaluationContext{}))
102+
103+
// Output: default
104+
}
105+
106+
func ExampleClient_Float() {
107+
ctx := context.Background()
108+
client := openfeature.NewClient("example-client")
109+
110+
fmt.Println(client.Float(ctx, "myflag", 0.5, openfeature.EvaluationContext{}))
111+
112+
// Output: 0.5
113+
}
114+
115+
func ExampleClient_Int() {
116+
ctx := context.Background()
117+
client := openfeature.NewClient("example-client")
118+
119+
fmt.Println(client.Int(ctx, "myflag", 5, openfeature.EvaluationContext{}))
120+
121+
// Output: 5
122+
}
123+
124+
func ExampleClient_Object() {
125+
ctx := context.Background()
126+
client := openfeature.NewClient("example-client")
127+
128+
fmt.Println(client.Object(ctx, "myflag", map[string]string{"foo": "bar"}, openfeature.EvaluationContext{}))
129+
130+
// Output: map[foo:bar]
131+
}

0 commit comments

Comments
 (0)