我有一个可以返回结果或null的服务,所以我将其定义为:
syntax = "proto3";
package package;
import "google/protobuf/empty.proto";
service A {
rpc getById (ASearchRequest) returns (AResponse) {
}
rpc getById (ASearchRequest) returns (google.protobuf.Empty) {
}
}
message AResponse {
string _id = 1;
string key = 2;
string name = 3;
}
message ASearchRequest {
required string id = 1;
}
但是编译器不会执行它,那么如何处理 ProtoBuf 中的可为空的响应类型呢?
一种可能的解决方案是定义一个消息AnOptionalResponse
,其中包含一个类型为AResponse
的可选成员。 然后,返回AnOptionalResponse
,它是空消息或包含可选AResponse
proto 成员的消息。
或者只是将AResponse
中的所有字段设置为可选字段。