terraform/helper/schema/field_reader_map_test.go

124 lines
2.3 KiB
Go
Raw Normal View History

package schema
import (
"reflect"
"testing"
)
func TestMapFieldReader_impl(t *testing.T) {
var _ FieldReader = new(MapFieldReader)
}
func TestMapFieldReader(t *testing.T) {
testFieldReader(t, func(s map[string]*Schema) FieldReader {
return &MapFieldReader{
Schema: s,
Map: BasicMapReader(map[string]string{
"bool": "true",
"int": "42",
2015-01-11 01:04:01 +01:00
"float": "3.1415",
"string": "string",
"list.#": "2",
"list.0": "foo",
"list.1": "bar",
"listInt.#": "2",
"listInt.0": "21",
"listInt.1": "42",
core: Use .% instead of .# for maps in state The flatmapped representation of state prior to this commit encoded maps and lists (and therefore by extension, sets) with a key corresponding to the number of elements, or the unknown variable indicator under a .# key and then individual items. For example, the list ["a", "b", "c"] would have been encoded as: listname.# = 3 listname.0 = "a" listname.1 = "b" listname.2 = "c" And the map {"key1": "value1", "key2", "value2"} would have been encoded as: mapname.# = 2 mapname.key1 = "value1" mapname.key2 = "value2" Sets use the hash code as the key - for example a set with a (fictional) hashcode calculation may look like: setname.# = 2 setname.12312512 = "value1" setname.56345233 = "value2" Prior to the work done to extend the type system, this was sufficient since the internal representation of these was effectively the same. However, following the separation of maps and lists into distinct first-class types, this encoding presents a problem: given a state file, it is impossible to tell the encoding of an empty list and an empty map apart. This presents problems for the type checker during interpolation, as many interpolation functions will operate on only one of these two structures. This commit therefore changes the representation in state of maps to use a "%" as the key for the number of elements. Consequently the map above will now be encoded as: mapname.% = 2 mapname.key1 = "value1" mapname.key2 = "value2" This has the effect of an empty list (or set) now being encoded as: listname.# = 0 And an empty map now being encoded as: mapname.% = 0 Therefore we can eliminate some nasty guessing logic from the resource variable supplier for interpolation, at the cost of having to migrate state up front (to follow in a subsequent commit). In order to reduce the number of potential situations in which resources would be "forced new", we continue to accept "#" as the count key when reading maps via helper/schema. There is no situation under which we can allow "#" as an actual map key in any case, as it would not be distinguishable from a list or set in state.
2016-06-05 10:34:43 +02:00
"map.%": "2",
"map.foo": "bar",
"map.bar": "baz",
"set.#": "2",
"set.10": "10",
"set.50": "50",
"setDeep.#": "2",
"setDeep.10.index": "10",
"setDeep.10.value": "foo",
"setDeep.50.index": "50",
"setDeep.50.value": "bar",
"mapInt.%": "2",
"mapInt.one": "1",
"mapInt.two": "2",
"mapIntNestedSchema.%": "2",
"mapIntNestedSchema.one": "1",
"mapIntNestedSchema.two": "2",
"mapFloat.%": "1",
"mapFloat.oneDotTwo": "1.2",
"mapBool.%": "2",
"mapBool.True": "true",
"mapBool.False": "false",
}),
}
})
}
func TestMapFieldReader_extra(t *testing.T) {
r := &MapFieldReader{
Schema: map[string]*Schema{
"mapDel": &Schema{Type: TypeMap},
"mapEmpty": &Schema{Type: TypeMap},
},
2015-01-09 03:02:19 +01:00
Map: BasicMapReader(map[string]string{
"mapDel": "",
core: Use .% instead of .# for maps in state The flatmapped representation of state prior to this commit encoded maps and lists (and therefore by extension, sets) with a key corresponding to the number of elements, or the unknown variable indicator under a .# key and then individual items. For example, the list ["a", "b", "c"] would have been encoded as: listname.# = 3 listname.0 = "a" listname.1 = "b" listname.2 = "c" And the map {"key1": "value1", "key2", "value2"} would have been encoded as: mapname.# = 2 mapname.key1 = "value1" mapname.key2 = "value2" Sets use the hash code as the key - for example a set with a (fictional) hashcode calculation may look like: setname.# = 2 setname.12312512 = "value1" setname.56345233 = "value2" Prior to the work done to extend the type system, this was sufficient since the internal representation of these was effectively the same. However, following the separation of maps and lists into distinct first-class types, this encoding presents a problem: given a state file, it is impossible to tell the encoding of an empty list and an empty map apart. This presents problems for the type checker during interpolation, as many interpolation functions will operate on only one of these two structures. This commit therefore changes the representation in state of maps to use a "%" as the key for the number of elements. Consequently the map above will now be encoded as: mapname.% = 2 mapname.key1 = "value1" mapname.key2 = "value2" This has the effect of an empty list (or set) now being encoded as: listname.# = 0 And an empty map now being encoded as: mapname.% = 0 Therefore we can eliminate some nasty guessing logic from the resource variable supplier for interpolation, at the cost of having to migrate state up front (to follow in a subsequent commit). In order to reduce the number of potential situations in which resources would be "forced new", we continue to accept "#" as the count key when reading maps via helper/schema. There is no situation under which we can allow "#" as an actual map key in any case, as it would not be distinguishable from a list or set in state.
2016-06-05 10:34:43 +02:00
"mapEmpty.%": "0",
2015-01-09 03:02:19 +01:00
}),
}
cases := map[string]struct {
Addr []string
Out interface{}
OutOk bool
OutComputed bool
OutErr bool
}{
"mapDel": {
[]string{"mapDel"},
map[string]interface{}{},
true,
false,
false,
},
"mapEmpty": {
[]string{"mapEmpty"},
map[string]interface{}{},
true,
false,
false,
},
}
for name, tc := range cases {
out, err := r.ReadField(tc.Addr)
2015-10-08 14:48:04 +02:00
if err != nil != tc.OutErr {
t.Fatalf("%s: err: %s", name, err)
}
if out.Computed != tc.OutComputed {
t.Fatalf("%s: err: %#v", name, out.Computed)
}
if s, ok := out.Value.(*Set); ok {
// If it is a set, convert to a list so its more easily checked.
out.Value = s.List()
}
if !reflect.DeepEqual(out.Value, tc.Out) {
t.Fatalf("%s: out: %#v", name, out.Value)
}
if out.Exists != tc.OutOk {
t.Fatalf("%s: outOk: %#v", name, out.Exists)
}
}
}