Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf naming conventions

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

  1. 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.
  2. For Java users, it appears that having java_outer_classname end in Protos 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 to com.example.protobuf.MyProtos unless there are no classes in the project's top package.
  3. Start enums at 0 to match C/C++.

  4. 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?

like image 965
Blair Zajac Avatar asked Nov 08 '10 04:11

Blair Zajac


People also ask

How do you name a proto file?

proto file should be named. MyProtos. proto , my_protos. proto or my-protos.

Can I rename Protobuf fields?

Renaming a field - With Protobuf content, the field names are only used in generated code. The field number is used to identify fields on the network. Renaming a field isn't a protocol breaking change for Protobuf. However, if a server is using JSON content then renaming a field is a breaking change.

Why are Protobuf fields numbered?

Field numbers are an important part of Protobuf. They're used to identify fields in the binary encoded data, which means they can't change from version to version of your service. The advantage is that backward compatibility and forward compatibility are possible.

How do you define a Protobuf?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.


1 Answers

Disclaimer: answer from a Googler using protobufs on a daily basis. I'm by no means representing Google in any way.

  1. 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.

  2. Although not specified officially, it sounds like a good suggestion, because normally you put more than one proto inside a folder.

  3. You normally start with 0. See the protocol buffer language guide.

  4. Yes. Read the following to get some feeling using it: https://developers.google.com/protocol-buffers/docs/javatutorial

like image 134
Michael Xin Sun Avatar answered Oct 03 '22 06:10

Michael Xin Sun