I have a protobuf message
message Event {
string type = 1;
string names = 2;
}
The type for ‘names’ has recently changed from string to []string in the underlying data source where older data is still a string but new data is going to be []string.
So, when the data is returned, some records can have string and the rest can have []string.
I need to customize the unmarshaling (jsonpb.Unmarshaler is being used to unmarshal json.Rawmessage to Event type) and convert the ‘names’ to a string if it’s an []string (by concatenating the values from array/slice) in order to keep the response format consistent so client doesn't break.
I get this error while unmarshaling:
json: cannot unmarshal array into Go value of type string
Can anyone please suggest on how this can be done?
I tried google.protobuf.Any like below but couldn’t completely figure the unmarshaling part.
message Event {
string type = 1;
google.protobuf.Any names = 2;
}
Unmarshall snippet:
evnt := new(pb.Event)
unmarshaler = jsonpb.Unmarshaler{AllowUnknownFields: true}
unmarshaler.Unmarshal(bytes.NewReader(*<json.RawMessage>*), evnt)