master
Kevin Wan 10 months ago committed by GitHub
parent 408827d876
commit 44d347d48a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -626,11 +626,11 @@ func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(fieldType reflect.Type
} }
// if value is a pointer, we need to check overflow with the pointer's value. // if value is a pointer, we need to check overflow with the pointer's value.
overflowValidator := value derefedValue := value
if overflowValidator.Type().Kind() == reflect.Ptr { for derefedValue.Type().Kind() == reflect.Ptr {
overflowValidator = overflowValidator.Elem() derefedValue = derefedValue.Elem()
} }
if overflowValidator.OverflowFloat(fValue) { if derefedValue.CanFloat() && derefedValue.OverflowFloat(fValue) {
return fmt.Errorf("parsing %q as float32: value out of range", v.String()) return fmt.Errorf("parsing %q as float32: value out of range", v.String())
} }

@ -1343,16 +1343,31 @@ func TestUnmarshalNullableSlice(t *testing.T) {
} }
func TestUnmarshalWithFloatPtr(t *testing.T) { func TestUnmarshalWithFloatPtr(t *testing.T) {
var v struct { t.Run("*float32", func(t *testing.T) {
WeightFloat32 *float32 `key:"weightFloat32,optional"` var v struct {
} WeightFloat32 *float32 `key:"weightFloat32,optional"`
m := map[string]any{ }
"weightFloat32": json.Number("3.2"), m := map[string]any{
} "weightFloat32": json.Number("3.2"),
}
if assert.NoError(t, UnmarshalKey(m, &v)) { if assert.NoError(t, UnmarshalKey(m, &v)) {
assert.Equal(t, float32(3.2), *v.WeightFloat32) assert.Equal(t, float32(3.2), *v.WeightFloat32)
} }
})
t.Run("**float32", func(t *testing.T) {
var v struct {
WeightFloat32 **float32 `key:"weightFloat32,optional"`
}
m := map[string]any{
"weightFloat32": json.Number("3.2"),
}
if assert.NoError(t, UnmarshalKey(m, &v)) {
assert.Equal(t, float32(3.2), **v.WeightFloat32)
}
})
} }
func TestUnmarshalIntSlice(t *testing.T) { func TestUnmarshalIntSlice(t *testing.T) {

Loading…
Cancel
Save