go - 如何在 ProtoBuffers 3 中建模 map<string, map<string, int>>

标签 go protocol-buffers marshalling

我正在使用 Go 实现一个 API 端点,该端点应该返回如下所示的数据:

{
    "object1s": [
        {
            "object2": {
                "key1": {
                    "key3": 1,
                    "key4": 2,
                    "key5": 3
                },
                "key2": {
                    "key3": 4,
                    "key4": 5,
                    "key5": 6
                }
            }
        },
        {
            "object2": {
                "key1": {
                    "key3": 7,
                    "key4": 8,
                    "key5": 9
                },
                "key2": {
                    "key3": 10,
                    "key4": 11,
                    "key5": 12
                }
            }
        }
    ]
}
如何使用 proto3 进行建模?
我有这个:
message SubObject {
  map<string, map<string, int32>> object2 = 1;
}

message ResponseMessage {
  repeated SubObject object1s = 1;
}
但我相信语法 map<string, map<string, int>>是无效的。
那么描述SubObject的正确方式是什么? ?

最佳答案

尚不支持您想要的方式。
现在,唯一的方法是创建一个 message键入以容纳内部 map field 。

message InnerObject {
    map<string, int32> object3 = 1;
}

message SubObject {
    map<string, InnerObject> object2 = 1;
}

message ResponseMessage {
    repeated SubObject object1s = 1;
}
因此,您必须按如下方式修改您的返回数据,
{
    "object1s": [
        {
            "object2": {
                "key1": {
                    "object3": {
                        "key3": 1,
                        "key4": 2
                    }
                }
            }
        }
    ]
}
引用:Issue#4596

关于go - 如何在 ProtoBuffers 3 中建模 map<string, map<string, int>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64022996/

相关文章:

go - sort.Sort 不修改数组

c++ - 使用 Google 的 protobuf 时出错

c# - "Invalid managed/unmanaged type combination."是什么意思?

go - golang 的 Geany 符号列表

css - 模板中的 Golang ttf 字体

python - 有没有办法将 Protocol Buffer 编译成纯 python 代码?

arrays - 如何在protobuf重复字段中发送0?

java - 编码具有对象字段的对象

c# - .NET 值类型在内存中的布局

interface - Go中接口(interface)的使用