动态更改序列化的protobuf消息?

动态更改序列化的protobuf消息?

问题描述:

Is it possible to alter (append, merge, etc) a serialized protobuf message without having to unmarshal it first? I'm using the golang/protobuf package.

Ideally I would like to have a service that can receive incoming serialized messages, append some fields on-the-fly, then pass the message along to the next service -kind of like middleware, where additional information can just be added to the payload without having to constantly unmarshal and marshal.

Some context: the system is realtime, so I'd like to minimize overhead wherever possible.

From the documentation on protocol buffers:

As you know, a protocol buffer message is a series of key-value pairs. The binary version of a message just uses the field's number as the key – the name and declared type for each field can only be determined on the decoding end by referencing the message type's definition (i.e. the .proto file).

When a message is encoded, the keys and values are concatenated into a byte stream. When the message is being decoded, the parser needs to be able to skip fields that it doesn't recognize. This way, new fields can be added to a message without breaking old programs that do not know about them. To this end, the "key" for each pair in a wire-format message is actually two values – the field number from your .proto file, plus a wire type that provides just enough information to find the length of the following value.

Therefore, to append a field to an encoded protocol buffer message, you can simply append an encoded field to the end of the byte stream/slice.