Skip to content

Commit 50db4de

Browse files
committed
flatmap: add Contains
1 parent 19aaabe commit 50db4de

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎flatmap/map.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import (
1212
// otherwise noted.
1313
type Map map[string]string
1414

15+
// Contains returns true if the map contains the given key.
16+
func (m Map) Contains (key string) bool {
17+
for _, k := range m.Keys() {
18+
if k == key {
19+
return true
20+
}
21+
}
22+
23+
return false
24+
}
25+
1526
// Delete deletes a key out of the map with the given prefix.
1627
func (m Map) Delete(prefix string) {
1728
for k, _ := range m {

‎flatmap/map_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@ import (
66
"testing"
77
)
88

9+
func TestMapContains(t *testing.T) {
10+
cases := []struct {
11+
Input map[string]string
12+
Key string
13+
Result bool
14+
}{
15+
{
16+
Input: map[string]string{
17+
"foo": "bar",
18+
"bar": "nope",
19+
},
20+
Key: "foo",
21+
Result: true,
22+
},
23+
24+
{
25+
Input: map[string]string{
26+
"foo": "bar",
27+
"bar": "nope",
28+
},
29+
Key: "baz",
30+
Result: false,
31+
},
32+
}
33+
34+
for i, tc := range cases {
35+
actual := Map(tc.Input).Contains(tc.Key)
36+
if actual != tc.Result {
37+
t.Fatalf("case %d bad: %#v", i, tc.Input)
38+
}
39+
}
40+
}
41+
942
func TestMapDelete(t *testing.T) {
1043
m := Flatten(map[string]interface{}{
1144
"foo": "bar",

0 commit comments

Comments
 (0)