Skip to content

Commit 82c4ea3

Browse files
committed
fix(values): wraps float64 and float32 types
1 parent eb2be16 commit 82c4ea3

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

pkg/values/value.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ func Wrap(v any) (Value, error) {
8787
return tv, nil
8888
case *Decimal:
8989
return tv, nil
90-
case *Int64:
91-
return tv, nil
9290
case *Float64:
9391
return tv, nil
92+
case *Int64:
93+
return tv, nil
9494
}
9595

9696
// Handle slices, structs, and pointers to structs
@@ -141,10 +141,10 @@ func Wrap(v any) (Value, error) {
141141

142142
case reflect.Bool:
143143
return Wrap(val.Convert(reflect.TypeOf(true)).Interface())
144-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
145-
return Wrap(val.Convert(reflect.TypeOf(int64(0))).Interface())
146144
case reflect.Float32, reflect.Float64:
147145
return Wrap(val.Convert(reflect.TypeOf(float64(0))).Interface())
146+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
147+
return Wrap(val.Convert(reflect.TypeOf(int64(0))).Interface())
148148
}
149149

150150
return nil, fmt.Errorf("could not wrap into value: %+v", v)

pkg/values/value_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@ func Test_IntTypes(t *testing.T) {
281281
}
282282
}
283283

284+
func Test_FloatTypes(t *testing.T) {
285+
testCases := []struct {
286+
name string
287+
test func(tt *testing.T)
288+
}{
289+
{name: "wrap float64 as float64", test: func(tt *testing.T) { wrappableTest[float64, float64](tt, float64(100.01)) }},
290+
{name: "wrap float32 as float64", test: func(tt *testing.T) { wrappableTest[float32, float64](tt, float32(100.01)) }},
291+
}
292+
293+
for _, tc := range testCases {
294+
t.Run(tc.name, func(st *testing.T) {
295+
tc.test(st)
296+
})
297+
}
298+
}
299+
284300
func Test_StructWrapUnwrap(t *testing.T) {
285301
// TODO: https://smartcontract-it.atlassian.net/browse/KS-439 decimal.Decimal is broken when encoded.
286302
type sStruct struct {

0 commit comments

Comments
 (0)