Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf RPC Service method without parameters

I'm trying to describe an RPC service using Google's Protocol Buffers

service WhoamiService {   rpc WhoAreYou() returns (Whoami) {} }  message Whoami {   optional bytes request_id = 1;   optional string hostname = 2;   optional string message = 3; } 

When I try to compile this definition, I get an error Expected type name pointing to the WhoAreYou() piece.

It works fine if I replace WhoAreYou() with WhoAreYou(Whoami), but in this case, the method does not need any parameters.. Is there a way to do this or is it simply not supported?

like image 933
Michael Robinson Avatar asked Apr 16 '15 22:04

Michael Robinson


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 ...

Is repeated optional protobuf?

A repeated field is inherently optional : you just don't add any values. As for com. google. protobuf.

How do I set default value in protobuf?

For bool s, the default value is false. For numeric types, the default value is zero. For enums , the default value is the first value listed in the enum's type definition. This means care must be taken when adding a value to the beginning of an enum value list.


2 Answers

You have to specify an input type. If you don't want the method to take any parameters, define an empty message type, like:

message WhoAreYouParams {} 

The reason this is required is so that if you later need to add an optional parameter, you can do so without breaking existing code.

like image 56
Kenton Varda Avatar answered Oct 02 '22 21:10

Kenton Varda


You can specify google.protobuf.Empty instead of your own empty message. Example:

rpc WhoAreYou(google.protobuf.Empty) returns (Whoami) { } 

Don't forget to import appropriate proto file:

import "google/protobuf/empty.proto"; 
like image 36
Alex Avatar answered Oct 02 '22 21:10

Alex