如何将 python 字典放入 protobuf 消息中?

how to put a python dictionary in a protobuf message?

假设我们有这个 Json blob:

{
  "thing": {
    "x": 1,
    "str": "hello,
    "params": {
      "opaque": "yes",
      "unknown": 1,
      "more": ...
    }
  }
}

params 的内容未知。我们所知道的是它是一本字典。 我们如何定义一个可以解析它的 protobuf 消息?

// file: thing.proto
message Thing {
    uint32 x = 1;
    string str = 2;
    WhatGoesHere? params = 3;
}

[编辑] 根据评论移动解决方案以回答。

解决方案:使用 google 提供的消息。

// file: solution.proto
import "google/protobuf/struct.proto";

message Solution1 {
    uint32 x = 1;
    string str = 2;
    google.protobuf.Struct params = 3;
}

message Solution2 {
    uint32 x = 1;
    string str = 2;
    map<string, google.protobuf.Value> params = 3;
}