I need to pass a pointer to a map to another function using the reflect package. Think of encoding/json.Unmarshal which accepts only pointers. Below is some code of what I need to do using reflection:
myTyp := &MyType{}
json.Unmarshal(jsonPayload1, myTyp)
m := make(map[string]*MyType)
m["someKey"] = myTyp
// Dump it again to Unmarshal. It will overwrite it only if
// there is a `someKey` key.
x := json.Unmarshal(jsonPayload2, &m)
So far I have the following:
....
m := reflect.MakeMap(reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(typ))
x := json.Unmarshal(jsonPayload2, m.Interface())
The issue with this code is that it's making a map of map[string]MyType but there is no way I can create a pointer for it. If I try to make the pointer [2] directly it panics with reflect.MakeMap of non-map type
.
Why the map must be allocated:
I need the map allocated so that I can set myTyp on a specific key. myTyp is decoded using multiple decoding sources so I don't know what fields were decoded with each source but I know that Unmarshal overwrites only keys that match the json content/keys so it's safe to use the same variable to multiple Unmarshal functions.
[2] reflect.MakeMap(reflect.PtrTo(reflect.MapOf(reflect.TypeOf("")), reflect.TypeOf(typ))
For a function like
json.Unamrshal
, you don't need the map, you only need the correct type pointer, which you can make withreflect.New
. The json package will allocate the map as needed.If you need to allocate the map, use the Set method to assign it to the pointer.
http://play.golang.org/p/hnOhh8242i