Can Protobuf Field Mask be applied to grpc only case?
Asked Answered
G

1

7

Let's take this example from the official 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;
}

If I don't have a grpc gateway and use grpc only, can I use mask in that way:

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

If so, how should that mask works - filters request? or be applied during db saving and how does it know about db ... So I am a bit confused about using it. In my own grpc sample I see that mask doesn't filter the request.

Gaskill answered 27/12, 2018 at 16:54 Comment(0)
U
5

According to the 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.

When you apply a field mask, it specifies which particular fields to update in a gRPC request. Keep in mind that if you are using it in a HTTP request, which from what I gather is what you are doing, must be a PATCH request rather than a PUT request.

For example, say you have a declaration named Books with the properties: title as a string, year_published as a int32, author as an Author. The declaration Author has the fields first_name as a string, and last_name as a string. If you were to use a field mask of author.first_name, you would only update the first_name field of author in book.

Please note that this is based off of the protobufs documentation and I could be completely misinterpreting, so take it with a grain of salt.

Usk answered 18/1, 2019 at 7:29 Comment(2)
I use grpc only, I mentioned it in question name :)Gaskill
Whoops, I seem to have missed that. Did the field mask end up working for you?Usk

© 2022 - 2024 — McMap. All rights reserved.