From eb04f83f2633675ccf03e7222268326c00e68eaf Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 20 Sep 2024 16:25:21 +0800 Subject: [PATCH 1/2] fix: when 'data' is nil, panic occurs --- common/structure/structure.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/structure/structure.go b/common/structure/structure.go index 99c980bc4c..3f995ccb6b 100644 --- a/common/structure/structure.go +++ b/common/structure/structure.go @@ -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'", From 053e99a2d19bbcf8525d8b9515f73386a1c975df Mon Sep 17 00:00:00 2001 From: nico Date: Sun, 22 Sep 2024 06:37:06 +0800 Subject: [PATCH 2/2] add unit test --- common/structure/structure_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/common/structure/structure_test.go b/common/structure/structure_test.go index e5fe693d79..266b828fd4 100644 --- a/common/structure/structure_test.go +++ b/common/structure/structure_test.go @@ -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, "") +}