GoLang 中具有通用原型请求数据的 GRPC 服务
我有 3 个原型如下:
1 - record.proto
message Record {
int64 primaryKey = 1;
int64 createdAt = 2;
int64 updatedAt = 3;
}
2 - user.proto
import "record.proto";
message User {
Record record = 31;
string name = 32;
string email = 33;
string password = 34;
}
3 - requests.proto
import "record.proto";
message Permissions{
Record record = 31;
string permission1= 32;
string permission2= 33;
}
问题1: 有没有一种方法可以在 golang 中实现 grpc 服务器和客户端,专门将 Record 作为请求,但同时支持后面的两种类型。即用户和权限。 像这样:
service DatabaseService {
rpc Create(Record) returns (Response);
}
这样我就可以发送 grpc 客户端请求,如下所示:
Create(User) returns (Response);
Create(Permissions) returns (Response);
I have 3 protos as follow:
1 - record.proto
message Record {
int64 primaryKey = 1;
int64 createdAt = 2;
int64 updatedAt = 3;
}
2 - user.proto
import "record.proto";
message User {
Record record = 31;
string name = 32;
string email = 33;
string password = 34;
}
3 - permissions.proto
import "record.proto";
message Permissions{
Record record = 31;
string permission1= 32;
string permission2= 33;
}
Question1:
Is there a way to implement a grpc server and client in golang that takes specifically Record as request but entertains both later types. i.e User and Permissions.
Something like:
service DatabaseService {
rpc Create(Record) returns (Response);
}
So that I can send grpc client request as both follow:
Create(User) returns (Response);
Create(Permissions) returns (Response);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
oneof
,它允许客户端发送User
或Permissions
。请参阅 https://developers.google.com/protocol-buffers/docs/proto3 #oneof
因此客户端可以在请求中填写其中任何一个。
You can use
oneof
which allows the client to send eitherUser
orPermissions
.Refer https://developers.google.com/protocol-buffers/docs/proto3#oneof
So client can fill any of them in the request.