mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
parent
408827d876
commit
44d347d48a
@ -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.
|
||||
overflowValidator := value
|
||||
if overflowValidator.Type().Kind() == reflect.Ptr {
|
||||
overflowValidator = overflowValidator.Elem()
|
||||
derefedValue := value
|
||||
for derefedValue.Type().Kind() == reflect.Ptr {
|
||||
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())
|
||||
}
|
||||
|
||||
|
@ -1343,16 +1343,31 @@ func TestUnmarshalNullableSlice(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnmarshalWithFloatPtr(t *testing.T) {
|
||||
var v struct {
|
||||
WeightFloat32 *float32 `key:"weightFloat32,optional"`
|
||||
}
|
||||
m := map[string]any{
|
||||
"weightFloat32": json.Number("3.2"),
|
||||
}
|
||||
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)
|
||||
}
|
||||
if assert.NoError(t, UnmarshalKey(m, &v)) {
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user