Skip to content

Commit 0256169

Browse files
authored
Add KeyValueStringSlice and KeyValueStringSliceWithFormat methods (#65)
1 parent f016e49 commit 0256169

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

collections/maps.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package collections
22

3-
import "sort"
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
const (
9+
DefaultKeyValueStringSliceFormat = "%s=%s"
10+
)
411

512
// Merge all the maps into one. Sadly, Go has no generics, so this is only defined for string to interface maps.
6-
func MergeMaps(maps ... map[string]interface{}) map[string]interface{} {
13+
func MergeMaps(maps ...map[string]interface{}) map[string]interface{} {
714
out := map[string]interface{}{}
815

916
for _, currMap := range maps {
@@ -26,4 +33,23 @@ func Keys(m map[string]string) []string {
2633
sort.Strings(out)
2734

2835
return out
29-
}
36+
}
37+
38+
// KeyValueStringSlice returns a string slice with key=value items, sorted alphabetically
39+
func KeyValueStringSlice(m map[string]string) []string {
40+
return KeyValueStringSliceWithFormat(m, DefaultKeyValueStringSliceFormat)
41+
}
42+
43+
// KeyValueStringSliceWithFormat returns a string slice using the specified format, sorted alphabetically.
44+
// The format should consist of at least two '%s' string verbs.
45+
func KeyValueStringSliceWithFormat(m map[string]string, format string) []string {
46+
out := []string{}
47+
48+
for key, value := range m {
49+
out = append(out, fmt.Sprintf(format, key, value))
50+
}
51+
52+
sort.Strings(out)
53+
54+
return out
55+
}

collections/maps_test.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package collections
22

33
import (
4+
"fmt"
45
"testing"
6+
57
"github.com/stretchr/testify/assert"
6-
"fmt"
78
)
89

910
func TestMergeMapsNoMaps(t *testing.T) {
@@ -123,7 +124,7 @@ func TestKeys(t *testing.T) {
123124
t.Parallel()
124125

125126
testCases := []struct {
126-
input map[string]string
127+
input map[string]string
127128
expected []string
128129
}{
129130
{map[string]string{}, []string{}},
@@ -137,4 +138,44 @@ func TestKeys(t *testing.T) {
137138
assert.Equal(t, testCase.expected, actual)
138139
})
139140
}
140-
}
141+
}
142+
143+
func TestKeyValueStringSlice(t *testing.T) {
144+
t.Parallel()
145+
146+
testCases := []struct {
147+
input map[string]string
148+
expected []string
149+
}{
150+
{map[string]string{"a": "foo"}, []string{"a=foo"}},
151+
{map[string]string{"a": "foo", "b": "bar", "c": "baz"}, []string{"a=foo", "b=bar", "c=baz"}},
152+
}
153+
154+
for _, testCase := range testCases {
155+
t.Run(fmt.Sprintf("%v", testCase.input), func(t *testing.T) {
156+
actual := KeyValueStringSlice(testCase.input)
157+
assert.Equal(t, testCase.expected, actual)
158+
})
159+
}
160+
}
161+
162+
func TestKeyValueStringSliceWithFormat(t *testing.T) {
163+
t.Parallel()
164+
165+
testCases := []struct {
166+
input map[string]string
167+
format string
168+
expected []string
169+
}{
170+
{map[string]string{"a": "foo"}, "%s='%s'", []string{"a='foo'"}},
171+
{map[string]string{"a": "foo"}, "%s%s", []string{"afoo"}},
172+
{map[string]string{"a": "foo", "b": "bar", "c": "baz"}, "%s=%s", []string{"a=foo", "b=bar", "c=baz"}},
173+
}
174+
175+
for _, testCase := range testCases {
176+
t.Run(fmt.Sprintf("%v", testCase.input), func(t *testing.T) {
177+
actual := KeyValueStringSliceWithFormat(testCase.input, testCase.format)
178+
assert.Equal(t, testCase.expected, actual)
179+
})
180+
}
181+
}

0 commit comments

Comments
 (0)