Skip to content

Commit

Permalink
Additional tests and minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Watson committed Mar 19, 2024
1 parent a7a1269 commit 4300539
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
12 changes: 10 additions & 2 deletions fmutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,22 @@ func (mask NestedMask) overwrite(srcRft, destRft protoreflect.Message) {
} else if srcFD.IsMap() && srcFD.Kind() == protoreflect.MessageKind {
srcMap := srcRft.Get(srcFD).Map()
destMap := destRft.Get(srcFD).Map()
if !destMap.IsValid() {
destRft.Set(srcFD, protoreflect.ValueOf(srcMap))
destMap = destRft.Get(srcFD).Map()
}
srcMap.Range(func(mk protoreflect.MapKey, mv protoreflect.Value) bool {
if mi, ok := submask[mk.String()]; ok {
if i, ok := mv.Interface().(protoreflect.Message); ok && len(mi) > 0 {
destMap.Set(mk, mv)
mi.overwrite(i, mv.Message())
newVal := protoreflect.ValueOf(i.New())
destMap.Set(mk, newVal)
mi.overwrite(mv.Message(), newVal.Message())
} else {

destMap.Set(mk, mv)
}
} else {
destMap.Clear(mk)
}
return true
})
Expand Down
65 changes: 61 additions & 4 deletions fmutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ func TestOverwrite(t *testing.T) {
},
{
name: "overwrite map with message values",
paths: []string{"attributes.src1.tags", "attributes.src2.tags"},
paths: []string{"attributes.src1.tags.key1", "attributes.src2"},
src: &testproto.Profile{
User: nil,
Attributes: map[string]*testproto.Attribute{
Expand All @@ -903,7 +903,7 @@ func TestOverwrite(t *testing.T) {
},
Attributes: map[string]*testproto.Attribute{
"dest1": {
Tags: map[string]string{"key4": "value5"},
Tags: map[string]string{"key4": "value4"},
},
},
},
Expand All @@ -913,13 +913,13 @@ func TestOverwrite(t *testing.T) {
},
Attributes: map[string]*testproto.Attribute{
"src1": {
Tags: map[string]string{"key1": "value1", "key2": "value2"},
Tags: map[string]string{"key1": "value1"},
},
"src2": {
Tags: map[string]string{"key3": "value3"},
},
"dest1": {
Tags: map[string]string{"key4": "value5"},
Tags: map[string]string{"key4": "value4"},
},
},
},
Expand Down Expand Up @@ -1037,6 +1037,63 @@ func TestOverwrite(t *testing.T) {
},
},
},
{
name: "overwrite repeated message fields to empty list",
paths: []string{"gallery.path"},
src: &testproto.Profile{
User: &testproto.User{
UserId: 567,
Name: "different-name",
},
Photo: &testproto.Photo{
Path: "photo-path",
},
LoginTimestamps: []int64{1, 2, 3},
Attributes: map[string]*testproto.Attribute{
"src": {},
},
Gallery: []*testproto.Photo{
{
PhotoId: 123,
Path: "test-path-1",
Dimensions: &testproto.Dimensions{
Width: 345,
Height: 456,
},
},
{
PhotoId: 234,
Path: "test-path-2",
Dimensions: &testproto.Dimensions{
Width: 3456,
Height: 4567,
},
},
{
PhotoId: 345,
Path: "test-path-3",
Dimensions: &testproto.Dimensions{
Width: 34567,
Height: 45678,
},
},
},
},
dest: &testproto.Profile{},
want: &testproto.Profile{
Gallery: []*testproto.Photo{
{
Path: "test-path-1",
},
{
Path: "test-path-2",
},
{
Path: "test-path-3",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 4300539

Please sign in to comment.