Skip to content
Advertisement

gRPC: Unknown JSON as input and output

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.

Advertisement

Answer

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,
  };
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement