英文:
How can I add a alias for field in protobuf?
问题
我们有一些非常旧的 proto 消息,类似于这样的:
message SomeProto {
string original_field = 1;
//其他字段
}
但是,original_field 的名称具有误导性,因为在产品生命周期内进行了许多更改,现在每当团队成员使用它时都感到困惑。
我想能够为字段 1 添加一个别名,以便它仍然与旧应用程序兼容。如果它是枚举类型,我可以像这样做:
message SomeProto {
option allow_alias = true;
string changed_field = 1;
string original_field = 1 [deprecated = true];
//其他字段
}
但是,当然它在消息中不起作用。
有没有一种优雅的方式来处理这个问题?
英文:
We have a very old proto messages like this
message SomeProto {
string original_field = 1;
//other fields
}
But, original_feild's name is misleading because many changes were made during product lifecycle, and now everyone on the team is confused everytime they are using it.
I want to be able to add a alias for field 1, so that it is still compatible with old apps. If it was enum, I could've done something like this.
message SomeProto {
option allow_alias = true;
string changed_field = 1;
string original_field = 1; [deprecated = true;]
//other fields
}
But, of course it doesn't work on message.
Is there a graceful way to handle this?
答案1
得分: 1
> 我想要能够为字段1添加一个别名,以便与旧应用程序兼容。
只要保持标签号不变,就可以保留二进制兼容性,您可以自由更改字段名称。
要保持源代码兼容性,可用的选项并不多。
您可以将整个消息定义复制为SomeProtoV2
,但从长远来看可能不太有用。当.proto文件更新时,我会修改源代码。
在编译型语言中,您会收到任何未更新的行的错误,因此很容易找到。对于脚本语言,进行项目范围的搜索是一个好主意。
英文:
> I want to be able to add a alias for field 1, so that it is still compatible with old apps.
Binary compatibility is retained as long as you keep the tag number the same, you are free to change the field name.
To retain source code compatibility, there aren't that many options available.
You could copy the whole message definition as SomeProtoV2
, but that is probably not very useful in the long term. I would just modify the source code when the .proto file gets updated.
In compiled languages you'll get errors from any line that has not been updated, so it is easy to find. For scripting languages a project-wide search is a good idea.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论