Protobuf Guideline
The API definition is based on HTTP and gRPC, written with Protobuf format They should includes all the Request, Reply and the corresponding Errors.
The definition of Proto could be either in directory of the project or in a unified repository, likes googleapis, envoy-api,istio-api.
For the proto in project, the api should be used as the root of package name.
For the proto in unified repository, the repository name should be used as the root of package name.
kratos-apis:
|____api // The definition of service's API
| | |____demo
| | | |____v1
| | | | |____demo.proto
|____annotations // the options annotations
|____third_party // third-party protos
Package
- my.package.v1 is the API’s directory, which defines the API of the services.
For example.
// RequestURL: /<package_name>.<version>.<service_name>/{method}
package <package_name>.<version>;
java_package
objc_class_prefix
option objc_class_prefix = "<PackageNameVersion>";
This version is for incompatible version and always used with <package_name>
. It should be modified for API breaking changes.
Import
- the proto dependencies’ import path should be started from root path.
- third_party, includes the proto from third-party such as protobuf, google rpc,google apis, gogo etc.
The package name should be lower-case, consist with the project directory structure, e.g., my/package/v1/
package my.package.v1;
File Structure
The name of proto files should be lower_snake_case.proto
The contents of proto file should be arranged as follows.
- License header (if applicable)
- File overview
- Syntax
- Package
- Imports (sorted)
- File options
Message & Field Naming
message SongServerRequest {
required string song_name = 1;
}
Corresponding with the definitions which mentioned above, the generated code can be shown as follows.
Using keyword to define an array(List):
repeated string keys = 1;
...
repeated Account accounts = 17;
Enums
The name of enums should be PascalCase, and the enum values’ name should be UPPER_CASE_SNAKE_CASE.
enum Foo {
FIRST_VALUE = 0;
SECOND_VALUE = 1;
}
Every line must be end with a semicolon (;) rather than comma.
Services
service FooService {
rpc GetSomething(FooRequest) returns (FooResponse);
}
Comment
- Service describes the functions of this service.
- Method describe the functions of this API.
- Field describe the information of this field.
Service API Definition (demo.proto)