Skip to content

Commit 4077d5a

Browse files
authored
fix: IntEvalution, FloatEvalution when disabled or missing flags (#610)
Signed-off-by: Seunghun Lee <[email protected]>
1 parent 353dbb1 commit 4077d5a

File tree

3 files changed

+90
-18
lines changed

3 files changed

+90
-18
lines changed

providers/unleash/pkg/demo_app_toggles.json

+17-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
],
124124
"variants": [
125125
{
126-
"name": "aaaa",
126+
"name": "int-flag-variant",
127127
"weight": 1000,
128128
"payload": {
129129
"type": "number",
@@ -148,7 +148,7 @@
148148
],
149149
"variants": [
150150
{
151-
"name": "aaaa",
151+
"name": "double-flag-variant",
152152
"weight": 1000,
153153
"payload": {
154154
"type": "number",
@@ -160,6 +160,21 @@
160160
}
161161
]
162162
},
163+
{
164+
"name": "disabled-flag",
165+
"type": "release",
166+
"enabled": false,
167+
"variants": [
168+
{
169+
"name": "disabled-flag-variant",
170+
"weight": 1000,
171+
"payload": {
172+
"type": "string",
173+
"value": "disabled flag variant value"
174+
}
175+
}
176+
]
177+
},
163178
{
164179
"name": "DateExample",
165180
"type": "release",

providers/unleash/pkg/provider.go

+16
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ func (p *Provider) BooleanEvaluation(ctx context.Context, flag string, defaultVa
107107

108108
func (p *Provider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx of.FlattenedContext) of.FloatResolutionDetail {
109109
res := p.ObjectEvaluation(ctx, flag, defaultValue, evalCtx)
110+
111+
if value, ok := res.Value.(float64); ok {
112+
return of.FloatResolutionDetail{
113+
Value: value,
114+
ProviderResolutionDetail: res.ProviderResolutionDetail,
115+
}
116+
}
117+
110118
if strValue, ok := res.Value.(string); ok {
111119
value, err := strconv.ParseFloat(strValue, 64)
112120
if err == nil {
@@ -127,6 +135,14 @@ func (p *Provider) FloatEvaluation(ctx context.Context, flag string, defaultValu
127135

128136
func (p *Provider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx of.FlattenedContext) of.IntResolutionDetail {
129137
res := p.ObjectEvaluation(ctx, flag, defaultValue, evalCtx)
138+
139+
if value, ok := res.Value.(int64); ok {
140+
return of.IntResolutionDetail{
141+
Value: value,
142+
ProviderResolutionDetail: res.ProviderResolutionDetail,
143+
}
144+
}
145+
130146
if strValue, ok := res.Value.(string); ok {
131147
value, err := strconv.ParseInt(strValue, 10, 64)
132148
if err == nil {

providers/unleash/pkg/provider_test.go

+57-16
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,63 @@ func TestBooleanEvaluation(t *testing.T) {
4040

4141
func TestIntEvaluation(t *testing.T) {
4242
defaultValue := int64(0)
43-
expectedValue := int64(123)
44-
resolution := provider.IntEvaluation(context.Background(), "int-flag", defaultValue, nil)
45-
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
46-
if !enabled {
47-
t.Fatalf("Expected feature to be enabled")
48-
}
49-
if resolution.ProviderResolutionDetail.Variant != "aaaa" {
50-
t.Fatalf("Expected variant name")
51-
}
52-
if resolution.Value != expectedValue {
53-
t.Fatalf("Expected one of the variant payloads")
54-
}
5543

56-
t.Run("evalCtx empty", func(t *testing.T) {
57-
resolution := provider.IntEvaluation(context.Background(), "non-existing-flag", defaultValue, nil)
44+
t.Run("int-flag", func(t *testing.T) {
45+
resolution := provider.IntEvaluation(context.Background(), "int-flag", defaultValue, nil)
46+
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
47+
require.True(t, enabled)
48+
require.Equal(t, "int-flag-variant", resolution.ProviderResolutionDetail.Variant)
49+
require.Equal(t, int64(123), resolution.Value)
50+
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
51+
})
52+
53+
t.Run("disabled-flag", func(t *testing.T) {
54+
resolution := provider.IntEvaluation(context.Background(), "disabled-flag", defaultValue, nil)
55+
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
56+
require.False(t, enabled)
57+
require.Equal(t, "", resolution.ProviderResolutionDetail.Variant)
5858
require.Equal(t, defaultValue, resolution.Value)
59+
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
5960
})
6061

61-
t.Run("evalCtx empty fallback to default", func(t *testing.T) {
62+
t.Run("non-existing-flag", func(t *testing.T) {
6263
resolution := provider.IntEvaluation(context.Background(), "non-existing-flag", defaultValue, nil)
64+
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
65+
require.False(t, enabled)
66+
require.Equal(t, "", resolution.ProviderResolutionDetail.Variant)
67+
require.Equal(t, defaultValue, resolution.Value)
68+
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
69+
})
70+
}
71+
72+
func TestFloatEvaluation(t *testing.T) {
73+
defaultValue := 0.0
74+
75+
t.Run("int-flag", func(t *testing.T) {
76+
resolution := provider.FloatEvaluation(context.Background(), "double-flag", defaultValue, nil)
77+
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
78+
require.True(t, enabled)
79+
require.Equal(t, "double-flag-variant", resolution.ProviderResolutionDetail.Variant)
80+
require.Equal(t, 1.23, resolution.Value)
81+
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
82+
})
83+
84+
t.Run("disabled-flag", func(t *testing.T) {
85+
resolution := provider.FloatEvaluation(context.Background(), "disabled-flag", defaultValue, nil)
86+
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
87+
require.False(t, enabled)
88+
require.Equal(t, "", resolution.ProviderResolutionDetail.Variant)
89+
require.Equal(t, defaultValue, resolution.Value)
90+
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
91+
})
92+
93+
t.Run("non-existing-flag", func(t *testing.T) {
94+
resolution := provider.FloatEvaluation(context.Background(), "non-existing-flag", defaultValue, nil)
95+
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
96+
require.False(t, enabled)
97+
require.Equal(t, "", resolution.ProviderResolutionDetail.Variant)
6398
require.Equal(t, defaultValue, resolution.Value)
99+
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
64100
})
65101
}
66102

@@ -228,13 +264,17 @@ func TestMain(m *testing.M) {
228264
}
229265
defer demoReader.Close()
230266

267+
appName := "my-application"
268+
backupFile := fmt.Sprintf("unleash-repo-schema-v1-%s.json", appName)
269+
231270
providerOptions := unleashProvider.ProviderConfig{
232271
Options: []unleash.ConfigOption{
233272
unleash.WithListener(&unleash.DebugListener{}),
234-
unleash.WithAppName("my-application"),
273+
unleash.WithAppName(appName),
235274
unleash.WithRefreshInterval(5 * time.Second),
236275
unleash.WithMetricsInterval(5 * time.Second),
237276
unleash.WithStorage(&unleash.BootstrapStorage{Reader: demoReader}),
277+
unleash.WithBackupPath("./"),
238278
unleash.WithUrl("https://localhost:4242"),
239279
},
240280
}
@@ -258,5 +298,6 @@ func TestMain(m *testing.M) {
258298

259299
cleanup()
260300

301+
os.Remove(backupFile)
261302
os.Exit(exitCode)
262303
}

0 commit comments

Comments
 (0)