How to make a map pointer using reflect?

2019-08-14 20:37发布

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))

标签: reflection go
1条回答
\"骚年 ilove
2楼-- · 2019-08-14 21:05

For a function like json.Unamrshal, you don't need the map, you only need the correct type pointer, which you can make with reflect.New. The json package will allocate the map as needed.

m := reflect.New(reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(typ)))

If you need to allocate the map, use the Set method to assign it to the pointer.

http://play.golang.org/p/hnOhh8242i

mapType := reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(typ))

ptr := reflect.New(mapType)
ptr.Elem().Set(reflect.MakeMap(mapType))
查看更多
登录 后发表回答