登录
首页 >  Golang >  Go问答

可以在仅使用 grpc 的情况下应用 Protobuf Field Mask 吗?

来源:stackoverflow

时间:2024-03-11 18:27:26 210浏览 收藏

一分耕耘,一分收获!既然都打开这篇《可以在仅使用 grpc 的情况下应用 Protobuf Field Mask 吗?》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

让我们以官方文档中的示例为例:

// 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 示例中,我看到 mask 不会过滤请求。


解决方案


根据protobuf docs

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

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

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

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>