2 回答
TA贡献1799条经验 获得超9个赞
最后我采用了这种方法:
config := &mapstructure.DecoderConfig{
DecodeHook: func(sourceType, destType reflect.Type, raw interface{}) (interface{}, error) {
var result Action
if fmt.Sprintf("%s", destType) == "Action" {
rawMap := raw.(map[interface{}]interface{})
switch rawMap["type"] {
case "one":
result = One{}
case "two":
result = Two{}
}
mapstructure.Decode(raw, &result)
return result, nil
}
return raw, nil
},
Result: &actions,
}
我只是从 Go 开始,所以可能有更好的方法,但这有点解决了我遇到的两个问题。
更正:我不得不恢复if fmt.Sprintf("%s", destType) == "Action"到reflect.TypeOf(result) == nil
- 2 回答
- 0 关注
- 143 浏览
添加回答
举报
