Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when 'data' is nil, panic occurs #1524

Open
wants to merge 2 commits into
base: Alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/structure/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down Expand Up @@ -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'",
Expand Down Expand Up @@ -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'",
Expand All @@ -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'",
Expand All @@ -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'",
Expand Down
18 changes: 18 additions & 0 deletions common/structure/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}