Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Protobuf Field Mask be applied to grpc only case?

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.

like image 689
Sergii Getman Avatar asked Dec 27 '18 16:12

Sergii Getman


People also ask

What is a field mask protobuf?

Field masks are used to specify a subset of fields that should be returned by a get operation or modified by an update operation. Field masks also have a custom JSON encoding (see below).

Why gRPC is not widely used?

Weaknesses of gRPCAs gRPC heavily uses HTTP/2, it is impossible to call a gRPC service from a web browser directly. No modern browser provides the control needed over web requests to support a gRPC client.

What is field mask?

Field masks are a way for API callers to list the fields that a request should return or update. Using fields masks allows the API to avoid unnecessary work and improves performance. Field masks are used for both read and update methods in the Slides API.

What is Google protobuf empty?

protobuf. Empty states: A generic empty message that you can re-use to avoid defining duplicated empty messages in your APIs. A typical example is to use it as the request or the response type of an API method.


1 Answers

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.

like image 158
akrantz01 Avatar answered Sep 20 '22 17:09

akrantz01