protobuf 对字段名称更改的向后兼容性 [英] protobuf backward compatibility on field name change

查看:239
本文介绍了protobuf 对字段名称更改的向后兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果修改了 proto 的枚举名称,是否向后兼容?

If a proto's enum name is changed, is it backward compatible?

例如我最初有这个:

enum ids {
  ID_1 = 1;
  ID_2 = 2; 
};


message {
  ids id = 1
};

我将枚举定义修改为:

enum ids {
  ID_3 = 1;
  ID_2 = 2;
};

带有 ID_1 的旧 proto 消息是否与从包含 ID_3 的消息编译的新解析器兼容?

Are old proto messages with ID_1 compatible with the new parser compiled from the message that contains ID_3?

推荐答案

既然你提到了使用 proto3,我首先有一些观察.

Since you mentioned using proto3, I have a couple of observations first.

根据proto3 docs,在定义枚举时,您应该始终将零值声明为枚举值列表中的第一个条目.这允许 protobuf 使用 0 作为数字默认值,并与 proto2 语义兼容,其中第一个枚举值始终是默认值.

According to the proto3 docs, when defining an enum, you should always declare a zero value as the first entry in the enum value list. This allows protobuf to use 0 as a numeric default value and for compatibility with proto2 semantics where the first enum value is always the default value.

类似于:

enum ids {
  UNKNOWN = 0;
  ID_1 = 1;
  ID_2 = 2; 
};

对于您原来的问题,是否替换现有枚举值上的标签向后兼容,答案是肯定的,也不是.

To your original question, is replacing a label on an existing enum value backward compatible, the answer is yes and no.

就在线行为而言,它是向后兼容的.假设一个服务有一个旧版本的原型,ID_1 = 1,客户端有一个新版本的原型,ID_3 = 1.如果服务器在它的末端设置 ID_1,这将转换为值 1 通过线路发送并将在客户端解释为 ID_3.

It is backward compatible as far as over-the-wire behavior goes. Assume a service has an older version of the proto with ID_1 = 1 and a client has a newer version of the proto with ID_3 = 1. If the server sets ID_1 on its end, this translates to a value of 1 being sent over the wire and will be interpreted as ID_3 on the client end.

它不是向后兼容的,因为当你修改后的 proto 被编译成你用来处理它的任何语言时,如果存在使用旧版本 proto 的现有代码,将会有一个编译时中断因为标签将从 ID_1 更改为 ID_3.

It is not backward compatible in the sense that when your revised proto gets compiled into whatever language you are using to handle it, there will be a compile-time break if there was existing code that was consuming the older version of the proto because the label will have changed from ID_1 to ID_3.

希望这会有所帮助.

这篇关于protobuf 对字段名称更改的向后兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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