Skip to content

Commit

Permalink
👔 up: maputil - fix a method name error, add tests for MergeMultiSMap
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 5, 2023
1 parent b73b8e9 commit e0bbc86
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions maputil/data.go
Expand Up @@ -20,8 +20,8 @@ func (d Data) Has(key string) bool {
return ok
}

// IsEmtpy if the data map
func (d Data) IsEmtpy() bool {
// IsEmpty if the data map
func (d Data) IsEmpty() bool {
return len(d) == 0
}

Expand Down
2 changes: 1 addition & 1 deletion maputil/data_test.go
Expand Up @@ -24,7 +24,7 @@ func TestData_usage(t *testing.T) {
assert.True(t, mp.Has("k1"))
assert.True(t, mp.Bool("k3"))
assert.False(t, mp.Bool("k4"))
assert.False(t, mp.IsEmtpy())
assert.False(t, mp.IsEmpty())
assert.Eq(t, 23, mp.Get("k1"))
assert.Eq(t, "b", mp.Get("k5.a"))
assert.Eq(t, 23, mp.Get("anyMp.b"))
Expand Down
24 changes: 22 additions & 2 deletions maputil/maputil.go
Expand Up @@ -16,8 +16,7 @@ const (
KeySepChar = '.'
)

// SimpleMerge simple merge two data map by string key.
// will merge the src to dst map
// SimpleMerge simple merge two data map by string key. will merge the src to dst map
func SimpleMerge(src, dst map[string]any) map[string]any {
if len(src) == 0 {
return dst
Expand Down Expand Up @@ -66,6 +65,27 @@ func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]stri
return dst
}

// MergeMultiSMap quick merge multi string-map data.
func MergeMultiSMap(mps ...map[string]string) map[string]string {
newMp := make(map[string]string)
for _, mp := range mps {
for k, v := range mp {
newMp[k] = v
}
}
return newMp
}

// FilterSMap filter empty elem for the string map.
func FilterSMap(sm map[string]string) map[string]string {
for key, val := range sm {
if val == "" {
delete(sm, key)
}
}
return sm
}

// MakeByPath build new value by key names
//
// Example:
Expand Down
5 changes: 5 additions & 0 deletions maputil/maputil_test.go
Expand Up @@ -49,6 +49,11 @@ func TestMergeStringMap(t *testing.T) {

ret = maputil.MergeSMap(nil, map[string]string{"a": "v1"}, true)
assert.Eq(t, map[string]string{"a": "v1"}, ret)

ret = maputil.MergeMultiSMap(maputil.SMap{"a": "v1"}, maputil.SMap{"a": "v2"}, maputil.SMap{"b": "v3"})
assert.ContainsKeys(t, ret, []string{"a", "b"})

assert.Eq(t, map[string]string{"a": "v1"}, maputil.FilterSMap(map[string]string{"a": "v1", "b": ""}))
}

func TestMakeByPath(t *testing.T) {
Expand Down

0 comments on commit e0bbc86

Please sign in to comment.