From a7a12695a3fc12278ce10808c5be3caa31a6f3e2 Mon Sep 17 00:00:00 2001 From: Geoff Watson Date: Tue, 19 Mar 2024 10:50:49 -0700 Subject: [PATCH] Add map test case --- fmutils.go | 5 ++--- fmutils_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/fmutils.go b/fmutils.go index c942e23..81e08b3 100644 --- a/fmutils.go +++ b/fmutils.go @@ -186,9 +186,8 @@ func (mask NestedMask) overwrite(srcRft, destRft protoreflect.Message) { 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 { - destVal := protoreflect.ValueOf(mv) - destMap.Set(mk, destVal) - mi.overwrite(i, destVal.Message()) + destMap.Set(mk, mv) + mi.overwrite(i, mv.Message()) } else { destMap.Set(mk, mv) } diff --git a/fmutils_test.go b/fmutils_test.go index 0993be7..e655687 100644 --- a/fmutils_test.go +++ b/fmutils_test.go @@ -883,6 +883,47 @@ func TestOverwrite(t *testing.T) { }, }, }, + { + name: "overwrite map with message values", + paths: []string{"attributes.src1.tags", "attributes.src2.tags"}, + src: &testproto.Profile{ + User: nil, + Attributes: map[string]*testproto.Attribute{ + "src1": { + Tags: map[string]string{"key1": "value1", "key2": "value2"}, + }, + "src2": { + Tags: map[string]string{"key3": "value3"}, + }, + }, + }, + dest: &testproto.Profile{ + User: &testproto.User{ + Name: "name", + }, + Attributes: map[string]*testproto.Attribute{ + "dest1": { + Tags: map[string]string{"key4": "value5"}, + }, + }, + }, + want: &testproto.Profile{ + User: &testproto.User{ + Name: "name", + }, + Attributes: map[string]*testproto.Attribute{ + "src1": { + Tags: map[string]string{"key1": "value1", "key2": "value2"}, + }, + "src2": { + Tags: map[string]string{"key3": "value3"}, + }, + "dest1": { + Tags: map[string]string{"key4": "value5"}, + }, + }, + }, + }, { name: "overwrite repeated message fields", paths: []string{"gallery.path"},