协议缓冲区:更改字段名称会破坏消息吗? [英] Protocol buffer: does changing field name break the message?

查看:42
本文介绍了协议缓冲区:更改字段名称会破坏消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用协议缓冲区,更改消息的字段名称是否仍然使其向后兼容?我找不到任何关于此的引用.

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

例如:原始消息

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 定义的应用程序之间的兼容性.

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

二进制 protobuf 编码基于标签号,所以这是您需要保留的.

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

您甚至可以在某种程度上更改字段类型(请查看 https://developers.google.com/protocol-buffers/docs/encoding#structure) 提供其线路类型保持不变,但这需要额外考虑,例如,是否更改 uint32uint64 从您的应用程序代码的角度来看是安全的,对于某些更好"的定义,比简单地定义一个新字段更好.

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.

如果您使用该功能,更改字段名称将影响 json 表示.

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

这篇关于协议缓冲区:更改字段名称会破坏消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆