在proto3中的rpc语法是否允许空请求或响应?
我想要以下的等价物:
rpc Logout;
rpc Status returns (Status);
rpc Log (LogData);
或者我应该创建一个空类型?
message Null {};
rpc Logout (Null) returns (Null);
rpc Status (Null) returns (Status);
rpc Log (LogData) returns (Null);
在proto3中的rpc语法是否允许空请求或响应?
我想要以下的等价物:
rpc Logout;
rpc Status returns (Status);
rpc Log (LogData);
或者我应该创建一个空类型?
message Null {};
rpc Logout (Null) returns (Null);
rpc Status (Null) returns (Status);
rpc Log (LogData) returns (Null);
当前回答
你也可以在Reply结构中使用另一个bool属性。像这样
message Reply {
string result = 1;
bool found = 2;
}
所以如果你没有找到结果或者发生了一些错误,你可以从服务类this返回
return new Reply()
{
Found = false
};
其他回答
Kenton的评论是合理的建议:
... 作为开发人员,我们真的不善于猜测我们未来可能想要什么。因此,为了安全起见,我建议始终为每个方法定义自定义参数和结果类型,即使它们是空的。
回答我自己的问题:
通过查看默认的原型文件,我遇到了Empty,这完全像我上面建议的Null类型:)
该文件摘录如下:
// A generic empty message that you can re-use to avoid defining duplicated
// empty messages in your APIs. A typical example is to use it as the request
// or the response type of an API method. For instance:
//
// service Foo {
// rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
// }
//
message Empty {
}
你也可以在Reply结构中使用另一个bool属性。像这样
message Reply {
string result = 1;
bool found = 2;
}
所以如果你没有找到结果或者发生了一些错误,你可以从服务类this返回
return new Reply()
{
Found = false
};
你也可以使用预定义:
import "google/protobuf/empty.proto";
package MyPackage;
service MyService {
rpc Check(google.protobuf.Empty) returns (google.protobuf.Empty) {}
}