简体   繁体   中英

how to parse a protobuf3 message without knowing its type at compile time with golang?

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:

  • receive a message type (string) from configuration and a name of a kafka topic
  • would need to create the binary -> memory deserializer and the memory -> json serializer at runtime.

how do you get a deserializer for your binary bytes just from the message name?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM