PROTOBUF text format analysis map

This answer clearly shows some examples of raw text parsing, but no examples of maps.

If a prototype has:

< p>

map aToB

I guess it is like this:

aToB {
123 : "foo"
}

But it doesn’t work. Does anyone know the exact syntax?

I initially tried to infer from the earlier answer, which led me astray because I mistakenly believed Multiple k/v pairs look like this:

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

This resulted in the following error:

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

Syntax applicable to multiple key-value pairs:

(Note: I am using the “proto3” version of the protocol buffer language)

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

Repeat the map variable after re-reading this relevant portion of the proto3 Map documentation The pattern of the name is more meaningful, which explains that the map is equivalent to defining your own “pair” message type and then marking it as “duplicate”.

A more complete example:

< p>Prototype definition:

syntax = "proto3";
package myproject.testing;

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

message UserCollection {
string descr iption = 20;
// HERE IS THE PROTOBUF MAP-TYPE FIELD:
map users = 10;
}

message TestData {< br /> UserCollection user_collection = 10;
}

The text format in the configuration file (“pbtxt”):

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

C will generate message content programmatically

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;

myproject::testing: :TestData data;
d ata.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));

This answer clearly shows some examples of raw text parsing, But there is no example of a map.

If a prototype has:

map aToB

I Guess it is like this:

aToB {
123: "foo"
}

But it doesn’t work. Anyone Know the exact syntax?

I initially tried to infer from the earlier answer, which led me astray because I mistakenly thought that multiple k/v pairs looked like this: < p>

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

This resulted in the following error:

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

Syntax for multiple key-value pairs:

(Note: I am using “proto3” of the protocol buffer language Version)

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

It makes more sense to repeat the pattern of map variable names after re-reading this relevant portion of the proto3 Map documentation, which explains the map equivalent Define your own “right” message type, and then mark it as “repeat”.

A more complete example:

Prototype definition:

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 users = 10;
}

message TestData {
UserCollection user_collection = 10;
}

In the configuration file Text format (“pbtxt”):

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

C will programmatically generate message content

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(< br /> pair_ty pe(std::string("user_1234"), user_1));
data.mutable_user_collection()->mutable_users()->insert(
pair_type(std::string("user_9b27"), user_2));

Leave a Comment

Your email address will not be published.