2014-12-19 12:15:04 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MapFieldReader reads fields out of an untyped map[string]string to
|
|
|
|
// the best of its ability.
|
|
|
|
type MapFieldReader struct {
|
2015-01-09 03:02:19 +01:00
|
|
|
Map MapReader
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema map[string]*Schema
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
func (r *MapFieldReader) ReadField(address []string) (FieldReadResult, error) {
|
2014-12-19 12:15:04 +01:00
|
|
|
k := strings.Join(address, ".")
|
2015-01-09 03:02:19 +01:00
|
|
|
schemaList := addrToSchema(address, r.Schema)
|
|
|
|
if len(schemaList) == 0 {
|
2015-01-03 18:13:46 +01:00
|
|
|
return FieldReadResult{}, nil
|
|
|
|
}
|
2014-12-19 12:15:04 +01:00
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
schema := schemaList[len(schemaList)-1]
|
2014-12-19 12:15:04 +01:00
|
|
|
switch schema.Type {
|
2015-05-27 03:52:36 +02:00
|
|
|
case TypeBool, TypeInt, TypeFloat, TypeString:
|
2015-01-09 03:02:19 +01:00
|
|
|
return r.readPrimitive(address, schema)
|
2014-12-19 12:15:04 +01:00
|
|
|
case TypeList:
|
2016-06-30 17:48:52 +02:00
|
|
|
return readListField(r, address, schema)
|
2014-12-19 12:15:04 +01:00
|
|
|
case TypeMap:
|
|
|
|
return r.readMap(k)
|
|
|
|
case TypeSet:
|
2015-01-09 03:02:19 +01:00
|
|
|
return r.readSet(address, schema)
|
2014-12-19 12:15:04 +01:00
|
|
|
case typeObject:
|
2015-01-03 18:13:46 +01:00
|
|
|
return readObjectField(r, address, schema.Elem.(map[string]*Schema))
|
2014-12-19 12:15:04 +01:00
|
|
|
default:
|
2015-01-11 01:04:01 +01:00
|
|
|
panic(fmt.Sprintf("Unknown type: %s", schema.Type))
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
func (r *MapFieldReader) readMap(k string) (FieldReadResult, error) {
|
2014-12-19 12:15:04 +01:00
|
|
|
result := make(map[string]interface{})
|
|
|
|
resultSet := false
|
|
|
|
|
2015-01-10 00:07:02 +01:00
|
|
|
// If the name of the map field is directly in the map with an
|
|
|
|
// empty string, it means that the map is being deleted, so mark
|
|
|
|
// that is is set.
|
|
|
|
if v, ok := r.Map.Access(k); ok && v == "" {
|
|
|
|
resultSet = true
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:15:04 +01:00
|
|
|
prefix := k + "."
|
2015-01-09 03:02:19 +01:00
|
|
|
r.Map.Range(func(k, v string) bool {
|
|
|
|
if strings.HasPrefix(k, prefix) {
|
2015-04-21 22:11:00 +02:00
|
|
|
resultSet = true
|
|
|
|
|
2015-01-15 23:12:24 +01:00
|
|
|
key := k[len(prefix):]
|
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
|
|
|
if key != "%" && key != "#" {
|
2015-01-15 23:12:24 +01:00
|
|
|
result[key] = v
|
|
|
|
}
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
return true
|
|
|
|
})
|
2014-12-19 12:15:04 +01:00
|
|
|
|
|
|
|
var resultVal interface{}
|
|
|
|
if resultSet {
|
|
|
|
resultVal = result
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
|
|
|
Value: resultVal,
|
|
|
|
Exists: resultSet,
|
|
|
|
}, nil
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MapFieldReader) readPrimitive(
|
2015-01-09 03:02:19 +01:00
|
|
|
address []string, schema *Schema) (FieldReadResult, error) {
|
|
|
|
k := strings.Join(address, ".")
|
|
|
|
result, ok := r.Map.Access(k)
|
2014-12-19 12:15:04 +01:00
|
|
|
if !ok {
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{}, nil
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 18:22:53 +01:00
|
|
|
returnVal, err := stringToPrimitive(result, false, schema)
|
|
|
|
if err != nil {
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{}, err
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
|
|
|
Value: returnVal,
|
|
|
|
Exists: true,
|
|
|
|
}, nil
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MapFieldReader) readSet(
|
2015-01-09 03:02:19 +01:00
|
|
|
address []string, schema *Schema) (FieldReadResult, error) {
|
2014-12-19 12:15:04 +01:00
|
|
|
// Get the number of elements in the list
|
2015-01-09 03:02:19 +01:00
|
|
|
countRaw, err := r.readPrimitive(
|
|
|
|
append(address, "#"), &Schema{Type: TypeInt})
|
2014-12-19 12:15:04 +01:00
|
|
|
if err != nil {
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{}, err
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if !countRaw.Exists {
|
2014-12-19 12:15:04 +01:00
|
|
|
// No count, means we have no list
|
2014-12-19 21:47:35 +01:00
|
|
|
countRaw.Value = 0
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the set that will be our result
|
2015-08-18 04:26:58 +02:00
|
|
|
set := schema.ZeroValue().(*Set)
|
2014-12-19 12:15:04 +01:00
|
|
|
|
|
|
|
// If we have an empty list, then return an empty list
|
2014-12-19 21:47:35 +01:00
|
|
|
if countRaw.Computed || countRaw.Value.(int) == 0 {
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: set,
|
2015-01-09 03:02:19 +01:00
|
|
|
Exists: countRaw.Exists,
|
2014-12-19 21:47:35 +01:00
|
|
|
Computed: countRaw.Computed,
|
|
|
|
}, nil
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the map and find all the set items
|
2015-01-09 03:02:19 +01:00
|
|
|
prefix := strings.Join(address, ".") + "."
|
|
|
|
countExpected := countRaw.Value.(int)
|
|
|
|
countActual := make(map[string]struct{})
|
|
|
|
completed := r.Map.Range(func(k, _ string) bool {
|
2014-12-19 12:15:04 +01:00
|
|
|
if !strings.HasPrefix(k, prefix) {
|
2015-01-09 03:02:19 +01:00
|
|
|
return true
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(k, prefix+"#") {
|
|
|
|
// Ignore the count field
|
2015-01-09 03:02:19 +01:00
|
|
|
return true
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split the key, since it might be a sub-object like "idx.field"
|
|
|
|
parts := strings.Split(k[len(prefix):], ".")
|
|
|
|
idx := parts[0]
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
var raw FieldReadResult
|
|
|
|
raw, err = r.ReadField(append(address, idx))
|
2014-12-19 12:15:04 +01:00
|
|
|
if err != nil {
|
2015-01-09 03:02:19 +01:00
|
|
|
return false
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if !raw.Exists {
|
2014-12-19 12:15:04 +01:00
|
|
|
// This shouldn't happen because we just verified it does exist
|
|
|
|
panic("missing field in set: " + k + "." + idx)
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
set.Add(raw.Value)
|
2015-01-09 03:02:19 +01:00
|
|
|
|
|
|
|
// Due to the way multimap readers work, if we've seen the number
|
|
|
|
// of fields we expect, then exit so that we don't read later values.
|
|
|
|
// For example: the "set" map might have "ports.#", "ports.0", and
|
|
|
|
// "ports.1", but the "state" map might have those plus "ports.2".
|
|
|
|
// We don't want "ports.2"
|
|
|
|
countActual[idx] = struct{}{}
|
|
|
|
if len(countActual) >= countExpected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if !completed && err != nil {
|
|
|
|
return FieldReadResult{}, err
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
|
|
|
Value: set,
|
|
|
|
Exists: true,
|
|
|
|
}, nil
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
2015-01-09 03:02:19 +01:00
|
|
|
|
|
|
|
// MapReader is an interface that is given to MapFieldReader for accessing
|
|
|
|
// a "map". This can be used to have alternate implementations. For a basic
|
|
|
|
// map[string]string, use BasicMapReader.
|
|
|
|
type MapReader interface {
|
|
|
|
Access(string) (string, bool)
|
|
|
|
Range(func(string, string) bool) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// BasicMapReader implements MapReader for a single map.
|
|
|
|
type BasicMapReader map[string]string
|
|
|
|
|
|
|
|
func (r BasicMapReader) Access(k string) (string, bool) {
|
|
|
|
v, ok := r[k]
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r BasicMapReader) Range(f func(string, string) bool) bool {
|
|
|
|
for k, v := range r {
|
|
|
|
if cont := f(k, v); !cont {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiMapReader reads over multiple maps, preferring keys that are
|
|
|
|
// founder earlier (lower number index) vs. later (higher number index)
|
|
|
|
type MultiMapReader []map[string]string
|
|
|
|
|
|
|
|
func (r MultiMapReader) Access(k string) (string, bool) {
|
|
|
|
for _, m := range r {
|
|
|
|
if v, ok := m[k]; ok {
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r MultiMapReader) Range(f func(string, string) bool) bool {
|
|
|
|
done := make(map[string]struct{})
|
|
|
|
for _, m := range r {
|
|
|
|
for k, v := range m {
|
|
|
|
if _, ok := done[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if cont := f(k, v); !cont {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
done[k] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|