Protobuf Field Mask可以应用于grpc唯一案例吗?

Ser*_*man 5 rest go grpc grpc-gateway

让我们从官方文档中拿这个例子:

// Updates a book.
rpc UpdateBook(UpdateBookRequest) returns (Book) {
  // Update maps to HTTP PATCH. Resource name is mapped to a URL path.
  // Resource is contained in the HTTP request body.
  option (google.api.http) = {
    // Note the URL template variable which captures the resource name of the
    // book to update.
    patch: "/v1/{book.name=shelves/*/books/*}"
    body: "book"
  };
}

message UpdateBookRequest {
  // The book resource which replaces the resource on the server.
  Book book = 1;

  // The update mask applies to the resource. For the `FieldMask` definition,
  // see https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#fieldmask
  FieldMask update_mask = 2;
}
Run Code Online (Sandbox Code Playgroud)

如果我没有grpc网关并且只使用grpc,我可以这样使用mask:

// Updates a book.
rpc UpdateBook(UpdateBookRequest) returns (Book);

message UpdateBookRequest {
  // The book resource which replaces the resource on the server.
  Book book = 1;

  // The update mask applies to the resource. For the `FieldMask` definition,
  // see https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#fieldmask
  FieldMask update_mask = 2;
}
Run Code Online (Sandbox Code Playgroud)

如果是这样,该掩码应如何工作 - 过滤请求?或者在db保存期间应用它以及它如何知道db ...所以我对使用它有点困惑.在我自己的grpc示例中,我看到该掩码不会过滤请求.

akr*_*ntz 5

根据 protobuf文档

更新操作中的字段掩码
更新操作中的字段掩码指定目标资源的哪些字段将被更新。API 只需要更改掩码中指定的字段值,而其他字段保持不变。如果传入资源来描述更新的值,则 API 会忽略掩码未覆盖的所有字段的值。

当您应用字段掩码时,它指定要在 gRPC 请求中更新哪些特定字段。请记住,如果您在 HTTP 请求中使用它(根据我收集的信息,您正在执行的操作),则必须是 PATCH 请求而不是 PUT 请求。

例如,假设您有一个Books以以下属性命名的声明:title作为字符串、year_published作为 int32、author作为作者。该声明Author具有字符串形式的字段first_namelast_name字符串形式的字段。如果您要使用 的字段掩码author.first_name,则只需更新infirst_name的字段。authorbook

请注意,这是基于 protobufs 文档的,我可能会完全误解,所以请持保留态度。