This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Description
When the target map to decode the result to is strongly typed, the decoding process fails, even if the map type matches the source structure.
func TestDecode_structToTypedMap(t *testing.T) {
type SourceChild struct {
String string `mapstructure:"string"`
}
type SourceParent struct {
Child SourceChild `mapstructure:"child"`
}
var target map[string]map[string]interface{} // This is the reason the test fails
//var target map[string]interface{} // With this variant it works
source := SourceParent{
Child: SourceChild{
String: "hello",
},
}
if err := Decode(source, &target); err != nil {
t.Fatalf("got error: %s", err)
}
expected := map[string]interface{}{
"child": map[string]interface{}{
"string": "hello",
},
}
if !reflect.DeepEqual(target, expected) {
t.Fatalf("bad: \nexpected: %#v\nresult: %#v", expected, target)
}
}
Produces:
=== RUN TestDecode_structToTypedMap
mapstructure_test.go:2884: got error: cannot assign type 'mapstructure.SourceChild' to map value field of type 'map[string]interface {}'
--- FAIL: TestDecode_structToTypedMap (0.00s)
As the target sutructure matches the needs of the decoding process, I would expect this to work.
mapstructure version bf980b3
go version go1.18.7 linux/amd64