协议缓冲区:更改字段名称会破坏消息吗?
Protocol buffer: does changing field name break the message?
使用protocol buffer,改变消息的字段名是否仍然让它向后兼容?我找不到任何关于它的引用。
例如:原始消息
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
更改为:
message Person {
required string full_name = 1;
required int32 id = 2;
optional string email = 3;
}
更改字段名称不会影响 protobuf 编码或使用仅字段名称不同的 proto 定义的应用程序之间的兼容性。
二进制 protobuf 编码基于标签号,因此这是您需要保留的内容。
您甚至可以在某种程度上更改字段类型(检查 https://developers.google.com/protocol-buffers/docs/encoding#structure 处的类型 table),前提是它的连线类型保持不变,但这需要额外的考虑,例如,更改uint32
到 uint64
从您的应用程序代码的角度来看是安全的,并且对于 'better' 的某些定义,比简单地定义一个新字段要好。
如果您使用该功能,更改字段名称将影响 json 表示。
使用protocol buffer,改变消息的字段名是否仍然让它向后兼容?我找不到任何关于它的引用。
例如:原始消息
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
更改为:
message Person {
required string full_name = 1;
required int32 id = 2;
optional string email = 3;
}
更改字段名称不会影响 protobuf 编码或使用仅字段名称不同的 proto 定义的应用程序之间的兼容性。
二进制 protobuf 编码基于标签号,因此这是您需要保留的内容。
您甚至可以在某种程度上更改字段类型(检查 https://developers.google.com/protocol-buffers/docs/encoding#structure 处的类型 table),前提是它的连线类型保持不变,但这需要额外的考虑,例如,更改uint32
到 uint64
从您的应用程序代码的角度来看是安全的,并且对于 'better' 的某些定义,比简单地定义一个新字段要好。
如果您使用该功能,更改字段名称将影响 json 表示。