【发布时间】:2017-01-21 07:43:07
【问题描述】:
我正在尝试使用 gRPC 构建一个简单的 CRUD 服务,但我不断发现自己创建的消息重叠很大。
最好用一个例子来描述:
message Todo {
// id is only available for a persisted entity in database.
string id = 1;
string content = 2;
// this is only available for users with admin role.
string secret_content = 3;
}
service Todos {
rpc CreateTodo(CreateRequest) returns (CreateResponse) {}
rpc ReadTodo(ReadRequest) returns (ReadResponse) {}
}
message CreateRequest {
// this todo is not supposed to have id,
// should I create another version of Todo without an id field?
Todo todo
}
message CreateResponse {
// this todo will always have an id.
Todo todo = 1;
}
message ReadRequest {
string id = 1;
}
message ReadResponse {
// this todo should only have the secret_content field if the
// user is authenticated as an admin, if not, the field should not
// fallback to the zero value, the whole field must be missing.
Todo todo = 1;
}
这是用 gRPC 构建类似 CRUD 的资源的好方法吗?也就是说,有一条消息 (Todo) 代表资源,并将此消息包装在每个操作的响应/请求类型中。
Todo 类型的消息是否应该包含所有请求/响应的所有字段,而不设置每个不使用的字段?
【问题讨论】:
标签: protocol-buffers grpc