I'm receiving a JSON blob from a Kubernetes API that contains a type that includes a protobuf subfield. Since that protobuf includes oneof
fields, I have to use golang/protobuf/jsonpb
and not encoding/json
.
I've reproduced this simply here:
client.proto
message Latency {
oneof latency_type {
StaticLatency static = 1;
NormalLatency normal = 2;
}
}
client.go:
type Metadata struct {
Name string `json:"name,omitempty"`
}
type LatencyWrapper struct {
Meta Metadata `json:"meta"`
Latency Latency `json:"latency"`
}
How can I unmarshal this message? I think I want something like this:
func (lw *LatencyWrapper) UnmarshalJSON(b []byte) error {
var parts map[string]string = Something()
json.Unmarshal(parts["meta"], &lw.meta)
jsonpb.Unmarshal(parts["latency"], &lw.latency)
return nil
}
Thanks for your help!
Michael