英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论