flatmap: deeper nesting tests

This commit is contained in:
Mitchell Hashimoto 2014-07-08 13:57:30 -07:00
parent 47468c32a4
commit 1277c324d0
3 changed files with 48 additions and 4 deletions

View File

@ -61,12 +61,15 @@ func expandMap(m map[string]string, prefix string) map[string]interface{} {
key := k[len(prefix):]
idx := strings.Index(key, ".")
if idx == -1 {
idx = len(k)
if idx != -1 {
key = key[:idx]
}
if _, ok := result[key]; ok {
continue
}
// It contains a period, so it is a more complex structure
result[key] = Expand(m, k[:idx])
result[key] = Expand(m, k[:len(prefix)+len(key)])
}
return result

View File

@ -49,12 +49,32 @@ func TestExpand(t *testing.T) {
},
},
},
{
Map: map[string]string{
"foo.#": "1",
"foo.0.name": "bar",
"foo.0.ports.#": "2",
"foo.0.ports.0": "1",
"foo.0.ports.1": "2",
},
Key: "foo",
Output: []interface{}{
map[string]interface{}{
"name": "bar",
"ports": []interface{}{
1,
2,
},
},
},
},
}
for _, tc := range cases {
actual := Expand(tc.Map, tc.Key)
if !reflect.DeepEqual(actual, tc.Output) {
t.Fatalf(
t.Errorf(
"Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
tc.Key,
tc.Map,

View File

@ -52,6 +52,27 @@ func TestFlatten(t *testing.T) {
"foo.0.enabled": "true",
},
},
{
Input: map[string]interface{}{
"foo": []map[interface{}]interface{}{
map[interface{}]interface{}{
"name": "bar",
"ports": []string{
"1",
"2",
},
},
},
},
Output: map[string]string{
"foo.#": "1",
"foo.0.name": "bar",
"foo.0.ports.#": "2",
"foo.0.ports.0": "1",
"foo.0.ports.1": "2",
},
},
}
for _, tc := range cases {