Protobuf消息字段ID和字段顺序之间的关系是什么?

huangapple go评论77阅读模式
英文:

What is the relation of protobuf message field id and field order?

问题

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

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

在任何情况下,顺序是否对兼容性有影响?

英文:

I want to understand if the messages bellow are compatible from the perspective of protobuf and serialization/deserialization.

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

Does the order matter for compatibility in any situation?

答案1

得分: 3

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

在协议级别上:

  • 解析器必须允许以任何顺序出现字段
  • 序列化器应该(但不是必须)按照升序的数字字段顺序写入字段
英文:

The textual order is largely irrelevant, although it may impact some code generation tooling - but most languages don't care about declaration order, so even that: won't matter. The fields are still defined semantically equivalent - the numbers match the existing meaning (name) and type. It is the number that is the determining feature in identifying a field.

At the protocol level:

  • parsers must allow fields in any order
  • serializers should (but not must) write fields in ascending numerical field order

huangapple
  • 本文由 发表于 2021年11月12日 03:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/69933575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定