google/protocol-buffers

Protocol Buffers : Language Guide (proto3) - Maps

C/H 2018. 9. 4. 08:30

Index

Maps

If you want to create an associative map as part of your data definition, protocol buffers provides a handy shortcut syntax:
데이터 정의의 일부로 연관 지도를 작성하려는 경우 프로토콜 버퍼는 다음과 같은 편리한 바로 가기 구문을 제공한다.

map<key_type, value_type> map_field = N;

...where the key_type can be any integral or string type (so, any scalar type except for floating point types and bytes). Note that enum is not a valid key_type. The value_type can be any type except another map.
...여기서 key_type은 적분 또는 문자열 유형일 수 있다(따라서 부동 소수점 유형 및 바이트를 제외한 스칼라 유형). Enum은 올바른 key_type이 아니다. value_type은 다른 맵을 제외한 모든 유형일 수 있다.

So, for example, if you wanted to create a map of projects where each Project message is associated with a string key, you could define it like this:
예를 들어 각 프로젝트 메시지가 문자열 키와 연결된 프로젝트 맵을 생성하려면 다음과 같이 정의할 수 있다.

map<string, Project> projects = 3;
  • Map fields cannot be repeated.
    맵 필드를 반복할 수 없다.
  • Wire format ordering and map iteration ordering of map values is undefined, so you cannot rely on your map items being in a particular order.
    지도 값의 와이어 형식 순서와 지도 반복 순서가 정의되지 않았으므로 특정 순서로 있는 지도 항목에 의존할 수 없다.
  • When generating text format for a .proto, maps are sorted by key. Numeric keys are sorted numerically.
    .proto의 텍스트 형식을 생성할 때 맵은 키별로 정렬된다. 숫자 키는 숫자로 정렬된다.
  • When parsing from the wire or when merging, if there are duplicate map keys the last key seen is used. When parsing a map from text format, parsing may fail if there are duplicate keys.
    와이어에서 파싱하거나 병합할 때 중복 맵 키가 있는 경우 마지막으로 표시되는 키가 사용된다. 텍스트 형식에서 지도를 구문 분석할 때 중복 키가 있는 경우 파싱이 실패할 수 있다.
  • If you provide a key but no value for a map field, the behavior when the field is serialized is language-dependent. In C++, Java, and Python the default value for the type is serialized, while in other languages nothing is serialized.
    지도 필드에 키는 제공하지만 값은 제공하지 않는 경우 필드가 직렬화되었을 때의 동작은 언어에 따라 달라진다. C++, Java 및 Python에서는 해당 유형의 기본값이 직렬화되지만 다른 언어에서는 직렬화된 값이 없다.

The generated map API is currently available for all proto3 supported languages. You can find out more about the map API for your chosen language in the relevant API reference.
생성된 지도 API는 현재 모든 proto3 지원 언어에 사용할 수 있다. 선택한 언어에 대한 지도 API에 대한 자세한 내용은 관련 API 참조에서 확인할 수 있다.

Backwards compatibility 역호환성

The map syntax is equivalent to the following on the wire, so protocol buffers implementations that do not support maps can still handle your data:
맵 구문은 와이어의 다음 내용과 동일하므로 맵을 지원하지 않는 프로토콜 버퍼 구현은 여전히 데이터를 처리할 수 있다.

message MapFieldEntry {
    key_type key = 1;
    value_type value = 2;
}
  
repeated MapFieldEntry map_field = N;

Any protocol buffers implementation that supports maps must both produce and accept data that can be accepted by the above definition.
맵을 지원하는 프로토콜 버퍼 구현은 위의 정의에서 수용할 수 있는 데이터를 생성 및 허용해야 한다.

반응형