Д
Size: a a a
ВС
ВС
RS
АП
RS
АП
RS
var type = []interface{}{
&Foo{},
&Bar{},
&Baz{},
}
АП
АП
АП
RS
event
ряд типов аляtype EventFoo struct {
Foo string json:”foo”
}
type EventBar struct {
Bar int`json:”bar”`
Baz float64`json:”baz”`
}
type EventFuzz struct {
Maz int json:”maz”
}
и мне периодически приходят JSON’ки такого типа:type Input struct {
Type string json:”type”
Data json.RawMessage json:”data”
}
и мне в зависимости от имени типа нужно анмаршалить в тот или иной Go’шный типАП
АП
Input
, берете от туда Type и потом внутри свича еще один анмашал в нужную структуруRS
var d interface{}
decode := func(tp interface{}) error {
d = tp
return json.Unmarshal(b, &d)
}
switch i.Type {
case “Foo”:
return decode(&EventFoo{})
case “Bar”:
return decode(&EventBar{})
case “Fuzz”:
return decode(&EventFuzz{})}
}
case
в этот switch
VM
var d interface{}
decode := func(tp interface{}) error {
d = tp
return json.Unmarshal(b, &d)
}
switch i.Type {
case “Foo”:
return decode(&EventFoo{})
case “Bar”:
return decode(&EventBar{})
case “Fuzz”:
return decode(&EventFuzz{})}
}
case
в этот switch
RS
VM
RS
RS
init
я не смогу просто заинициализировать мапу типов