Protobuf Naming Conventions

Besides the rather short Google provided style guide , here are my thoughts on the Google Protocol Buffer message names.

  • Use the "Message" at the end of the message type names.

    • This makes it easy to see in the source code that the class is a class generated by the proto-buf. This also has the advantage that if I have a class with a rich domain, then it can have a real name, for example AddressBookMessage for the protobuf class and AddressBook for the real class.
  • For Java users, it seems that java_outer_classname end in Protos is standard.

    • I did not notice this at the initial stage, so my current protobuf classes are in com.example.project.protobuf.MyProtos , but I see no reason to keep it there, since we need to have a containing class, so it can be com.example.protobuf.MyProtos to com.example.protobuf.MyProtos if there are no classes in the top package of the project.
  • Start enums at 0 according to C / C ++.

  • Use a unique name for the repeated field.

    • Most of the generated methods sound better with a unique field name, even if it is repeated, for example. message-> add_child (), not message-> add_children () if you had a repeated child field.

Are there any other standards that people use or differ from?

+6
protocol-buffers
source share
2 answers

Disclaimer: A response from Googler using protobufs on a daily basis. I in no way represent Google.

  • Do not do this. Compiled protocol buffers are simply a class definition indicated in the language you are using, with some improvements. the addition of β€œMessage” is additional verbosity. Usually you just use protocol buffers without other class definitions, and even if you use other class definitions, just import java_outer_classname and make the exact point. You can even put the full path in the code to erase a single import line, no problem.

  • Although this is not officially indicated, it sounds good, because usually you put several protons in a folder.

  • You usually start with 0. See the protocol buffer language guide.

  • Yes. Read the following to feel its use: https://developers.google.com/protocol-buffers/docs/javatutorial

+3
source share

I do not agree with answer 4. In a related article I can find only such examples:

 repeated PhoneNumber phones = 4; repeated Person people = 1; 

And even at https://developers.google.com/protocol-buffers/docs/proto3 we find the plural:

 repeated Result results = 1; repeated string snippets = 3; 
+3
source share

All Articles