Monday, 13 March 2017

Does (g)rpc support dynamic objects as parameters?

Im writing an rpc function that returns an object from MongoDB where the user can define the returned parameters in the call. The problem is that I don't seem to find how I define a 'dynamic' object in the proto. All the examples I find are based on that the object is static, and all the paramters are set in the .proto file. The rpc function looks like this:

function fetch(call, callback) {
  ObjectSvc.getById(call.request.id, call.request.parameter, (err, object) => {
    if (err) {
      log.error(err);
      callback(err);
    } else {
      callback(null, object);
    }
  });
};

My .proto definition looks like this:

package object;

service Object{
  rpc fetch (queryReq) returns (objectRes) {}
}

message queryReq {
  string id = 1;

  string parameters = 2;
}

message objectRes {
  // what should I write here?
}

I know I can set the result as a string, and just stringify the whole object, but is that the intended way? Are there not a better (and more 'correct') way to do this? Or shall I return a (maybe) big ass string each time? It seems a bit suboptimal.



via David Gustavsson

No comments:

Post a Comment