Skip to content
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
2 changes: 1 addition & 1 deletion decode_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func WeaklyTypedHook(
func RecursiveStructToMapHookFunc() DecodeHookFunc {
return func(f reflect.Value, t reflect.Value) (any, error) {
if f.Kind() != reflect.Struct {
return f.Interface(), nil
return safeInterface(f), nil
}

var i any = struct{}{}
Expand Down
43 changes: 43 additions & 0 deletions decode_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,49 @@ func TestWeaklyTypedHook(t *testing.T) {
}
}

func TestRecursiveStructToMapHookFunc_nil(t *testing.T) {
cases := []struct {
name string
hook DecodeHookFunc
}{
{"standalone", RecursiveStructToMapHookFunc()},
{"composed", ComposeDecodeHookFunc(
StringToTimeDurationHookFunc(),
RecursiveStructToMapHookFunc(),
)},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
input := map[string]any{
"nil": nil,
"nested": struct {
Value string
}{Value: "hello"},
}
var result map[string]any
decoder, err := NewDecoder(&DecoderConfig{
DecodeHook: tc.hook,
DecodeNil: true,
Result: &result,
})
if err != nil {
t.Fatal(err)
}
if err := decoder.Decode(input); err != nil {
t.Fatal(err)
}
want := map[string]any{
"nil": nil,
"nested": map[string]any{"Value": "hello"},
}
if !reflect.DeepEqual(result, want) {
t.Fatalf("expected %#v, got %#v", want, result)
}
})
}
}

func TestStructToMapHookFuncTabled(t *testing.T) {
var f DecodeHookFunc = RecursiveStructToMapHookFunc()

Expand Down