1
我正在跟进Golang Decoding Generic JSON Objects to One of Many Formats作为解组泛型json的一种方式。我将会有许多不同类型的tho可以被其他人添加,所以硬编码的case语句是不可行的。使用类型查找映射反编组泛型json
我也不想将该类型硬编码为字符串,但让那些使用库的人选择“查找”名称,以防他们稍后重新命名其基础结构。
我基本上是在寻找这样的事情:
type myInterface interface {
Something() // irrelevant, just to show you It's not about interface{}
}
type myBar struct {} // fulfils myInterface
type mySomething struct {} // fulfils myInterface
var types = make(map[string]type) // <--- Obvious Pseudo code ;)
types["foo:bar"] = myBar // done by whoever uses the library
types["1230988"] = mySomething // ...
type storageWrapper struct {
Type string
Data json.RawMessage
}
func loadSomething(id string) myInterface {
buf := db.load(id) // pseudo code but you get the idea
sw := &storageWrapper{}
json.Unmarshal(buf, sw)
// now the interesting part
targetType := types[sw.Type]
thing := &targetType{}
json.Unmarshal(sw.Data, thing)
return thing
}
我有这种感觉,我的得太多整个问题。或者,我正试图弯曲与其底层哲学冲突的事物。我非常开放并且非常感谢任何意见,建议采取不同的方法来解决整个问题
接受这个答案是因为它完成了工作。不过,我现在改变了我的代码,所以它不再依赖于类型映射 – paukul