如何使用 Protobuf 的反射修改 Map

How to use reflection of Protobuf to modify a Map

我在我的 C++14 项目中使用 Protobuf3。已经有一些函数,其中returns的google::protobuf::Message*s作为一个rpc请求,我需要做的是设置它们的字段。所以需要用到Protobuf3的反射。

这是一个原型文件:

syntax="proto3";
package srv.user;

option cc_generic_services = true;
message BatchGetUserInfosRequest {
    uint64 my_uid = 1;
    repeated uint64 peer_uids = 2;
    map<string, string> infos = 3;
}

message BatchGetUserInfosResponse {
    uint64 my_uid = 1;
    string info = 2;
}

Service UserSrv {
    rpc BatchGetUserInfos(BatchGetUserInfosRequest) returns (BatchGetUserInfosResponse);
};

现在我调用了一个函数,它 returns 一个 google::protobuf::Message*,指向一个对象 BatchGetUserInfosRequest 我尝试设置它的字段。

// msg is a Message*, pointing to an object of BatchGetUserInfosRequest
auto descriptor = msg->GetDescriptor();
auto reflection = msg->GetReflection();
auto field = descriptor->FindFieldByName("my_uid");
reflection->SetUInt64(msg, field, 1234);
auto field2 = descriptor->FindFieldByName("peer_uids");
reflection->GetMutableRepeatedFieldRef<uint64_t>(msg, field2).CopyFrom(peerUids); // peerUids is a std::vector<uint64_t>

如你所见,我可以像上面那样设置my_uidpeer_uids,但是对于字段infos,它是一个google::protobuf::Map,我不知道如何设置反射机制

如果你深入研究 source code,你会发现 proto3 中的 map 是在 RepeatedField 上实现的:

  // Whether the message is an automatically generated map entry type for the
  // maps field.
  //
  // For maps fields:
  //     map<KeyType, ValueType> map_field = 1;
  // The parsed descriptor looks like:
  //     message MapFieldEntry {
  //         option map_entry = true;
  //         optional KeyType key = 1;
  //         optional ValueType value = 2;
  //     }
  //     repeated MapFieldEntry map_field = 1;
  //
  // Implementations may choose not to generate the map_entry=true message, but
  // use a native map in the target language to hold the keys and values.
  // The reflection APIs in such implementations still need to work as
  // if the field is a repeated message field.
  //
  // NOTE: Do not set the option in .proto files. Always use the maps syntax
  // instead. The option should only be implicitly set by the proto compiler
  // parser.
  optional bool map_entry = 7;

protobuf 的测试代码启发,这对我有用:

  BatchGetUserInfosRequest message;
  auto *descriptor = message.GetDescriptor();
  auto *reflection = message.GetReflection();

  const google::protobuf::FieldDescriptor *fd_map_string_string =
      descriptor->FindFieldByName("infos");
  const google::protobuf::FieldDescriptor *fd_map_string_string_key =
      fd_map_string_string->message_type()->map_key();
  const google::protobuf::FieldDescriptor *fd_map_string_string_value =
      fd_map_string_string->message_type()->map_value();

  const google::protobuf::MutableRepeatedFieldRef<google::protobuf::Message>
      mmf_string_string =
          reflection->GetMutableRepeatedFieldRef<google::protobuf::Message>(
              &message, fd_map_string_string);
  std::unique_ptr<google::protobuf::Message> entry_string_string(
      google::protobuf::MessageFactory::generated_factory()
          ->GetPrototype(fd_map_string_string->message_type())
          ->New(message.GetArena()));
  entry_string_string->GetReflection()->SetString(
      entry_string_string.get(), fd_map_string_string->message_type()->field(0),
      "1234");
  entry_string_string->GetReflection()->SetString(
      entry_string_string.get(), fd_map_string_string->message_type()->field(1),
      std::to_string(10));
  mmf_string_string.Add(*entry_string_string);


  std::cout << "1234: " << message.infos().at("1234") << '\n';

输出:

1234: 10