dpd46554 2015-12-18 21:30
浏览 326
已采纳

动态更改序列化的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.

  • 写回答

1条回答 默认 最新

  • douzhi2012 2015-12-18 21:51
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!