mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
Merge 053e99a2d1
into de19f927e8
This commit is contained in:
commit
f2c576ff11
|
@ -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'",
|
||||
|
|
|
@ -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, "")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user