我可以使用空请求或响应定义 grpc 调用吗?
Can I define a grpc call with a null request or response?
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);
Kenton 下面的评论是中肯的建议:
... we as developers are really bad at guessing what we might want in the future. So I recommend being safe by always defining custom params and results types for every method, even if they are empty.
回答我自己的问题:
查看默认原型文件,我发现 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;
}
所以如果您找不到结果或发生了一些错误,您可以 return 从服务 class this
return new Reply()
{
Found = false
};
您也可以使用预定义的:
import "google/protobuf/empty.proto";
package MyPackage;
service MyService {
rpc Check(google.protobuf.Empty) returns (google.protobuf.Empty) {}
}
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);
Kenton 下面的评论是中肯的建议:
... we as developers are really bad at guessing what we might want in the future. So I recommend being safe by always defining custom params and results types for every method, even if they are empty.
回答我自己的问题:
查看默认原型文件,我发现 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;
}
所以如果您找不到结果或发生了一些错误,您可以 return 从服务 class this
return new Reply()
{
Found = false
};
您也可以使用预定义的:
import "google/protobuf/empty.proto";
package MyPackage;
service MyService {
rpc Check(google.protobuf.Empty) returns (google.protobuf.Empty) {}
}