GRPC:未知的JSON作为输入和输出
我是GRPC的新手,我正在尝试曝光GRPC服务器,其中RPC事先没有知道形状。
要求是特定字段可以具有其中的任何值。
前任: file.proto
syntax = "proto3";
import "google/protobuf/struct.proto";
string corr_id = 1;
google.protobuf.Struct message = 2;
当我浏览文档时,我们无法在不知道之前创建GRPC服务器。为了使该结构工作,我正在进行以下转换并使其正常工作。
Actual JSON Object:
{
name: "wer",
age: 28,
hobbies: ["Cricket", "Tea"],
key: {
value1: 89,
value2: "sure next"
}
}
gRPC Server Requires this below conversion:
{
"fields": {
"name": {
"stringValue": "wer"
},
"age": {
"numberValue": 28
},
"hobbies": {
"listValue": {
"fields": [
"Cricket",
"Tea"
]
}
},
"key": {
"structValue": {
"fields": {
"value1": {
"numberValue": 89
},
"value2": {
"stringValue": "sure next"
}
}
}
}
}
}
是否可以使用此转换?还是我可以在没有转换的情况下实现此目标的其他方法来使GRPC服务器解析请求?
想象一下我要为以下JSON做什么?
{
name: "Sathish",
age: 28,
address: [
{
is_primary: true,
name: "address1"
},
{
is_primary: false,
name: "address2"
}
],
hobbies: ["Cricket", "Tea"],
movies: {
liked: ["Re"],
unliked: ["Te"]
},
key: {
value1: 89,
value2: "sure next"
}
}
注意:我目前暂时使用@grpc/grpc-js
软件包。
I'm new to gRPC, I'm trying to expose gRPC server in which and rpc does not have known shape before hand.
Requirement is that particular field can have any value present in it.
Ex:
file.proto
syntax = "proto3";
import "google/protobuf/struct.proto";
string corr_id = 1;
google.protobuf.Struct message = 2;
When i went through the docs there is no way we can create gRPC server without knowing the fields before hand. For this struct to work, i'm doing the following conversion and making it work.
Actual JSON Object:
{
name: "wer",
age: 28,
hobbies: ["Cricket", "Tea"],
key: {
value1: 89,
value2: "sure next"
}
}
gRPC Server Requires this below conversion:
{
"fields": {
"name": {
"stringValue": "wer"
},
"age": {
"numberValue": 28
},
"hobbies": {
"listValue": {
"fields": [
"Cricket",
"Tea"
]
}
},
"key": {
"structValue": {
"fields": {
"value1": {
"numberValue": 89
},
"value2": {
"stringValue": "sure next"
}
}
}
}
}
}
Is there util available already to do this conversion ? OR is there any other way i can achieve this without the conversion to make gRPC server parse the request ?
Imagine what i have to do for the below json ?
{
name: "Sathish",
age: 28,
address: [
{
is_primary: true,
name: "address1"
},
{
is_primary: false,
name: "address2"
}
],
hobbies: ["Cricket", "Tea"],
movies: {
liked: ["Re"],
unliked: ["Te"]
},
key: {
value1: 89,
value2: "sure next"
}
}
Note: I'm using @grpc/grpc-js
package alone for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有现有的实用程序来执行此转换,但是编写一个函数并不是那么复杂。例如,我在
@grpc/grpc-js-xds
图书馆:There is not an existing utility for performing this transformation, but it is not that complicated to write a function to do it. For example, I use the following code in the
@grpc/grpc-js-xds
library: