protobuf3 map not modifiable?

Viewed 108

protobuf message definition: with protobuf-3.6.1

syntax = "proto3";

message UserMessage {
    message Int64List {
        repeated int64 value = 1;
    };
    int64 uid = 1;
    int64 gid = 2;
    repeated int32 cat = 3;
    map<string, int32> simple_map = 4;
    map<string, Int64List> map_list = 5;
};

cpp code:

UserMessage message;
auto m = message.mutable_simple_map();
(*m)["aaa"] = 2;
(*m)["bbb"] = 5;
{
    std::cout << message.ShortDebugString() << std::endl;
    auto a = message.simple_map();
    for (auto& b : a) {
        std::cout << "new  " << b.first << ":" << b.second << std::endl;
    }
}
(*m)["aaa"] = 20;
(*m)["bcb"] = 5;
{
    std::cout << message.ShortDebugString() << std::endl;
    auto a = message.simple_map();
    for (auto& b : a) {
        std::cout << "new  " << b.first << ":" << b.second << std::endl;
    }
}

output:

simple_map { key: "aaa" value: 2 } simple_map { key: "bbb" value: 5 }
new  bbb:5
new  aaa:2
simple_map { key: "aaa" value: 2 } simple_map { key: "bbb" value: 5 }
new  bbb:5
new  aaa:20
new  bcb:5

I declared a Message with map struct, and insert some pairs and print. Then I updated some of them and print again. The result is strange for me. Why the map in message not updated? But standalone print is correct?

0 Answers
Related