Skip to content

Commit

Permalink
Add tests for deep mapping of nested slices of structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ayratsa committed Nov 30, 2024
1 parent c607fb9 commit 4480298
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3368,6 +3368,49 @@ func testArrayInput(t *testing.T, input map[string]interface{}, expected *Array)
}
}

func TestDecode_structArrayDeepMap(t *testing.T) {
type SourceChild struct {
String string `mapstructure:"some-string"`
}

type SourceParent struct {
ChildrenA []SourceChild `mapstructure:"children-a,deep"`
ChildrenB *[]SourceChild `mapstructure:"children-b,deep"`
}

var target map[string]interface{}

source := SourceParent{
ChildrenA: []SourceChild{
{String: "one"},
{String: "two"},
},
ChildrenB: &[]SourceChild{
{String: "one"},
{String: "two"},
},
}

if err := Decode(source, &target); err != nil {
t.Fatalf("got error: %s", err)
}

expected := map[string]interface{}{
"children-a": []map[string]interface{}{
{"some-string": "one"},
{"some-string": "two"},
},
"children-b": []map[string]interface{}{
{"some-string": "one"},
{"some-string": "two"},
},
}

if !reflect.DeepEqual(target, expected) {
t.Fatalf("failed: \nexpected: %#v\nresult: %#v", expected, target)
}
}

func stringPtr(v string) *string { return &v }
func intPtr(v int) *int { return &v }
func uintPtr(v uint) *uint { return &v }
Expand Down

0 comments on commit 4480298

Please sign in to comment.