GRPC:未知的JSON作为输入和输出

发布于 2025-02-13 05:58:17 字数 1468 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2025-02-20 05:58:17

没有现有的实用程序来执行此转换,但是编写一个函数并不是那么复杂。例如,我在 @grpc/grpc-js-xds图书馆

function validateValue(obj: any): Value {
  if (Array.isArray(obj)) {
    return {
      kind: 'listValue',
      listValue: {
        values: obj.map((value) => validateValue(value)),
      },
    };
  } else {
    switch (typeof obj) {
      case 'boolean':
        return {
          kind: 'boolValue',
          boolValue: obj,
        };
      case 'number':
        return {
          kind: 'numberValue',
          numberValue: obj,
        };
      case 'string':
        return {
          kind: 'stringValue',
          stringValue: obj,
        };
      case 'object':
        if (obj === null) {
          return {
            kind: 'nullValue',
            nullValue: 'NULL_VALUE',
          };
        } else {
          return {
            kind: 'structValue',
            structValue: getStructFromJson(obj),
          };
        }
      default:
        throw new Error(`Could not handle struct value of type ${typeof obj}`);
    }
  }
}

function getStructFromJson(obj: any): Struct {
  if (typeof obj !== 'object' || obj === null) {
    throw new Error('Invalid JSON object for Struct field');
  }
  const fields: { [key: string]: Value } = {};
  for (const [fieldName, value] of Object.entries(obj)) {
    fields[fieldName] = validateValue(value);
  }
  return {
    fields,
  };
}

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:

function validateValue(obj: any): Value {
  if (Array.isArray(obj)) {
    return {
      kind: 'listValue',
      listValue: {
        values: obj.map((value) => validateValue(value)),
      },
    };
  } else {
    switch (typeof obj) {
      case 'boolean':
        return {
          kind: 'boolValue',
          boolValue: obj,
        };
      case 'number':
        return {
          kind: 'numberValue',
          numberValue: obj,
        };
      case 'string':
        return {
          kind: 'stringValue',
          stringValue: obj,
        };
      case 'object':
        if (obj === null) {
          return {
            kind: 'nullValue',
            nullValue: 'NULL_VALUE',
          };
        } else {
          return {
            kind: 'structValue',
            structValue: getStructFromJson(obj),
          };
        }
      default:
        throw new Error(`Could not handle struct value of type ${typeof obj}`);
    }
  }
}

function getStructFromJson(obj: any): Struct {
  if (typeof obj !== 'object' || obj === null) {
    throw new Error('Invalid JSON object for Struct field');
  }
  const fields: { [key: string]: Value } = {};
  for (const [fieldName, value] of Object.entries(obj)) {
    fields[fieldName] = validateValue(value);
  }
  return {
    fields,
  };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文