【问题标题】:How to set repeated of repeated elements in proto3如何在proto3中设置重复元素的重复
【发布时间】:2020-04-28 20:48:13
【问题描述】:

我的 .proto 中有这两个定义:

// all the shards on a server
message ConfigEntry {
  repeated Shard shards = 2;
  string server = 3;
}

// information on all the groups
message QueryResponse {
  repeated ConfigEntry config = 1;
}

在我的 c++ 文件中,我尝试在 QueryResponse 中设置这张地图:

std::map<std::string, std::vector<shard_t>> servers;

我找不到任何方法将我的地图中的值设置为在 QueryResponse 中进行配置,有什么想法吗?

【问题讨论】:

标签: c++ protocol-buffers grpc proto protobuf-c


【解决方案1】:

应该是这样的:

    for (auto const &item : servers)
  {
    auto c = response->add_config();
    c->set_server(item.first);
    for (auto s : item.second)
    {
      Shard *sh = c->add_shards();
      sh->set_lower(s.lower);
      sh->set_upper(s.upper);
    }
  }

我们调用 add_config() 来获取一个 ConfigEntry,我们将设置它的服务器名称,然后遍历向量并分别添加每个分片。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-04
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多