gpt4 book ai didi

arrays - 如何将 JSON 数组建模为 protobuf 定义

转载 作者:IT王子 更新时间:2023-10-29 01:38:29 30 4
gpt4 key购买 nike

我正在使用 protobuf 在 golang 中编写一个新服务。我想在 .proto 文件中对以下请求 JSON 进行建模。

[
{
"var": ["myVariable1","myVariable2"],
"key1": 123123,
"key2": 1122,
"key3": "abcd-0101"
},
{
"var": ["myVariable1"],
"key1": 123124,
"key2": 1123,
"key3": "abcd-0102"
},
]

目前有两个问题:

  • 事先不知道每个数组元素中的键,因此我无法在 .proto 文件中创建消息并使其重复。我需要保留它的 map
  • 我无法为 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 编写消息:

message RequestMessage {
string var = 0;
double key1 = 1;
double key2 = 2;
string key3 = 3;
}

此外,您可以创建另一个包含 RequestMessage

数组的消息
message Request {
repeated RequestMessage request = 0;
}

关于arrays - 如何将 JSON 数组建模为 protobuf 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46945555/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com