Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an array in Protobuf service rpc

I have the following schema in my .proto file:

service MyService {     rpc GetItem (ItemQuery) returns (Item) {     } }  message ItemQuery {     int id = 1; } message Item {     int id = 1;     string name = 2; } 

Now I want to add another rpc method to return multiple Items. Something like this:

rpc GetItems (ItemsQuery) returns (repeated Item) { } 

Is there a better way to do it than define an Items message?

like image 598
Shoham Avatar asked Apr 02 '17 11:04

Shoham


People also ask

What is RPC in Protobuf?

A RPC is a form of Client-Server Communication that uses a function call rather than a usual HTTP call. It uses IDL (Interface Definition Language) as a form of contract on functions to be called and on the data type. RPC Architecture. If you all haven't realized it yet, the RPC in gRPC stands for Remote Procedure Call ...

What is a service in Protobuf?

Protocol buffers are a mechanism for sending data through the series of tubes known as the Internet. One common use of them is to define gRPC specifications — essentially a form of remote procedure calls. With gRPC service definitions, you create a “service” that has RPC methods.

What is oneof in Protobuf?

Protocol Buffer (Protobuf) provides two simpler options for dealing with values that might be of more than one type. The Any type can represent any known Protobuf message type. And you can use the oneof keyword to specify that only one of a range of fields can be set in any message.


1 Answers

Option 1 - Use stream:

rpc GetItems (ItemsQuery) returns (stream Item) { } 

Option 2 - Set a response message which will use a repeated object:

service MyService {     rpc GetItem (ItemQuery) returns (ItemResponse) {     } }  message ItemQuery {     int id = 1; } message ItemResponse {     repeated Item items = 1; } message Item {     int id = 1;     string name = 2; } 
like image 149
Shoham Avatar answered Sep 16 '22 21:09

Shoham