protobuf 数据类型可以升级吗?
Can protobuf data types be upgraded?
我是 Protobuf 的新手,所以不知道这是否可行。如果在原始文件中我有一个 fixed32
数据类型,我可以将它升级到 fixed64
这样所有 existing fixed32
值也可以被读取(即向后兼容性)?
如果没有,执行此类升级的最佳方法是什么?
tl;dr 是的,但不是您的确切类型。
您可以在 varint 编码类型之间自由移动,因为它们在线路上都使用完全相同的编码。
int32
, uint32
, int64
, uint64
, and bool
are all compatible – this means you can change a field from one of these types to another without breaking forwards- or backwards-compatibility. If a number is parsed from the wire which doesn't fit in the corresponding type, you will get the same effect as if you had cast the number to that type in C++ (e.g. if a 64-bit number is read as an int32
, it will be truncated to 32 bits).
遗憾的是,这不适用于 fixed32
(仅与 sfixed32
兼容)和 fixed64
(仅与 sfixed64
兼容)。
如果您已经有大量使用 fixed32
字段的数据,则必须创建一个新的 int64
字段并逐渐从现有字段过渡到新字段(新代码读取两个字段,但只填充新字段)。
我是 Protobuf 的新手,所以不知道这是否可行。如果在原始文件中我有一个 fixed32
数据类型,我可以将它升级到 fixed64
这样所有 existing fixed32
值也可以被读取(即向后兼容性)?
如果没有,执行此类升级的最佳方法是什么?
tl;dr 是的,但不是您的确切类型。
您可以在 varint 编码类型之间自由移动,因为它们在线路上都使用完全相同的编码。
int32
,uint32
,int64
,uint64
, andbool
are all compatible – this means you can change a field from one of these types to another without breaking forwards- or backwards-compatibility. If a number is parsed from the wire which doesn't fit in the corresponding type, you will get the same effect as if you had cast the number to that type in C++ (e.g. if a 64-bit number is read as anint32
, it will be truncated to 32 bits).
遗憾的是,这不适用于 fixed32
(仅与 sfixed32
兼容)和 fixed64
(仅与 sfixed64
兼容)。
如果您已经有大量使用 fixed32
字段的数据,则必须创建一个新的 int64
字段并逐渐从现有字段过渡到新字段(新代码读取两个字段,但只填充新字段)。