ProtoBuf支持多态吗?

时间:2018-10-23 03:57:38

标签: protocol-buffers

我有一个可以返回结果或为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中处理可为空的响应类型?

1 个答案:

答案 0 :(得分:1)

一个可能的解决方案可能是定义一条消息AnOptionalResponse,其中包含一个类型为AResponse的单个可选成员。然后,返回AnOptionalResponse,它可以是空消息,也可以是包含可选AResponse原型成员的消息。

或者只是将AResponse中的所有字段设为可选。

相关问题