Skip to content

Commit f554876

Browse files
Craig Pastrobeeme1mr
andauthored
refactor: write [T]Value in terms of [T]ValueDetails (#224)
Signed-off-by: Craig Pastro <[email protected]> Co-authored-by: Michael Beemer <[email protected]>
1 parent e2b3586 commit f554876

File tree

1 file changed

+12
-88
lines changed

1 file changed

+12
-88
lines changed

pkg/openfeature/client.go

Lines changed: 12 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -292,30 +292,12 @@ func WithHookHints(hookHints HookHints) Option {
292292
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
293293
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
294294
func (c *Client) BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (bool, error) {
295-
c.mx.RLock()
296-
defer c.mx.RUnlock()
297-
298-
evalOptions := &EvaluationOptions{}
299-
for _, option := range options {
300-
option(evalOptions)
301-
}
302-
303-
evalDetails, err := c.evaluate(ctx, flag, Boolean, defaultValue, evalCtx, *evalOptions)
295+
details, err := c.BooleanValueDetails(ctx, flag, defaultValue, evalCtx, options...)
304296
if err != nil {
305297
return defaultValue, err
306298
}
307299

308-
value, ok := evalDetails.Value.(bool)
309-
if !ok {
310-
err := errors.New("evaluated value is not a boolean")
311-
c.logger().Error(
312-
err, "invalid flag resolution type", "expectedType", "bool",
313-
"gotType", fmt.Sprintf("%T", evalDetails.Value),
314-
)
315-
return defaultValue, err
316-
}
317-
318-
return value, nil
300+
return details.Value, nil
319301
}
320302

321303
// StringValue performs a flag evaluation that returns a string.
@@ -327,30 +309,12 @@ func (c *Client) BooleanValue(ctx context.Context, flag string, defaultValue boo
327309
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
328310
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
329311
func (c *Client) StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (string, error) {
330-
c.mx.RLock()
331-
defer c.mx.RUnlock()
332-
333-
evalOptions := &EvaluationOptions{}
334-
for _, option := range options {
335-
option(evalOptions)
336-
}
337-
338-
evalDetails, err := c.evaluate(ctx, flag, String, defaultValue, evalCtx, *evalOptions)
312+
details, err := c.StringValueDetails(ctx, flag, defaultValue, evalCtx, options...)
339313
if err != nil {
340314
return defaultValue, err
341315
}
342316

343-
value, ok := evalDetails.Value.(string)
344-
if !ok {
345-
err := errors.New("evaluated value is not a string")
346-
c.logger().Error(
347-
err, "invalid flag resolution type", "expectedType", "string",
348-
"gotType", fmt.Sprintf("%T", evalDetails.Value),
349-
)
350-
return defaultValue, err
351-
}
352-
353-
return value, nil
317+
return details.Value, nil
354318
}
355319

356320
// FloatValue performs a flag evaluation that returns a float64.
@@ -362,30 +326,12 @@ func (c *Client) StringValue(ctx context.Context, flag string, defaultValue stri
362326
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
363327
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
364328
func (c *Client) FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (float64, error) {
365-
c.mx.RLock()
366-
defer c.mx.RUnlock()
367-
368-
evalOptions := &EvaluationOptions{}
369-
for _, option := range options {
370-
option(evalOptions)
371-
}
372-
373-
evalDetails, err := c.evaluate(ctx, flag, Float, defaultValue, evalCtx, *evalOptions)
329+
details, err := c.FloatValueDetails(ctx, flag, defaultValue, evalCtx, options...)
374330
if err != nil {
375331
return defaultValue, err
376332
}
377333

378-
value, ok := evalDetails.Value.(float64)
379-
if !ok {
380-
err := errors.New("evaluated value is not a float64")
381-
c.logger().Error(
382-
err, "invalid flag resolution type", "expectedType", "float64",
383-
"gotType", fmt.Sprintf("%T", evalDetails.Value),
384-
)
385-
return defaultValue, err
386-
}
387-
388-
return value, nil
334+
return details.Value, nil
389335
}
390336

391337
// IntValue performs a flag evaluation that returns an int64.
@@ -397,30 +343,12 @@ func (c *Client) FloatValue(ctx context.Context, flag string, defaultValue float
397343
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
398344
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
399345
func (c *Client) IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (int64, error) {
400-
c.mx.RLock()
401-
defer c.mx.RUnlock()
402-
403-
evalOptions := &EvaluationOptions{}
404-
for _, option := range options {
405-
option(evalOptions)
406-
}
407-
408-
evalDetails, err := c.evaluate(ctx, flag, Int, defaultValue, evalCtx, *evalOptions)
346+
details, err := c.IntValueDetails(ctx, flag, defaultValue, evalCtx, options...)
409347
if err != nil {
410348
return defaultValue, err
411349
}
412350

413-
value, ok := evalDetails.Value.(int64)
414-
if !ok {
415-
err := errors.New("evaluated value is not an int64")
416-
c.logger().Error(
417-
err, "invalid flag resolution type", "expectedType", "int64",
418-
"gotType", fmt.Sprintf("%T", evalDetails.Value),
419-
)
420-
return defaultValue, err
421-
}
422-
423-
return value, nil
351+
return details.Value, nil
424352
}
425353

426354
// ObjectValue performs a flag evaluation that returns an object.
@@ -432,16 +360,12 @@ func (c *Client) IntValue(ctx context.Context, flag string, defaultValue int64,
432360
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
433361
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
434362
func (c *Client) ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (interface{}, error) {
435-
c.mx.RLock()
436-
defer c.mx.RUnlock()
437-
438-
evalOptions := &EvaluationOptions{}
439-
for _, option := range options {
440-
option(evalOptions)
363+
details, err := c.ObjectValueDetails(ctx, flag, defaultValue, evalCtx, options...)
364+
if err != nil {
365+
return defaultValue, err
441366
}
442367

443-
evalDetails, err := c.evaluate(ctx, flag, Object, defaultValue, evalCtx, *evalOptions)
444-
return evalDetails.Value, err
368+
return details.Value, nil
445369
}
446370

447371
// BooleanValueDetails performs a flag evaluation that returns an evaluation details struct.

0 commit comments

Comments
 (0)