mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Utility to merge maps
This commit is contained in:
15
utils/map.go
15
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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user