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
11 changes: 7 additions & 4 deletions mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1259,11 +1259,14 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
case reflect.Slice:
if deep {
var childType reflect.Type
switch v.Type().Elem().Kind() {
case reflect.Struct:
elemType := v.Type().Elem()
// Recurse into slices of structs and slices of pointers to
// structs alike, so []*T maps the same way []T does.
if elemType.Kind() == reflect.Struct ||
(elemType.Kind() == reflect.Ptr && elemType.Elem().Kind() == reflect.Struct) {
childType = reflect.TypeOf(map[string]any{})
default:
childType = v.Type().Elem()
} else {
childType = elemType
}

sType := reflect.SliceOf(childType)
Expand Down
9 changes: 9 additions & 0 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3865,6 +3865,7 @@ func TestDecode_structArrayDeepMap(t *testing.T) {
type SourceParent struct {
ChildrenA []SourceChild `mapstructure:"children-a,deep"`
ChildrenB *[]SourceChild `mapstructure:"children-b,deep"`
ChildrenC []*SourceChild `mapstructure:"children-c,deep"`
}

var target map[string]any
Expand All @@ -3878,6 +3879,10 @@ func TestDecode_structArrayDeepMap(t *testing.T) {
{String: "one"},
{String: "two"},
},
ChildrenC: []*SourceChild{
{String: "one"},
{String: "two"},
},
}

if err := Decode(source, &target); err != nil {
Expand All @@ -3893,6 +3898,10 @@ func TestDecode_structArrayDeepMap(t *testing.T) {
{"some-string": "one"},
{"some-string": "two"},
},
"children-c": []map[string]any{
{"some-string": "one"},
{"some-string": "two"},
},
}

if !reflect.DeepEqual(target, expected) {
Expand Down