【问题标题】:What is the relation of protobuf message field id and field order?protobuf 消息字段 id 和字段顺序的关系是什么?
【发布时间】:2021-11-11 19:03:02
【问题描述】:

我想从protobuf和序列化/反序列化的角度了解下面的消息是否兼容。

message HelloReply {
  string message = 1;
  string personalized_message = 2;
}
message HelloReply {
  string personalized_message = 2;
  string message = 1;
}

在任何情况下,顺序是否对兼容性很重要?

【问题讨论】:

  • 二进制消息中数字的全部指向identify the field,所以这些消息应该是兼容的

标签: go protocol-buffers field message


【解决方案1】:

文本顺序在很大程度上无关紧要,尽管它可能会影响某些代码生成工具 - 但大多数语言并不关心声明顺序,因此即使这样:也无所谓。这些字段仍然在语义上等价地定义 - 数字与现有含义(名称)和类型匹配。数字是识别字段的决定性特征。

在协议级别:

  • 解析器必须允许任何顺序的字段
  • 序列化程序应该(但不是必须)按字段数字升序写入字段

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多