flatmap: add Contains

This commit is contained in:
Mitchell Hashimoto 2014-07-14 21:40:29 -07:00
parent 19aaabeecc
commit 50db4deb16
2 changed files with 44 additions and 0 deletions

View File

@ -12,6 +12,17 @@ import (
// otherwise noted.
type Map map[string]string
// Contains returns true if the map contains the given key.
func (m Map) Contains (key string) bool {
for _, k := range m.Keys() {
if k == key {
return true
}
}
return false
}
// Delete deletes a key out of the map with the given prefix.
func (m Map) Delete(prefix string) {
for k, _ := range m {

View File

@ -6,6 +6,39 @@ import (
"testing"
)
func TestMapContains(t *testing.T) {
cases := []struct {
Input map[string]string
Key string
Result bool
}{
{
Input: map[string]string{
"foo": "bar",
"bar": "nope",
},
Key: "foo",
Result: true,
},
{
Input: map[string]string{
"foo": "bar",
"bar": "nope",
},
Key: "baz",
Result: false,
},
}
for i, tc := range cases {
actual := Map(tc.Input).Contains(tc.Key)
if actual != tc.Result {
t.Fatalf("case %d bad: %#v", i, tc.Input)
}
}
}
func TestMapDelete(t *testing.T) {
m := Flatten(map[string]interface{}{
"foo": "bar",