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 possible panic when using ComposeDecodeHookFunc() with no funcs #251

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions decode_hooks.go
Expand Up @@ -63,6 +63,11 @@ func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
return func(f reflect.Value, t reflect.Value) (interface{}, error) {
var err error
var data interface{}

if len(fs) == 0 {
data = f.Interface()
}

newFrom := f
for _, f1 := range fs {
data, err = DecodeHookExec(f1, newFrom, t)
Expand Down
32 changes: 32 additions & 0 deletions decode_hooks_test.go
Expand Up @@ -84,6 +84,38 @@ func TestComposeDecodeHookFunc_kinds(t *testing.T) {
}
}

func TestComposeDecodeHookFunc_safe_nofuncs(t *testing.T) {
f := ComposeDecodeHookFunc()
type myStruct2 struct {
MyInt int
}

type myStruct1 struct {
Blah map[string]myStruct2
}

src := &myStruct1{Blah: map[string]myStruct2{
"test": {
MyInt: 1,
},
}}

dst := &myStruct1{}
dConf := &DecoderConfig{
Result: dst,
ErrorUnused: true,
DecodeHook: f,
}
d, err := NewDecoder(dConf)
if err != nil {
t.Fatal(err)
}
err = d.Decode(src)
if err != nil {
t.Fatal(err)
}
}

func TestStringToSliceHookFunc(t *testing.T) {
f := StringToSliceHookFunc(",")

Expand Down