gRPC APIs should be defined in
.proto file(s) using the
The file structure must put higher level and more important definitions before lower level and less important items. In each proto file, the applicable sections should be in the following order:
- Copyright and license notice if needed.
importstatements in that order.
- The API overview documentation which prepares the readers for the rest of the file.
- The API proto
servicedefinitions, in decreasing order of importance.
- The resource
messagedefinitions. A parent resource must be defined before its child resource(s).
- The RPC request and response
messagedefinitions, in the same order of the corresponding methods. Each request message must precede its corresponding response message, if any.
If a single proto file contains the entire API surface, it should be named after the API:
Large .proto files may be split into multiple files. The services, resource messages, and request/response messages should be moved into separate files as needed.
We recommend one file per service with its corresponding request and
responses. Consider naming this file
<enclosed service name>.proto.
For proto files with only resources, consider naming this file simply as
Proto File Names
Proto file names should use lower_case_underscore_separated_names and
must use the extension
.proto. For example:
In order to generate consistent client libraries across different APIs,
API developers must use consistent proto options in their
files. API definitions that conform to this guide must use the
following file-level proto options:
syntax = "proto3"; // The package name should start with the company name and end with // the major version. package google.abc.xyz.v1; // This option specifies the namespace to be used in C# code. This defaults // to the PascalCased version of the proto package, which is fine if the // package name consists of single-word segments. // For example, a package name of "google.shopping.pets.v1" would use a C# // namespace of "Google.Shopping.Pets.V1". // However, if any segment of a package name consists of multiple words, // this option needs to be specified to avoid only the first word being // capitalized. For example, a Google Pet Store API might have a package name of // "google.shopping.petstore.v1", which would mean a C# namespace of // "Google.Shopping.Petstore.V1". Instead, the option should be used to // capitalize it properly as "Google.Shopping.PetStore.V1". // // For more detail on C#/.NET capitalization rules, see the [Framework Design // Guidelines](https://msdn.microsoft.com/en-us/library/ms229043). // // One corner-case of capitalization: while acronyms are generally // PascalCased (e.g. Http), two-letter acronyms are normally all in capitals, // e.g. `IOStream` and `OSVersion`, not `IoStream` and `OsVersion`. However, // in APIs this should be used carefully, as protoc doesn't know which words // are abbreviations and which aren't: it would introduce inconsistency to have // a namespace of (say) `OSLogin` but then a class called `OsDetails` generated // from a message of the same name. Unless you can be certain that the acronym // won't crop up in a message or field name, it's safest to stick to regular // PascalCase. // // For pre-releases, the Alpha/Beta should also be capitalized, so "V1Beta1" // rather than "V1beta1" for example. option csharp_namespace = "Google.Abc.Xyz.V1"; // This option lets the proto compiler generate Java code inside the package // name (see below) instead of inside an outer class. It creates a simpler // developer experience by reducing one-level of name nesting and be // consistent with most programming languages that don't support outer classes. option java_multiple_files = true; // The Java outer classname should be the filename in UpperCamelCase. This // class is only used to hold proto descriptor, so developers don't need to // work with it directly. option java_outer_classname = "XyzProto"; // The Java package name must be proto package name with proper prefix. option java_package = "com.google.abc.xyz.v1"; // A reasonable prefix for the Objective-C symbols generated from the package. // It should at a minimum be 3 characters long, all uppercase, and convention // is to use an abbreviation of the package name. Something short, but // hopefully unique enough to not conflict with things that may come along in // the future. 'GPB' is reserved for the protocol buffer implementation itself. option objc_class_prefix = "GABCX"; // This option specifies the namespace to be used in PHP code. This defaults // to the PascalCased version of the proto package, which is fine if the // package name consists of single-word segments. // For example, a package name of "google.shopping.pets.v1" would use a PHP // namespace of "Google\\Shopping\\Pets\\V1". // However, if any segment of a package name consists of multiple words, // this option needs to be specified to avoid only the first word being // capitalized. For example, a Google Pet Store API might have a package name of // "google.shopping.petstore.v1", which would mean a PHP namespace of // "Google\\Shopping\\Petstore\\V1". Instead, the option should be used to // capitalize it properly as "Google\\Shopping\\PetStore\\V1". // // For pre-releases, the Alpha/Beta should not be capitalized, so "V1beta1" // rather than "V1Beta1" for example. Note that this is different from the // capitalization used in the csharp_namespace option for pre-releases. option php_namespace = "Google\\Abc\\Xyz\\V1";