This article describes the .proto
coding 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 .proto
to 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 .proto
a 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.proto
the format.
All documents should be most of the following ways:
- Licensing head (if any)
- Overview of the document
- grammar
- package
- Introducing item (after sorting)
- File Options
- 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_name1
instead 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 UNSPECIFIED
the suffix.
service
In .proto
the 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
Required
Field (for proto2, should be removed from the grammatical level only for the proto3required
)Groups
(For proto2 only)