【问题标题】:Can Protobuf Field Mask be applied to grpc only case?Protobuf 字段掩码可以仅应用于 grpc 的情况吗?
【发布时间】:2018-12-27 16:54:28
【问题描述】:

我们以官方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,我可以这样使用掩码吗:

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

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

【问题讨论】:

    标签: rest go grpc grpc-gateway


    【解决方案1】:

    根据protobufdocs

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

    当您应用字段掩码时,它会指定要在 gRPC 请求中更新哪些特定字段。请记住,如果您在 HTTP 请求中使用它,我收集到的就是您正在执行的操作,那么它必须是 PATCH 请求而不是 PUT 请求。

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

    请注意,这是基于 protobufs 文档的,我可能会完全误解,所以请谨慎对待。

    【讨论】:

    • 我只使用 grpc,我在问题名称中提到了它:)
    • 哎呀,我好像错过了。字段掩码最终对您有用吗?
    猜你喜欢
    • 2021-07-14
    • 2012-11-20
    • 2018-10-24
    • 1970-01-01
    • 2020-10-23
    • 2011-11-09
    • 2022-12-16
    • 2016-09-10
    • 2017-12-19
    相关资源
    最近更新 更多