How to return a simple boolean value in ProtoBuffer?

Viewed 11497

in my proto file, I define a service interface:

syntax = "proto3";

package mynamespace;

import "google/protobuf/empty.proto";

service MyService {
    rpc isTokenValid (TokenRequest) returns (TokenResponse) {
    }
}

message TokenRequest {
    string token = 1;
}

message TokenResponse {
    bool valid = 1;
}

The above works well, however, I think the TokenResponse is ugly. the bool valid = 1 is redundant, ideally it should be like the following

rpc isTokenValid (TokenRequest) returns (BooleanResponse) {
}

But I didn't figure out how to write proto file like that, can any expert share some best practice on that?

Thanks in advance!

Updates:

How to return an array directly? For example, this is my code:

service MyService {
    rpc arrayResponse (TokenRequest) returns (ArrayResponse) {}
}

message ArrayResponse {
    repeated Data data = 1;
}

message Data {
    string field1 = 1;
    string field2 = 2;
}

I think this is ugly, how to refactor in the correct google way?

Thanks!

1 Answers

Why not just use the predefined BoolValue as specified in Google's wrappers.proto for your response?

Something like:

syntax = "proto3";

package mynamespace;

import "google/protobuf/wrappers.proto";

service MyService {
    rpc isTokenValid (TokenRequest) returns (google.protobuf.BoolValue) {
    }
}

message TokenRequest {
    string token = 1;
}
Related