Protocol buffer: does changing field name break the message?

时光毁灭记忆、已成空白 提交于 2019-12-04 10:17:11

问题


With protocol buffer, does changing field name of a message still let it compatible backward? I couldn't find any cite about that.

Eg: original message

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

Change to:

message Person {
  required string full_name = 1;
  required int32 id = 2;
  optional string email = 3;
}

回答1:


Changing a field name will not affected protobuf encoding or compatibility between applications that use proto definitions which differ only by field names.

The binary protobuf encoding is based on tag numbers, so that is what you need to preserve.

You can even change a field type to some extent (check the type table at https://developers.google.com/protocol-buffers/docs/encoding#structure) providing its wire type stays the same, but that requires additional considerations whether, for example, changing uint32 to uint64 is safe from the point of view of your application code and for some definition of 'better' is better that simply defining a new field.

Changing a field name will affect json representation, if you use that feature.



来源:https://stackoverflow.com/questions/45431685/protocol-buffer-does-changing-field-name-break-the-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!