-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support FieldMask in Updates (#1197)
* feat: support FieldMask in Updates * fix index out of bounds * simplify * refactor for oneofs
- Loading branch information
Showing
5 changed files
with
160 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,16 @@ package services | |
import ( | ||
"context" | ||
"encoding/base64" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/googleapis/gapic-showcase/server" | ||
pb "github.com/googleapis/gapic-showcase/server/genproto" | ||
"google.golang.org/genproto/protobuf/field_mask" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/types/known/fieldmaskpb" | ||
) | ||
|
||
func Test_User_lifecycle(t *testing.T) { | ||
|
@@ -257,19 +259,26 @@ func Test_Get_deleted(t *testing.T) { | |
} | ||
|
||
func Test_Update_fieldmask(t *testing.T) { | ||
first := &pb.User{DisplayName: "Ekko", Email: "[email protected]"} | ||
second := &pb.User{DisplayName: "Foo", Email: "[email protected]"} | ||
paths := []string{"display_name"} | ||
s := NewIdentityServer() | ||
_, err := s.UpdateUser( | ||
created, err := s.CreateUser( | ||
context.Background(), | ||
&pb.UpdateUserRequest{ | ||
User: nil, | ||
UpdateMask: &field_mask.FieldMask{Paths: []string{"email"}}, | ||
}) | ||
status, _ := status.FromError(err) | ||
if status.Code() != codes.Unimplemented { | ||
t.Errorf( | ||
"Update: Want error code %d got %d", | ||
codes.Unimplemented, | ||
status.Code()) | ||
&pb.CreateUserRequest{User: first}) | ||
if err != nil { | ||
t.Errorf("Create: unexpected err %+v", err) | ||
} | ||
second.Name = created.GetName() | ||
|
||
got, err := s.UpdateUser( | ||
context.Background(), | ||
&pb.UpdateUserRequest{User: second, UpdateMask: &fieldmaskpb.FieldMask{Paths: paths}}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if diff := cmp.Diff(got.GetDisplayName(), second.GetDisplayName()); diff != "" { | ||
t.Errorf("Using update_mask %s, got(-),want(+):\n%s", strings.Join(paths, ","), diff) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package services | ||
|
||
import ( | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
func strContains(haystack []string, needle string) bool { | ||
for _, s := range haystack { | ||
if s == needle { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// applyFieldMask applies the values from the src message to the values of the | ||
// dst message according to the contents of the given field mask paths. | ||
// If paths is empty/nil, or contains *, it is considered a full update. | ||
// | ||
// TODO: Does not support nested message paths. Currently only used with flat | ||
// resource messages. | ||
func applyFieldMask(src, dst protoreflect.Message, paths []string) { | ||
fullUpdate := len(paths) == 0 || strContains(paths, "*") | ||
|
||
fields := dst.Descriptor().Fields() | ||
for i := 0; i < fields.Len(); i++ { | ||
field := fields.Get(i) | ||
isOneof := field.ContainingOneof() != nil && !field.ContainingOneof().IsSynthetic() | ||
|
||
// Set field in dst with value from src, skipping true oneofs, while | ||
// handling proto3_optional fields represented as synthetic oneofs. | ||
if (fullUpdate || strContains(paths, string(field.Name()))) && !isOneof { | ||
dst.Set(field, src.Get(field)) | ||
} | ||
} | ||
|
||
oneofs := dst.Descriptor().Oneofs() | ||
for i := 0; i < oneofs.Len(); i++ { | ||
oneof := oneofs.Get(i) | ||
// Skip proto3_optional synthetic oneofs. | ||
if oneof.IsSynthetic() { | ||
continue | ||
} | ||
|
||
setOneof := src.WhichOneof(oneof) | ||
if setOneof == nil && fullUpdate { | ||
// Full update with no field set in this oneof of | ||
// src means clear all fields for this oneof in dst. | ||
fields := oneof.Fields() | ||
for j := 0; j < fields.Len(); j++ { | ||
dst.Clear(fields.Get(j)) | ||
} | ||
} else if setOneof != nil && (fullUpdate || strContains(paths, string(setOneof.Name()))) { | ||
// Full update or targeted updated with a field set in this oneof of | ||
// src means set that field for the same oneof in dst, which implicitly | ||
// clears any previously set field for this oneof. | ||
dst.Set(setOneof, src.Get(setOneof)) | ||
} | ||
} | ||
} |