Nested map[string]interface{} not decoded
Author: bepCreated May 20, 2018Updated May 30, 2024
The example below prints:
map[V1:v1 M2:map[V2:v2]]
V1
v1
M2
map[V2:v2]
[VS1_1 VS1_2]
VS1_1
VS1_2
[map[VMS:vms]]
map[VMS:vms]The first map[string]interface{} is traversed, but the last (map[V2:v2]) seems to be just set as-is without any decoding of its elements. The same is the case for maps inside slices.
package main
import (
"fmt"
"log"
"reflect"
"github.com/mitchellh/mapstructure"
)
type T struct {
M1 map[string]interface{}
S1 []interface{}
S2 []interface{}
}
func main() {
dest := &T{}
hook := func(t1 reflect.Type, t2 reflect.Type, v interface{}) (interface{}, error) {
fmt.Printf("%v\n", v)
return v, nil
}
input := map[string]interface{}{
"M1": map[string]interface{}{
"V1": "v1",
"M2": map[string]interface{}{
"V2": "v2",
},
},
"S1": []interface{}{"VS1_1", "VS1_2"},
"S2": []interface{}{map[string]interface{}{
"VMS": "vms",
}},
}
conf := &mapstructure.DecoderConfig{
DecodeHook: hook,
ErrorUnused: false,
WeaklyTypedInput: false,
Metadata: nil,
Result: dest,
ZeroFields: true,
}
dec, err := mapstructure.NewDecoder(conf)
if err != nil {
log.Fatal(err)
}
err = dec.Decode(input)
if err != nil {
log.Fatal(err)
}
}
Source: mitchellh/mapstructure