Skip to content

Commit 945bd96

Browse files
authored
fix!: remove duplicate Value field from ResolutionDetail structs (#58)
Signed-off-by: Skye Gill <[email protected]>
1 parent d05691c commit 945bd96

File tree

6 files changed

+34
-94
lines changed

6 files changed

+34
-94
lines changed

pkg/openfeature/client.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ var typeToString = map[Type]string{
109109
type EvaluationDetails struct {
110110
FlagKey string
111111
FlagType Type
112-
ResolutionDetail
112+
InterfaceResolutionDetail
113113
}
114114

115115
// BooleanValue return boolean evaluation for flag
@@ -245,7 +245,7 @@ func (c Client) evaluate(
245245
evalDetails := EvaluationDetails{
246246
FlagKey: flag,
247247
FlagType: flagType,
248-
ResolutionDetail: ResolutionDetail{
248+
InterfaceResolutionDetail: InterfaceResolutionDetail{
249249
Value: defaultValue,
250250
},
251251
}
@@ -269,26 +269,30 @@ func (c Client) evaluate(
269269
}
270270

271271
flatCtx := flattenContext(evalCtx)
272-
var resolution ResolutionDetail
272+
var resolution InterfaceResolutionDetail
273273
switch flagType {
274274
case Object:
275275
resolution = api.provider.ObjectEvaluation(flag, defaultValue, flatCtx)
276276
case Boolean:
277277
defValue := defaultValue.(bool)
278278
res := api.provider.BooleanEvaluation(flag, defValue, flatCtx)
279-
resolution = res.ResolutionDetail
279+
resolution.ResolutionDetail = res.ResolutionDetail
280+
resolution.Value = res.Value
280281
case String:
281282
defValue := defaultValue.(string)
282283
res := api.provider.StringEvaluation(flag, defValue, flatCtx)
283-
resolution = res.ResolutionDetail
284+
resolution.ResolutionDetail = res.ResolutionDetail
285+
resolution.Value = res.Value
284286
case Float:
285287
defValue := defaultValue.(float64)
286288
res := api.provider.FloatEvaluation(flag, defValue, flatCtx)
287-
resolution = res.ResolutionDetail
289+
resolution.ResolutionDetail = res.ResolutionDetail
290+
resolution.Value = res.Value
288291
case Int:
289292
defValue := defaultValue.(int64)
290293
res := api.provider.IntEvaluation(flag, defValue, flatCtx)
291-
resolution = res.ResolutionDetail
294+
resolution.ResolutionDetail = res.ResolutionDetail
295+
resolution.Value = res.Value
292296
}
293297

294298
err = resolution.Error()
@@ -302,7 +306,7 @@ func (c Client) evaluate(
302306
return evalDetails, err
303307
}
304308
if resolution.Value != nil {
305-
evalDetails.ResolutionDetail = resolution
309+
evalDetails.InterfaceResolutionDetail = resolution
306310
}
307311

308312
if err := c.afterHooks(hookCtx, providerInvocationClientApiHooks, evalDetails, options); err != nil {

pkg/openfeature/client_test.go

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ func TestRequirement_1_4_9(t *testing.T) {
224224
Return(BoolResolutionDetail{
225225
Value: false,
226226
ResolutionDetail: ResolutionDetail{
227-
Value: false,
228227
ErrorCode: "GENERAL",
229228
Reason: "forced test error",
230229
},
@@ -260,7 +259,6 @@ func TestRequirement_1_4_9(t *testing.T) {
260259
Return(StringResolutionDetail{
261260
Value: "foo",
262261
ResolutionDetail: ResolutionDetail{
263-
Value: "foo",
264262
ErrorCode: "GENERAL",
265263
Reason: "forced test error",
266264
},
@@ -296,7 +294,6 @@ func TestRequirement_1_4_9(t *testing.T) {
296294
Return(FloatResolutionDetail{
297295
Value: 0,
298296
ResolutionDetail: ResolutionDetail{
299-
Value: 0,
300297
ErrorCode: "GENERAL",
301298
Reason: "forced test error",
302299
},
@@ -332,7 +329,6 @@ func TestRequirement_1_4_9(t *testing.T) {
332329
Return(IntResolutionDetail{
333330
Value: 0,
334331
ResolutionDetail: ResolutionDetail{
335-
Value: 0,
336332
ErrorCode: "GENERAL",
337333
Reason: "forced test error",
338334
},
@@ -368,10 +364,11 @@ func TestRequirement_1_4_9(t *testing.T) {
368364
mockProvider.EXPECT().Metadata().AnyTimes()
369365
mockProvider.EXPECT().Hooks().AnyTimes()
370366
mockProvider.EXPECT().ObjectEvaluation(flagKey, defaultValue, flatCtx).
371-
Return(ResolutionDetail{
372-
Value: obj{foo: "foo"},
373-
ErrorCode: "GENERAL",
374-
Reason: "forced test error",
367+
Return(InterfaceResolutionDetail{
368+
ResolutionDetail: ResolutionDetail{
369+
ErrorCode: "GENERAL",
370+
Reason: "forced test error",
371+
},
375372
}).Times(2)
376373
SetProvider(mockProvider)
377374

@@ -413,74 +410,6 @@ func TestRequirement_1_4_9(t *testing.T) {
413410
// The `client` SHOULD transform the `evaluation context` using the `provider's` `context transformer` function
414411
// if one is defined, before passing the result of the transformation to the provider's flag resolution functions.
415412

416-
func TestClient_ProviderEvaluationReturnsUnexpectedType(t *testing.T) {
417-
client := NewClient("test-client")
418-
419-
t.Run("Boolean", func(t *testing.T) {
420-
defer t.Cleanup(initSingleton)
421-
ctrl := gomock.NewController(t)
422-
mockProvider := NewMockFeatureProvider(ctrl)
423-
mockProvider.EXPECT().Metadata().AnyTimes()
424-
SetProvider(mockProvider)
425-
mockProvider.EXPECT().Hooks().AnyTimes()
426-
mockProvider.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any()).
427-
Return(BoolResolutionDetail{ResolutionDetail: ResolutionDetail{Value: 3}})
428-
429-
_, err := client.BooleanValue("", false, EvaluationContext{}, EvaluationOptions{})
430-
if err == nil {
431-
t.Error("expected BooleanValue to return an error, got nil")
432-
}
433-
})
434-
435-
t.Run("String", func(t *testing.T) {
436-
defer t.Cleanup(initSingleton)
437-
ctrl := gomock.NewController(t)
438-
mockProvider := NewMockFeatureProvider(ctrl)
439-
mockProvider.EXPECT().Metadata().AnyTimes()
440-
SetProvider(mockProvider)
441-
mockProvider.EXPECT().Hooks().AnyTimes()
442-
mockProvider.EXPECT().StringEvaluation(gomock.Any(), gomock.Any(), gomock.Any()).
443-
Return(StringResolutionDetail{ResolutionDetail: ResolutionDetail{Value: 3}})
444-
445-
_, err := client.StringValue("", "", EvaluationContext{}, EvaluationOptions{})
446-
if err == nil {
447-
t.Error("expected StringValue to return an error, got nil")
448-
}
449-
})
450-
451-
t.Run("Float", func(t *testing.T) {
452-
defer t.Cleanup(initSingleton)
453-
ctrl := gomock.NewController(t)
454-
mockProvider := NewMockFeatureProvider(ctrl)
455-
mockProvider.EXPECT().Metadata().AnyTimes()
456-
SetProvider(mockProvider)
457-
mockProvider.EXPECT().Hooks().AnyTimes()
458-
mockProvider.EXPECT().FloatEvaluation(gomock.Any(), gomock.Any(), gomock.Any()).
459-
Return(FloatResolutionDetail{ResolutionDetail: ResolutionDetail{Value: false}})
460-
461-
_, err := client.FloatValue("", 3, EvaluationContext{}, EvaluationOptions{})
462-
if err == nil {
463-
t.Error("expected FloatValue to return an error, got nil")
464-
}
465-
})
466-
467-
t.Run("Int", func(t *testing.T) {
468-
defer t.Cleanup(initSingleton)
469-
ctrl := gomock.NewController(t)
470-
mockProvider := NewMockFeatureProvider(ctrl)
471-
mockProvider.EXPECT().Metadata().AnyTimes()
472-
SetProvider(mockProvider)
473-
mockProvider.EXPECT().Hooks().AnyTimes()
474-
mockProvider.EXPECT().IntEvaluation(gomock.Any(), gomock.Any(), gomock.Any()).
475-
Return(IntResolutionDetail{ResolutionDetail: ResolutionDetail{Value: false}})
476-
477-
_, err := client.IntValue("", 3, EvaluationContext{}, EvaluationOptions{})
478-
if err == nil {
479-
t.Error("expected IntValue to return an error, got nil")
480-
}
481-
})
482-
}
483-
484413
func TestFlattenContext(t *testing.T) {
485414
tests := map[string]struct {
486415
inCtx EvaluationContext

pkg/openfeature/noop_provider.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ func (e NoopProvider) IntEvaluation(flag string, defaultValue int64, evalCtx map
5353
}
5454

5555
// ObjectEvaluation returns an object flag
56-
func (e NoopProvider) ObjectEvaluation(flag string, defaultValue interface{}, evalCtx map[string]interface{}) ResolutionDetail {
57-
return ResolutionDetail{
58-
Value: defaultValue,
59-
Variant: "default-variant",
60-
Reason: DEFAULT,
56+
func (e NoopProvider) ObjectEvaluation(flag string, defaultValue interface{}, evalCtx map[string]interface{}) InterfaceResolutionDetail {
57+
return InterfaceResolutionDetail{
58+
Value: defaultValue,
59+
ResolutionDetail: ResolutionDetail{
60+
Variant: "default-variant",
61+
Reason: DEFAULT,
62+
},
6163
}
6264
}
6365

pkg/openfeature/provider.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type FeatureProvider interface {
2020
StringEvaluation(flag string, defaultValue string, evalCtx map[string]interface{}) StringResolutionDetail
2121
FloatEvaluation(flag string, defaultValue float64, evalCtx map[string]interface{}) FloatResolutionDetail
2222
IntEvaluation(flag string, defaultValue int64, evalCtx map[string]interface{}) IntResolutionDetail
23-
ObjectEvaluation(flag string, defaultValue interface{}, evalCtx map[string]interface{}) ResolutionDetail
23+
ObjectEvaluation(flag string, defaultValue interface{}, evalCtx map[string]interface{}) InterfaceResolutionDetail
2424
Hooks() []Hook
2525
}
2626

@@ -30,7 +30,6 @@ type FeatureProvider interface {
3030
// N.B we could use generics but to support older versions of golang for now we will have type specific resolution
3131
// detail
3232
type ResolutionDetail struct {
33-
Value interface{}
3433
ErrorCode string
3534
Reason string
3635
Variant string
@@ -67,6 +66,12 @@ type IntResolutionDetail struct {
6766
ResolutionDetail
6867
}
6968

69+
// InterfaceResolutionDetail provides a resolution detail with interface{} type
70+
type InterfaceResolutionDetail struct {
71+
Value interface{}
72+
ResolutionDetail
73+
}
74+
7075
// Metadata provides provider name
7176
type Metadata struct {
7277
Name string

pkg/openfeature/provider_mock_test.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/openfeature/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestRequirement_2_2(t *testing.T) {
4545
StringEvaluation(flag string, defaultValue string, evalCtx map[string]interface{}) StringResolutionDetail
4646
FloatEvaluation(flag string, defaultValue float64, evalCtx map[string]interface{}) FloatResolutionDetail
4747
IntEvaluation(flag string, defaultValue int64, evalCtx map[string]interface{}) IntResolutionDetail
48-
ObjectEvaluation(flag string, defaultValue interface{}, evalCtx map[string]interface{}) ResolutionDetail
48+
ObjectEvaluation(flag string, defaultValue interface{}, evalCtx map[string]interface{}) InterfaceResolutionDetail
4949
}
5050

5151
var mockProviderI interface{} = mockProvider

0 commit comments

Comments
 (0)