2014-12-19 11:56:46 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMapFieldReader_impl(t *testing.T) {
|
|
|
|
var _ FieldReader = new(MapFieldReader)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapFieldReader(t *testing.T) {
|
2015-01-10 21:42:15 +01:00
|
|
|
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",
|
2015-01-10 21:42:15 +01:00
|
|
|
"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",
|
2015-01-10 21:42:15 +01:00
|
|
|
"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",
|
2016-11-17 21:12:29 +01:00
|
|
|
|
|
|
|
"mapInt.%": "2",
|
|
|
|
"mapInt.one": "1",
|
|
|
|
"mapInt.two": "2",
|
|
|
|
|
|
|
|
"mapFloat.%": "1",
|
|
|
|
"mapFloat.oneDotTwo": "1.2",
|
|
|
|
|
|
|
|
"mapBool.%": "2",
|
|
|
|
"mapBool.True": "true",
|
|
|
|
"mapBool.False": "false",
|
2015-01-10 21:42:15 +01:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapFieldReader_extra(t *testing.T) {
|
2014-12-19 11:56:46 +01:00
|
|
|
r := &MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
2015-04-21 22:11:00 +02:00
|
|
|
"mapDel": &Schema{Type: TypeMap},
|
|
|
|
"mapEmpty": &Schema{Type: TypeMap},
|
2015-01-03 18:13:46 +01:00
|
|
|
},
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-10 00:07:02 +01:00
|
|
|
"mapDel": "",
|
2015-04-21 22:11:00 +02:00
|
|
|
|
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
|
|
|
}),
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2014-12-19 12:15:04 +01:00
|
|
|
Addr []string
|
|
|
|
Out interface{}
|
|
|
|
OutOk bool
|
|
|
|
OutComputed bool
|
|
|
|
OutErr bool
|
2014-12-19 11:56:46 +01:00
|
|
|
}{
|
2015-01-10 00:07:02 +01:00
|
|
|
"mapDel": {
|
|
|
|
[]string{"mapDel"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
2015-04-21 22:11:00 +02:00
|
|
|
|
|
|
|
"mapEmpty": {
|
|
|
|
[]string{"mapEmpty"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
2015-01-03 18:13:46 +01:00
|
|
|
out, err := r.ReadField(tc.Addr)
|
2015-10-08 14:48:04 +02:00
|
|
|
if err != nil != tc.OutErr {
|
2014-12-19 21:47:35 +01:00
|
|
|
t.Fatalf("%s: err: %s", name, err)
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if out.Computed != tc.OutComputed {
|
|
|
|
t.Fatalf("%s: err: %#v", name, out.Computed)
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
2014-12-19 11:56:46 +01:00
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
if s, ok := out.Value.(*Set); ok {
|
2014-12-19 11:56:46 +01:00
|
|
|
// If it is a set, convert to a list so its more easily checked.
|
2014-12-19 21:47:35 +01:00
|
|
|
out.Value = s.List()
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
if !reflect.DeepEqual(out.Value, tc.Out) {
|
|
|
|
t.Fatalf("%s: out: %#v", name, out.Value)
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if out.Exists != tc.OutOk {
|
|
|
|
t.Fatalf("%s: outOk: %#v", name, out.Exists)
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|