Proto3: Style

This article describes the .protocoding style file. Follow practice, you can make your message protocol buffer corresponding class definitions and their consistency and read.

Note, protocol buffer style has been in progress over time, so you'll probably written in .prototo see the different practices or style file. When you modify these files , please consider the existing style . Consistency is the key . However, in creating a new .protoa file, preferably using the most popular style.

Standard file format

  • Up to 80 letters per line.
  • Indent using two spaces.

File Structure

File name should use lower_snake_case.protothe format.

All documents should be most of the following ways:

  1. Licensing head (if any)
  2. Overview of the document
  3. grammar
  4. package
  5. Introducing item (after sorting)
  6. File Options
  7. leftover

package

Package names should be lowercase, and should correspond to the directory hierarchy. For example, if the file my/package/, then the package name should be my.package.

Messages and field names

Use hump (capitalized) name message name - for example, SongServerRequest. Use underscore named field names (including oneof field names and extensions) - e.g., song_name.

message SongServerRequest {
    string song_name = 1;
}

Field is generated using the naming convention accessor following format:

const string& song_name() { ... }
void set_song_name(const string& x) { ... }
public String getSongName() { ... }
public Builder setSongName(String v) { ... }

If your field names contain numbers, that number should be tight after the last letter instead of an underscore, for example, to use song_name1instead song_name_1.

Repeat field

Use plural names for duplicate fields:

repeated string keys = 1;
...
repeated MyMessage accounts = 17;

enumerate

Use hump (capitalized) name enumerated types, uppercase underlined named variables name:

enum Foo {
  FOO_UNSPECIFIED = 0;
  FOO_FIRST_VALUE = 1;
  FOO_SECOND_VALUE = 2;
}

Each enumeration should end with a semicolon instead of a comma. Enumeration value prefix, not surrounded by a closure message. 0 deserve enumeration values should have UNSPECIFIEDthe suffix.

service

In .protothe definition of the RPC service, and RPC service name for the method name, you should use the hump (capitalized) name.

service FooService {
  rpc GetSomething(FooRequest) returns (FooResponse);
}

To avoid things

  • RequiredField (for proto2, should be removed from the grammatical level only for the proto3 required)
  • Groups(For proto2 only)

Guess you like

Origin www.cnblogs.com/lianshuiwuyi/p/12228655.html