如何使用反射创建地图指针?

huangapple go评论144阅读模式
英文:

How to make a map pointer using reflect?

问题

我需要使用反射包将一个指向map的指针传递给另一个函数。可以将其类比为encoding/json.Unmarshal,它只接受指针作为参数。以下是我使用反射需要完成的一些代码:

myTyp := &MyType{}
json.Unmarshal(jsonPayload1, myTyp)
m := make(map[string]*MyType)
m["someKey"] = myTyp
// 再次解码。只有当存在"someKey"键时,它才会覆盖它。
x := json.Unmarshal(jsonPayload2, &m)

到目前为止,我有以下代码:

....
m := reflect.MakeMap(reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(typ)))
x := json.Unmarshal(jsonPayload2, m.Interface())

这段代码的问题在于它创建了一个map[string]MyType的映射,但我无法为其创建一个指针。如果我尝试直接创建指针,它会引发reflect.MakeMap of non-map type的恐慌错误。

为什么必须分配映射:

我需要分配映射,以便可以将myTyp设置在特定的键上。myTyp是使用多个解码源解码的,因此我不知道每个源解码了哪些字段,但我知道Unmarshal只会覆盖与JSON内容/键匹配的键,因此可以安全地在多个Unmarshal函数中使用同一个变量。

[2] reflect.MakeMap(reflect.PtrTo(reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(typ))))

英文:

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

答案1

得分: 5

对于像json.Unmarshal这样的函数,你不需要使用map,只需要正确的类型指针,可以使用reflect.New来创建。json包会根据需要分配map。

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

如果你需要分配map,可以使用Set方法将其分配给指针。

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

ptr := reflect.New(mapType)
ptr.Elem().Set(reflect.MakeMap(mapType))

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

英文:

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

huangapple
  • 本文由 发表于 2015年10月17日 05:31:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/33179876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定