Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions decode_hooks.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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