一旦你更新了原型的定义,旧的原型文件会发生什么?
What happens to old proto files once you update the definition of the proto?
假设有一天我将原始消息保存在文件中 my_file.pb
第二天,我在消息中添加了一个新字段。
然后我从磁盘读取 my_file.pb 并尝试访问新字段。这会崩溃吗?这是UB吗?是否按标准处理?
我尝试阅读文档,但我所能得到的是:
You can add new fields to your message formats without breaking backwards-compatibility; old binaries simply ignore the new field when parsing. So if you have a communications protocol that uses protocol buffers as its data format, you can extend your protocol without having to worry about breaking existing code.
这并没有告诉我如果我的代码试图访问最初未在存储的原型中定义的字段会发生什么行为。
如果您使用的是 proto3:没问题,所有字段都是可选的。它只是空的/零。
如果您使用的是 proto2:视情况而定; "optional" 或 "repeated" - 很好,没问题 - 它只是 empty/default。 "required":繁荣,你干杯了。所以...只是不要那样做?
假设有一天我将原始消息保存在文件中 my_file.pb
第二天,我在消息中添加了一个新字段。
然后我从磁盘读取 my_file.pb 并尝试访问新字段。这会崩溃吗?这是UB吗?是否按标准处理?
我尝试阅读文档,但我所能得到的是:
You can add new fields to your message formats without breaking backwards-compatibility; old binaries simply ignore the new field when parsing. So if you have a communications protocol that uses protocol buffers as its data format, you can extend your protocol without having to worry about breaking existing code.
这并没有告诉我如果我的代码试图访问最初未在存储的原型中定义的字段会发生什么行为。
如果您使用的是 proto3:没问题,所有字段都是可选的。它只是空的/零。
如果您使用的是 proto2:视情况而定; "optional" 或 "repeated" - 很好,没问题 - 它只是 empty/default。 "required":繁荣,你干杯了。所以...只是不要那样做?