Search code examples
arraysprotocol-buffers

How to return more than one array in Protobuf service rpc


I have the following schema in my .proto file:

service DownloadOctants {
    rpc GetOctants (OctantsQuery) returns (OctantsResponse) {
    }
}

message OctantsQuery {
    repeated int32 octants = 1;
}
message OctantsResponse {
    repeated Mesh meshes = 1;
}
message Mesh {
    repeated float vertices = 1;
    repeated int32 faces = 2;
}

Mesh consists of two arrays. Is that possible?

If no, how else should I design my scheme?

If yes, how do I access the values of the arrays vertices and faces?

I saw the sample program at https://grpc.io/docs/languages/node/basics/ :

var call = client.listFeatures(rectangle);
  call.on('data', function(feature) {
      console.log('Found feature called "' + feature.name + '" at ' +
          feature.location.latitude/COORD_FACTOR + ', ' +
          feature.location.longitude/COORD_FACTOR);
  });

This assumes that listFeatures returns an array, but only one array. It seems to be more challenging to process two or more arrays.


Solution

  • Q1 "Mesh consists of two arrays"

    Yes, it's possible as you show in your protobuf.

    Q2: "How do I access the value of vertices and faces"

    The sample program (Route Guide) ListFeatures method streams responses and so the implementation is different.

    Your proto returns OctantsResponse which contains 2 (vertices,faces) arrays so you can:

    ...
    octantsQuery = {
      octants = [1,2,4,5,6]
    };
    
    client.getOctants(octantsQuery, (err, octantsResponse) => {
      if (err) {
        console.log(err);
      }
    
      // Iterate over the Meshes
      octantsResponse.meshes.forEach(m => {
        // Iterate over the vertices
        m.vertices.forEach(v => {
          console.log(`vertex: ${v}`);
        });
        // Iterate over the faces
        m.faces.forEach(f => {
          console.log(`face: ${f}`);
        });
      });
    });
    

    It's good practice to include syntax and package definitions in your protos:

    syntax = "proto3"; // Or?
    
    package some-namespace;
    
    ...