diff --git a/utils/map.go b/utils/map.go index aa80a67e5..b04feaf5c 100644 --- a/utils/map.go +++ b/utils/map.go @@ -183,3 +183,18 @@ func MapKeysReplace(m map[string]struct{}, old, new string) map[string]struct{} return m } */ + +// Used to merge multiple maps (eg: output of struct having ExtraFields) +func MergeMapsStringIface(mps ...map[string]interface{}) (outMp map[string]interface{}) { + outMp = make(map[string]interface{}) + for i, mp := range mps { + if i == 0 { + outMp = mp + continue + } + for k, v := range mp { + outMp[k] = v + } + } + return +} diff --git a/utils/map_test.go b/utils/map_test.go index a8d629492..b9076f88b 100644 --- a/utils/map_test.go +++ b/utils/map_test.go @@ -1,6 +1,9 @@ package utils -import "testing" +import ( + "reflect" + "testing" +) func TestStringMapParse(t *testing.T) { sm := ParseStringMap("1;2;3;4") @@ -31,3 +34,24 @@ func TestStringMapCompare(t *testing.T) { t.Error("Error detecting missing: ", sm) } } + +func TestMapMergeMapsStringIface(t *testing.T) { + mp1 := map[string]interface{}{ + "Hdr1": "Val1", + "Hdr2": "Val2", + "Hdr3": "Val3", + } + mp2 := map[string]interface{}{ + "Hdr3": "Val4", + "Hdr4": "Val4", + } + eMergedMap := map[string]interface{}{ + "Hdr1": "Val1", + "Hdr2": "Val2", + "Hdr3": "Val4", + "Hdr4": "Val4", + } + if mergedMap := MergeMapsStringIface(mp1, mp2); !reflect.DeepEqual(eMergedMap, mergedMap) { + t.Errorf("Expecting: %+v, received: %+v", eMergedMap, mergedMap) + } +}