google/protocol-buffers

Protocol Buffers : Language Guide (proto3) - Using Other Message Types

C/H 2018. 8. 28. 08:30

Index

Using Other Message Types 기타 메시지 유형 사용

You can use other message types as field types. For example, let's say you wanted to include Result messages in each SearchResponse message – to do this, you can define a Result message type in the same .proto and then specify a field of type Result in SearchResponse:
다른 메시지 유형을 필드 유형으로 사용할 수 있다. 예를 들어 각 SearchResponse 메시지에 결과 메시지를 포함한다고 가정해 보자. – 이렇게 하려면 동일한 .proto에서 결과 메시지 유형을 정의한 다음 SearchResponse에서 결과 유형의 필드를 지정할 수 있다.

message SearchResponse {
  repeated Result results = 1;
}

message Result {
  string url = 1;
  string title = 2;
  repeated string snippets = 3;
}

mporting Definitions 정의 가져오기

In the above example, the Result message type is defined in the same file as SearchResponse – what if the message type you want to use as a field type is already defined in another .proto file?
위의 예제에서 결과 메시지 유형은 SearchResponse와 동일한 파일에 정의되어 있다. - 필드 유형으로 사용하려는 메시지 유형이 다른 .proto 파일에 이미 정의되어 있으면 어떻게 될까?

You can use definitions from other .proto files by importing them. To import another .proto's definitions, you add an import statement to the top of your file:
다른 .proto 파일의 정의를 가져와 사용할 수 있다. 다른 .proto의 정의를 가져오려면 파일 맨 위에 가져오기 문을 추가한다.

import "myproject/other_protos.proto";

By default you can only use definitions from directly imported .proto files. However, sometimes you may need to move a .proto file to a new location. Instead of moving the .proto file directly and updating all the call sites in a single change, now you can put a dummy .proto file in the old location to forward all the imports to the new location using the import public notion. import public dependencies can be transitively relied upon by anyone importing the proto containing the import public statement. For example:
기본적으로 직접 가져온 .proto 파일에서 정의만 사용할 수 있다. 하지만 가끔씩 당신은 새 위치를 가리키는.proto 파일 이동해야 할 수 있다. .proto 파일을 직접 이동하고 모든 통화 사이트를 한 번의 변경으로 업데이트하는 대신, 이제 이전 위치에 더미 .proto 파일을 추가하여 모든 가져오기를 공용 개념을 사용하여 새 위치로 전달할 수 있다. 수입 공공 의존성은 수입 공문을 포함하는 양성자를 수입하는 모든 사람에 의해 과도하게 의존될 수 있다. 예를 들면 다음과 같다.

// new.proto
// All definitions are moved here
// old.proto
// This is the proto that all clients are importing.
import public "new.proto";
import "other.proto";
// client.proto
import "old.proto";
// You use definitions from old.proto and new.proto, but not other.proto

The protocol compiler searches for imported files in a set of directories specified on the protocol compiler command line using the -I/--proto_path flag. If no flag was given, it looks in the directory in which the compiler was invoked. In general you should set the --proto_path flag to the root of your project and use fully qualified names for all imports.
프로토콜 컴파일러는 -I/--proto_path 플래그를 사용하여 프로토콜 컴파일러 명령줄에 지정된 디렉토리 집합에서 가져온 파일을 검색한다. 플래그가 지정되지 않은 경우 컴파일러가 호출된 디렉터리를 찾는다. 일반적으로 --proto_path 플래그를 프로젝트의 루트로 설정하고 모든 가져오기에 대해 정규화된 이름을 사용해야 한다.

Using proto2 Message Types proto2 메시지 유형 사용

It's possible to import proto2 message types and use them in your proto3 messages, and vice versa. However, proto2 enums cannot be used directly in proto3 syntax (it's okay if an imported proto2 message uses them).
proto2 메시지 유형을 가져와 proto3 메시지에서 사용하거나 그 반대로 사용할 수 있다. 그러나 proto2 열거형은 proto3 구문에서 직접 사용할 수 없다(가져온 proto2 메시지가 사용되는 경우 괜찮다).

반응형