【发布时间】:2018-04-07 07:38:58
【问题描述】:
我正在使用 protobuf 在 golang 中编写一个新服务。我想在 .proto 文件中对以下请求 JSON 建模。
[
{
"var": ["myVariable1","myVariable2"],
"key1": 123123,
"key2": 1122,
"key3": "abcd-0101"
},
{
"var": ["myVariable1"],
"key1": 123124,
"key2": 1123,
"key3": "abcd-0102"
},
]
目前有两个问题:
- 事先不知道每个数组元素中的键,因此我无法在 .proto 文件中创建消息并使其重复。我需要保留它的地图
- 我无法对只是一个没有键的数组的 json 建模。每次我这样做时,都会显示以下错误:无法解码请求:json:无法将数组解组为 Go 值
以下是我的 .proto 文件:
syntax = "proto3";
package pb;
import "google/protobuf/empty.proto";
import "google/api/annotations.proto";
service Transmitter {
rpc GetVariables(GetVariablesRequest) returns (GetVariablesResponse) {
option (google.api.http) = {
post: "/api/v1/{Service}/getVars"
body: "*"
};
};
}
message GetVariablesRequest {
string Service = 1;
repeated GetVarInput in = 2;
}
message GetVariablesResponse {
string msg = 1;
}
message GetVarInput {
map<string,string> Input = 2;
}
我尝试使用字节而不是重复的 GetVarInput,但它总是为空。还尝试了 body: "*" 和 body : "in"
请提供一些指示。
【问题讨论】:
-
你不能在你的 json blob 中给数组一些占位符名称吗?例如
{ "data" : [ {...}, {...} ] }
标签: arrays json go protocol-buffers grpc