知识卡片
Protobuf字段号不可重排
Protocol Buffers 的二进制消息靠字段号识别字段,发布后重排编号会改变线上的含义。新字段通常可追加,但要考虑旧客户端忽略未知字段、默认值和业务语义;删除字段还应保留其编号,防止未来误用。字段改名未必改变二进制线格式,却可能破坏生成代码或 JSON 名称依赖。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- · 查看原书依据
In protocols used for binary representations, it is important to note that the position and order of fields is critical, as they govern the layout of the message. Adding a new service or new method is backward compatible as is adding a field to a message, but care is required.
参考来源
- 位置:《Mastering API Architecture》第1章《Design, Build, and Specify APIs》之《Implementing RPC with gRPC》(源文件:_epub-src/OEBPS/ch01.xhtml)
- 结论依据:原文强调 .proto 的字段编号影响线格式;对字段改名的限定另据 Protocol Buffers 官方 ProtoJSON 指南(https://protobuf.dev/programming-guides/json/)和最佳实践(https://protobuf.dev/best-practices/dos-donts/),区分二进制线格式、生成代码与 JSON 名称依赖,不把这一修正算作原书本段直接结论。
- 原始内容:In protocols used for binary representations, it is important to note that the position and order of fields is critical, as they govern the layout of the message. Adding a new service or new method is backward compatible as is adding a field to a message, but care is required.