From 50db4deb16cf40f34737f81b8e44f0edb1d5c978 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 14 Jul 2014 21:40:29 -0700 Subject: [PATCH] flatmap: add Contains --- flatmap/map.go | 11 +++++++++++ flatmap/map_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/flatmap/map.go b/flatmap/map.go index 3035baba7..2bc8c761e 100644 --- a/flatmap/map.go +++ b/flatmap/map.go @@ -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 { diff --git a/flatmap/map_test.go b/flatmap/map_test.go index 1021ecf1d..e3b4cb1bd 100644 --- a/flatmap/map_test.go +++ b/flatmap/map_test.go @@ -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",