This commit is contained in:
落心 2024-11-14 23:47:02 +08:00 committed by GitHub
commit f2c576ff11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View File

@ -170,6 +170,8 @@ func (d *Decoder) decodeInt(name string, data any, val reflect.Value) (err error
} else {
err = fmt.Errorf("cannot parse '%s' as int: %s", name, err)
}
case data == nil:
val.SetInt(0)
default:
err = fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
@ -197,6 +199,8 @@ func (d *Decoder) decodeUint(name string, data any, val reflect.Value) (err erro
} else {
err = fmt.Errorf("cannot parse '%s' as int: %s", name, err)
}
case data == nil:
val.SetUint(0)
default:
err = fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
@ -224,6 +228,8 @@ func (d *Decoder) decodeFloat(name string, data any, val reflect.Value) (err err
} else {
err = fmt.Errorf("cannot parse '%s' as int: %s", name, err)
}
case data == nil:
val.SetFloat(0)
default:
err = fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
@ -245,6 +251,8 @@ func (d *Decoder) decodeString(name string, data any, val reflect.Value) (err er
val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
case isFloat(kind) && d.option.WeaklyTypedInput:
val.SetString(strconv.FormatFloat(dataVal.Float(), 'E', -1, dataVal.Type().Bits()))
case data == nil:
val.SetString("")
default:
err = fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
@ -264,6 +272,8 @@ func (d *Decoder) decodeBool(name string, data any, val reflect.Value) (err erro
val.SetBool(dataVal.Int() != 0)
case isUint(kind) && d.option.WeaklyTypedInput:
val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
case data == nil:
val.SetBool(false)
default:
err = fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",

View File

@ -267,3 +267,21 @@ func TestStructure_TextUnmarshaller(t *testing.T) {
err = decoder.Decode(rawMap, s)
assert.NotNilf(t, err, "should throw error: %#v", s)
}
func TestStructure_Null(t *testing.T) {
rawMap := map[string]any{
"opt": map[string]any{
"bar": nil,
},
}
s := struct {
Opt struct {
Bar string `test:"bar,optional"`
} `test:"opt,optional"`
}{}
err := decoder.Decode(rawMap, &s)
assert.Nil(t, err)
assert.Equal(t, s.Opt.Bar, "")
}