当前位置 : 主页 > 大数据 > 区块链 >

休息 – Protobuf Field Mask可以应用于grpc唯一的情况吗?

来源:互联网 收集:自由互联 发布时间:2021-06-22
让我们从官方 doc中拿这个例子: // 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
让我们从官方 doc中拿这个例子:

// 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;
}

如果我没有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;
}

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

根据protobuf docs:

Field Masks in Update Operations
A field mask in update operations specifies which fields of the targeted resource are going to be updated. The API is required to only change the values of the fields as specified in the mask and leave the others untouched. If a resource is passed in to describe the updated values, the API ignores the values of all fields not covered by the mask.

应用字段掩码时,它指定要在gRPC请求中更新的特定字段.请记住,如果您在HTTP请求中使用它,从我收集的是您正在做的事情,必须是PATCH请求而不是PUT请求.

例如,假设您有一个名为Books的声明,其属性为:title为字符串,year_published为int32,作者为作者.声明Author将字段first_name作为字符串,将last_name作为字符串.如果要使用author.first_name的字段掩码,则只能更新book中author的first_name字段.

请注意,这是基于protobufs文档,我可能完全曲解,所以请耐心等待.

网友评论