Protobuf 命名约定
除了 Google 提供的相当简短的样式指南之外,以下是我对命名的想法Google Protocol Buffer 消息。
在消息类型名称末尾使用“消息”。
- 这使得在源代码中很容易看出一个类是 protobuf 生成的类。这还有一个优点,如果我有一个丰富的域特定类,那么它可以有真实的名称,比如 protobuf 类的 AddressBookMessage 和真实类的 AddressBook。
对于 Java 用户来说,以
Protos
结尾的java_outer_classname
似乎是标准的。- 我最初没有注意到这一点,所以我当前的 protobuf 类位于
com.example.project.protobuf.MyProtos
中,但我认为没有理由将其保留在那里,因为我们需要有一个包含类,因此可以将其移动到com.example.protobuf.MyProtos
,除非项目的顶级包中没有类。
- 我最初没有注意到这一点,所以我当前的 protobuf 类位于
从 0 开始枚举以匹配 C/C++。
对重复字段使用单一名称。
- 大多数生成的方法使用单一字段名称听起来更好,即使它是重复的,例如 message->add_child(),而不是 message->add_children()(如果有重复的子字段)。
人们还使用其他标准或与这些标准不同吗?
Besides the rather short Google provided style guide, here are my thoughts on naming Google Protocol Buffer messages.
Use "Message" at the end of message types names.
- This makes it easy to see in source code that a class is a protobuf generated class. This also has the advantage that if I have a rich domain specific class, then it can have the real name, say AddressBookMessage for the protobuf class and AddressBook for the real class.
For Java users, it appears that having
java_outer_classname
end inProtos
is standard.- I didn't notice this initially, so my current protobuf classes are in
com.example.project.protobuf.MyProtos
, but I don't see a reason to keep it there given that we need to have a containing class, so it could be moved tocom.example.protobuf.MyProtos
unless there are no classes in the project's top package.
- I didn't notice this initially, so my current protobuf classes are in
Start enums at 0 to match C/C++.
Use a singular name for a repeated field.
- Most of the generated methods sound better with a singular field name, even if it is repeated, e.g. message->add_child(), instead of message->add_children() if one had a repeated child field.
Are there any other standards people use or differ from these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
免责声明:来自每天使用 protobuf 的 Google 员工的回答。我绝不以任何方式代表谷歌。
不要那样做。编译的协议缓冲区只是您所使用的语言指定的类定义,并进行了一些改进。添加“消息”会显得更加冗长。通常,您只使用协议缓冲区而不使用其他类定义,即使您使用其他类定义,也只需导入 java_outer_classname 并从中做一个点。您甚至可以在代码中放入某个事物的完整路径来删除一行导入,没问题。
虽然没有正式指定,但这听起来是一个很好的建议,因为通常你会在一个文件夹中放置多个原型。
通常从 0 开始。请参阅协议缓冲区语言指南。
您可以使用复数来表示重复的字段名称。阅读以下内容以了解使用它的一些感受:https://developers.google.com/协议缓冲区/docs/javatutorial
Disclaimer: answer from a Googler using protobufs on a daily basis. I'm by no means representing Google in any way.
Don't do that. Compiled protocol buffers are just a class definition specified by the language you are using, with some improvements. Adding "Message" is extra verbosity. Normally you just use protocol buffers without other class definitions, and even if you use other class definitions, just import java_outer_classname and do a dot from that. You can even put full path of a thing in code to erase one line of import, no problem.
Although not specified officially, it sounds like a good suggestion, because normally you put more than one proto inside a folder.
You normally start with 0. See the protocol buffer language guide.
You use plurals for repeated field names. Read the following to get some feeling using it: https://developers.google.com/protocol-buffers/docs/javatutorial
我不同意答案 4。在链接的文章中,我只能找到这样的示例:
甚至在 https://developers.google.com/protocol-buffers/docs/proto3 我们只找到复数:
I don't agree with answer 4. In the linked article I can find only examples like this:
And even in https://developers.google.com/protocol-buffers/docs/proto3 we only find plurals:
您要寻找的是https://cloud.google.com/apis/ design/ 讨论了 Google 自己的 API 中使用的 protobuf/gRPC 设计约定。
protobuf 风格指南 (https://developers.google.com/protocol-buffers/docs /style) 正如你所说,相当短。
What you're looking for is the https://cloud.google.com/apis/design/ which talks about protobuf/gRPC design conventions that are used in Google's own APIs.
The protobuf Style Guide (https://developers.google.com/protocol-buffers/docs/style) is rather short as you said.
Google 似乎在 https://developers.google.com/protocol-buffers/docs/style 同时:
它为
add_
成员函数生成一个错误的函数名称:有人可能会认为第一个方法无论如何都是多余的。通过使用
mutable_
成员函数,如果将复数字段名称用于重复
字段,我不会发现任何有关难看的方法名称的问题。因此,从现在开始我将尝试遵循这个指导方针。另一个原因:我们也倾向于在 C++ 中使用复数变量名来表示容器/集合,例如
It seems Google added a guideline for
repeated
field names (point 4) at https://developers.google.com/protocol-buffers/docs/style in the meanwhile:It generates one bad function name for the
add_<field_name>
member function:One could argue that the first method is redundant anyway. By using the
mutable_<field_name>
member function I do not see any problems regarding ugly method names if plural field names are used forrepeated
fields.Therefore I will try to follow this guideline from now on. Another reason for it: We also tend to use plural variable names for containers/collections in C++, e.g.