Reputation: 1457
here's a scenario:
you're implementing in golang a generic component that can may be used with any type of proto message (binary serialization) and need to deserialize binary proto data without knowing its type at compile time.
for instance, i encountered this issue while writing a generic kafka json archiver, the component would:
how do you get a deserializer for your binary bytes just from the message name?
Upvotes: 5
Views: 5983
Reputation: 1457
golang proto libraries have a helper utility for this purpose:
// MessageType returns the message type (pointer to struct) for a named message.
// The type is not guaranteed to implement proto.Message if the name refers to a
// map entry.
func MessageType(name string) reflect.Type {
// ....
}
to use it you can use a method similar to this:
func getProto(messageType string, messageBytes []byte) proto.Message {
pbtype := proto.MessageType(messageType)
msg := reflect.New(pbtype.Elem()).Interface().(proto.Message)
proto.Unmarshal(messageBytes, msg)
return msg
}
i have put a full example of this on github: https://github.com/rotemtam/pbreflect-example
Upvotes: 5