2016-12-01 1 views
2

This 대답은 proto 텍스트 구문 분석의 일부 예제를 명확하게 보여 주지만지도에 대한 예제는 없습니다. 프로토가있는 경우protobuf 텍스트 형식 구문 분석 맵

:

map<int32, string> aToB 

내가 좋아하는 뭔가 생각 것 :

aToB { 
    123: "foo" 
} 

을했지만 작동하지 않습니다. 누구든지 정확한 구문을 알고 있습니까?

+1

텍스트 형식으로 인코딩하면 어떻게해야할까요? – jpa

답변

1

내가 잘못 여러 K/V 쌍과 같을 것이다 생각했기 때문에 나는 처음에 잘못된 길로 나를 이끄는 earlier answer에서 외삽 시도 :

aToB {   # (this example has a bug) 
    key: 123 
    value: "foo" 
    key: 876  # WRONG! 
    value: "bar" # NOPE! 
} 

libprotobuf ERROR: Non-repeated field "key" is specified multiple times. 

: 즉, 다음과 같은 오류 주도 0 여러 키 - 값 쌍에 대한

적절한 구문 :

(참고 : 나는 프로토콜 버퍼 언어의 "proto3"버전을 사용하고 있습니다)

aToB { 
    key: 123 
    value: "foo" 
} 
aToB { 
    key: 876   
    value: "bar"  
} 

의 이름을 반복의 패턴 지도 변수는 this relevant portion of the proto3 Map documentation을 다시 읽은 후에 더 의미가 있습니다.지도는 자신의 "쌍"메시지 유형을 정의한 다음 "반복됨"으로 표시하는 것과 같습니다.


더 완벽한 예 :

프로토 정의 :

user_collection { 
    description = "my default users" 
    users { 
    key: "user_1234" 
    value { 
     handle: "winniepoo" 
     paid_membership: true 
    } 
    } 
    users { 
    key: "user_9b27" 
    value { 
     handle: "smokeybear" 
    } 
    } 
} 

C++ 그 것 : config 파일에서

syntax = "proto3"; 
package myproject.testing; 

message UserRecord { 
    string handle = 10; 
    bool paid_membership = 20; 
} 

message UserCollection { 
    string description = 20; 
    // HERE IS THE PROTOBUF MAP-TYPE FIELD: 
    map<string, UserRecord> users = 10; 
} 

message TestData { 
    UserCollection user_collection = 10; 
} 

텍스트 형식 ("pbtxt") 프로그래밍 방식으로 메시지 내용 생성

myproject::testing::UserRecord user_1; 
user_1.set_handle("winniepoo"); 
user_1.set_paid_membership(true); 
myproject::testing::UserRecord user_2; 
user_2.set_handle("smokeybear"); 
user_2.set_paid_membership(false); 

using pair_type = 
    google::protobuf::MapPair<std::string, myproject::testing::UserRecord>; 

myproject::testing::TestData data; 
data.mutable_user_collection()->mutable_users()->insert(
    pair_type(std::string("user_1234"), user_1)); 
data.mutable_user_collection()->mutable_users()->insert(
    pair_type(std::string("user_9b27"), user_2)); 
0

텍스트 형식은 다음과 같습니다

aToB { 
    key: 123 
    value: "foo" 
}