2014-07-01 22:25:54 +02:00
|
|
|
package flatmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-02-23 19:03:59 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/hil"
|
2014-07-01 22:25:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExpand(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Map map[string]string
|
|
|
|
Key string
|
|
|
|
Output interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
Key: "foo",
|
|
|
|
Output: "bar",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"foo.#": "2",
|
|
|
|
"foo.0": "one",
|
|
|
|
"foo.1": "two",
|
|
|
|
},
|
|
|
|
Key: "foo",
|
|
|
|
Output: []interface{}{
|
|
|
|
"one",
|
|
|
|
"two",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"foo.#": "1",
|
|
|
|
"foo.0.name": "bar",
|
|
|
|
"foo.0.port": "3000",
|
|
|
|
"foo.0.enabled": "true",
|
|
|
|
},
|
|
|
|
Key: "foo",
|
|
|
|
Output: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "bar",
|
2014-07-24 20:40:46 +02:00
|
|
|
"port": "3000",
|
2014-07-01 22:25:54 +02:00
|
|
|
"enabled": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-07-08 22:57:30 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
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{}{
|
2014-07-24 20:40:46 +02:00
|
|
|
"1",
|
|
|
|
"2",
|
2014-07-08 22:57:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-12-16 22:33:39 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"list_of_map.#": "2",
|
|
|
|
"list_of_map.0.%": "1",
|
|
|
|
"list_of_map.0.a": "1",
|
|
|
|
"list_of_map.1.%": "2",
|
|
|
|
"list_of_map.1.b": "2",
|
|
|
|
"list_of_map.1.c": "3",
|
|
|
|
},
|
|
|
|
Key: "list_of_map",
|
|
|
|
Output: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"a": "1",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"b": "2",
|
|
|
|
"c": "3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"map_of_list.%": "2",
|
|
|
|
"map_of_list.list2.#": "1",
|
|
|
|
"map_of_list.list2.0": "c",
|
|
|
|
"map_of_list.list1.#": "2",
|
|
|
|
"map_of_list.list1.0": "a",
|
|
|
|
"map_of_list.list1.1": "b",
|
|
|
|
},
|
|
|
|
Key: "map_of_list",
|
|
|
|
Output: map[string]interface{}{
|
|
|
|
"list1": []interface{}{"a", "b"},
|
|
|
|
"list2": []interface{}{"c"},
|
|
|
|
},
|
|
|
|
},
|
2017-01-04 22:11:46 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"set.#": "3",
|
|
|
|
"set.1234": "a",
|
|
|
|
"set.1235": "b",
|
|
|
|
"set.1236": "c",
|
|
|
|
},
|
|
|
|
Key: "set",
|
|
|
|
Output: []interface{}{"a", "b", "c"},
|
|
|
|
},
|
2017-02-23 19:03:59 +01:00
|
|
|
|
2017-03-01 19:28:02 +01:00
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"computed_set.#": "1",
|
|
|
|
"computed_set.~1234.a": "a",
|
|
|
|
"computed_set.~1234.b": "b",
|
|
|
|
"computed_set.~1234.c": "c",
|
|
|
|
},
|
|
|
|
Key: "computed_set",
|
|
|
|
Output: []interface{}{
|
|
|
|
map[string]interface{}{"a": "a", "b": "b", "c": "c"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2017-02-23 19:03:59 +01:00
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"struct.#": "1",
|
|
|
|
"struct.0.name": "hello",
|
|
|
|
"struct.0.rules.#": hil.UnknownValue,
|
|
|
|
},
|
|
|
|
Key: "struct",
|
|
|
|
Output: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "hello",
|
|
|
|
"rules": hil.UnknownValue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-04-14 22:32:30 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"struct.#": "1",
|
|
|
|
"struct.0.name": "hello",
|
|
|
|
"struct.0.set.#": "0",
|
|
|
|
"struct.0.set.0.key": "value",
|
|
|
|
},
|
|
|
|
Key: "struct",
|
|
|
|
Output: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "hello",
|
|
|
|
"set": []interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-04-13 17:11:41 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Map: map[string]string{
|
|
|
|
"empty_map_of_sets.%": "0",
|
|
|
|
"empty_map_of_sets.set1.#": "0",
|
|
|
|
"empty_map_of_sets.set1.1234": "x",
|
|
|
|
},
|
|
|
|
Key: "empty_map_of_sets",
|
|
|
|
Output: map[string]interface{}{},
|
|
|
|
},
|
2014-07-01 22:25:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2017-04-13 17:11:41 +02:00
|
|
|
t.Run(tc.Key, func(t *testing.T) {
|
|
|
|
actual := Expand(tc.Map, tc.Key)
|
|
|
|
if !reflect.DeepEqual(actual, tc.Output) {
|
|
|
|
t.Errorf(
|
|
|
|
"Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
tc.Key,
|
|
|
|
tc.Map,
|
|
|
|
actual,
|
|
|
|
tc.Output)
|
|
|
|
}
|
|
|
|
})
|
2014-07-01 22:25:54 +02:00
|
|
|
}
|
|
|
|
}
|