Skip to content

Commit db417f9

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

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

pkg/values/value.go

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ func Wrap(v any) (Value, error) {
4646
return NewList(tv)
4747
case decimal.Decimal:
4848
return NewDecimal(tv), nil
49+
case float64:
50+
return NewFloat64(tv), nil
51+
case float32:
52+
return NewFloat64(float64(tv)), nil
4953
case int64:
5054
return NewInt64(tv), nil
5155
case int32:
@@ -87,6 +91,8 @@ func Wrap(v any) (Value, error) {
8791
return tv, nil
8892
case *Decimal:
8993
return tv, nil
94+
case *Float64:
95+
return tv, nil
9096
case *Int64:
9197
return tv, nil
9298
case *Float64:
@@ -141,6 +147,8 @@ func Wrap(v any) (Value, error) {
141147

142148
case reflect.Bool:
143149
return Wrap(val.Convert(reflect.TypeOf(true)).Interface())
150+
case reflect.Float32, reflect.Float64:
151+
return Wrap(val.Convert(reflect.TypeOf(float64(0))).Interface())
144152
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
145153
return Wrap(val.Convert(reflect.TypeOf(int64(0))).Interface())
146154
case reflect.Float32, reflect.Float64:

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)