【问题标题】:How to create a CRUD with gRPC service without much repetition?如何在没有太多重复的情况下使用 gRPC 服务创建 CRUD?
【发布时间】: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


    【解决方案1】:

    Todo 类型的消息是否应该包含所有请求/响应的所有字段,而不设置每个不使用的字段?

    是的,这似乎是一个合理的设计。在 protobuf v2 中,您可以将这些字段标记为 optional 以使其更易于理解。但是在 v3 中,默认情况下所有字段都是可选的。

    【讨论】:

      猜你喜欢
      • 2020-07-18
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 2023-03-31
      • 2013-09-20
      • 2013-12-16
      • 2021-07-27
      • 2015-01-23
      相关资源
      最近更新 更多