Skip to content

Commit

Permalink
enhance: maputil: add NewMapStringSlice()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Mar 29, 2022
1 parent 7a37e07 commit 8b9bb7d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions type/maputil/map_string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (

type MapStringSlice map[string][]string

func NewMapStringSlice() MapStringSlice {
return MapStringSlice{}
}

func (mss MapStringSlice) Add(key, value string) {
if _, ok := mss[key]; !ok {
mss[key] = []string{value}
Expand Down
36 changes: 36 additions & 0 deletions type/maputil/map_string_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package maputil

import (
"strings"
"testing"
)

var mssTests = []struct {
data map[string][]string
sortTestKey string
sortTestValues []string
}{
{
data: map[string][]string{
"foo": {"foo", "bar", "baz"}},
sortTestKey: "foo",
sortTestValues: []string{"bar", "baz", "foo"}},
}

func TestMapStringSlice(t *testing.T) {
for _, tt := range mssTests {
mss := NewMapStringSlice()
for key, vals := range tt.data {
for _, val := range vals {
mss.Add(key, val)
}
}
mss.Sort(true)
valsWant := strings.Join(tt.sortTestValues, ",")
valsTry := strings.Join(mss[tt.sortTestKey], ",")
if valsTry != valsWant {
t.Errorf("maputil.MapStringSlice.Sort() Fail: want [%s], got [%s]",
valsWant, valsTry)
}
}
}

0 comments on commit 8b9bb7d

Please sign in to comment.