flatmap: deeper nesting tests
This commit is contained in:
parent
47468c32a4
commit
1277c324d0
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue