본문 바로가기
Framework and Tool/gRPC and Protobuf

Protocol buffer - Convention

by ocwokocw 2022. 4. 29.

- 출처: https://developers.google.com/protocol-buffers/docs/style

- Formatting

protocol buffer 파일을 작성할 때에는 아래의 format을 따른다.
 
  • 1 라인당 80 글자수
  • 들여쓰기는 2 space
  • string은 ""를 사용

- File 구조

File은 lower_snake_case.proto 형태를 갖는다. 파일내에 기술되는 항목의 순서는 다음과 같다.
 
  • License header
  • File overview
  • Syntax(2, 3)
  • Package
  • Imports (sorted)
  • File options
  • 그 외

- Packages

package 명은 소문자 여야 한다.

- Message and field name

  • Message: CamelCase (ex - SongServerRequest)
  • Field: underscore_separated_names (ex - song_name)
message SongServerRequest {
  optional string song_name = 1;
}
 
숫자는 _ 뒤가 아니라 문자뒤에 나타나야 한다.
 
  • song_name1 (O)
  • song_name_1 (X)

- Repeated fields

repeated field를 기술할 때는 복수화를 사용해준다.
 
repeated string keys = 1;
  ...
repeated MyMessage accounts = 17;
 

- Enums

enum type을 정의할 때는 CamelCase를 사용하며, 값의 이름을 정의할 때는 CAPITALS_WITH_UNDERSCORES를 사용한다.
 
enum FooBar {
  FOO_BAR_UNSPECIFIED = 0;
  FOO_BAR_FIRST_VALUE = 1;
  FOO_BAR_SECOND_VALUE = 2;
}
 

- Services

.proto 에서 RPC 서비스를 정의할 때는 서비스 명과 RPC 메소드명 모두 CamelCase 를 사용한다.
service FooService {
  rpc GetSomething(GetSomethingRequest) returns (GetSomethingResponse);
  rpc ListSomething(ListSomethingRequest) returns (ListSomethingResponse);
}
 
 

'Framework and Tool > gRPC and Protobuf' 카테고리의 다른 글

Protocol buffer - Proto3  (0) 2022.04.24
Protocol buffer  (0) 2022.03.13
gRPC with protobuf  (0) 2022.03.13
RPC(Remote procedure call)  (0) 2022.03.13

댓글