forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flatmap: add richer API to resulting flattened map
- Loading branch information
Showing
4 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package flatmap | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Map is a wrapper around map[string]string that provides some helpers | ||
// above it that assume the map is in the format that flatmap expects | ||
// (the result of Flatten). | ||
// | ||
// All modifying functions such as Delete are done in-place unless | ||
// otherwise noted. | ||
type Map map[string]string | ||
|
||
// Delete deletes a key out of the map with the given prefix. | ||
func (m Map) Delete(prefix string) { | ||
for k, _ := range m { | ||
match := k == prefix | ||
if !match && !strings.HasPrefix(k, prefix) { | ||
continue | ||
} | ||
if k[len(prefix):len(prefix)+1] != "." { | ||
continue | ||
} | ||
|
||
delete(m, k) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package flatmap | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestMapDelete(t *testing.T) { | ||
m := Flatten(map[string]interface{}{ | ||
"foo": "bar", | ||
"routes": []map[string]string{ | ||
map[string]string{ | ||
"foo": "bar", | ||
}, | ||
}, | ||
}) | ||
|
||
m.Delete("routes") | ||
|
||
expected := Map(map[string]string{"foo": "bar"}) | ||
if !reflect.DeepEqual(m, expected) { | ||
t.Fatalf("bad: %#v", m) | ||
} | ||
} |