Skip to content

Commit caeea08

Browse files
Merge pull request #1 from erka/rd/memprovider-type-mismatch
fix(memprovider): add type conversion for int8/int16/int32/float32 va…
2 parents 8d02105 + 3b558ef commit caeea08

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

openfeature/memprovider/in_memory_provider.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,26 +160,42 @@ func (i InMemoryProvider) find(flag string) (*InMemoryFlag, *openfeature.Provide
160160
// It coerces smaller numeric types to their canonical forms (int* -> int64, float32 -> float64)
161161
// to provide a more forgiving API for test flag configuration.
162162
func genericResolve[T comparable](value any, defaultValue T, detail *openfeature.ProviderResolutionDetail) T {
163-
// Coerce smaller numeric types to canonical forms
164-
switch v := value.(type) {
165-
case int:
166-
value = int64(v)
167-
case int8:
168-
value = int64(v)
169-
case int16:
170-
value = int64(v)
171-
case int32:
172-
value = int64(v)
173-
case float32:
174-
value = float64(v)
175-
}
176-
177-
v, ok := value.(T)
178-
179-
if ok {
163+
// Try direct type assertion first
164+
if v, ok := value.(T); ok {
180165
return v
181166
}
182167

168+
// Handle type conversions based on target type
169+
switch any(defaultValue).(type) {
170+
case int64:
171+
// Convert various int types to int64
172+
switch v := value.(type) {
173+
case int8:
174+
return any(int64(v)).(T)
175+
case int16:
176+
return any(int64(v)).(T)
177+
case int32:
178+
return any(int64(v)).(T)
179+
case int:
180+
return any(int64(v)).(T)
181+
}
182+
case float64:
183+
// Convert float32 to float64 and int types to float64
184+
switch v := value.(type) {
185+
case float32:
186+
return any(float64(v)).(T)
187+
case int8:
188+
return any(float64(v)).(T)
189+
case int16:
190+
return any(float64(v)).(T)
191+
case int32:
192+
return any(float64(v)).(T)
193+
case int:
194+
return any(float64(v)).(T)
195+
}
196+
}
197+
198+
// If no conversion worked, return error
183199
detail.Reason = openfeature.ErrorReason
184200
detail.ResolutionError = openfeature.NewTypeMismatchResolutionError("incorrect type association")
185201
return defaultValue

openfeature/memprovider/in_memory_provider_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ func TestInMemoryProvider_Float(t *testing.T) {
6969
},
7070
ContextEvaluator: nil,
7171
},
72+
"float32Flag": {
73+
Key: "float32Flag",
74+
State: Enabled,
75+
DefaultVariant: "fOne",
76+
Variants: map[string]any{
77+
"fOne": float32(3.5),
78+
"fTwo": float32(4.5),
79+
},
80+
ContextEvaluator: nil,
81+
},
7282
})
7383

7484
ctx := t.Context()
@@ -80,6 +90,14 @@ func TestInMemoryProvider_Float(t *testing.T) {
8090
t.Errorf("incorrect evaluation, expected %f, got %f", 1.1, evaluation.Value)
8191
}
8292
})
93+
94+
t.Run("test float32 conversion success", func(t *testing.T) {
95+
evaluation := memoryProvider.FloatEvaluation(ctx, "float32Flag", 1.0, nil)
96+
expected := 3.5
97+
if evaluation.Value != expected {
98+
t.Errorf("incorrect evaluation, expected %f, got %f", expected, evaluation.Value)
99+
}
100+
})
83101
}
84102

85103
func TestInMemoryProvider_Int(t *testing.T) {

0 commit comments

Comments
 (0)