flatmap: add richer API to resulting flattened map
This commit is contained in:
parent
cb52983c84
commit
16485ba3ca
|
@ -12,14 +12,14 @@ import (
|
||||||
// any combination of those together.
|
// any combination of those together.
|
||||||
//
|
//
|
||||||
// See the tests for examples of what inputs are turned into.
|
// See the tests for examples of what inputs are turned into.
|
||||||
func Flatten(thing map[string]interface{}) map[string]string {
|
func Flatten(thing map[string]interface{}) Map {
|
||||||
result := make(map[string]string)
|
result := make(map[string]string)
|
||||||
|
|
||||||
for k, raw := range thing {
|
for k, raw := range thing {
|
||||||
flatten(result, k, reflect.ValueOf(raw))
|
flatten(result, k, reflect.ValueOf(raw))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return Map(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func flatten(result map[string]string, prefix string, v reflect.Value) {
|
func flatten(result map[string]string, prefix string, v reflect.Value) {
|
||||||
|
|
|
@ -77,7 +77,7 @@ func TestFlatten(t *testing.T) {
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
actual := Flatten(tc.Input)
|
actual := Flatten(tc.Input)
|
||||||
if !reflect.DeepEqual(actual, tc.Output) {
|
if !reflect.DeepEqual(actual, Map(tc.Output)) {
|
||||||
t.Fatalf(
|
t.Fatalf(
|
||||||
"Input:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
|
"Input:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
|
||||||
tc.Input,
|
tc.Input,
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue