2014-12-19 11:56:46 +01:00
|
|
|
package schema
|
|
|
|
|
2014-12-19 18:22:53 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2014-12-19 11:56:46 +01:00
|
|
|
// FieldReaders are responsible for decoding fields out of data into
|
|
|
|
// the proper typed representation. ResourceData uses this to query data
|
|
|
|
// out of multiple sources: config, state, diffs, etc.
|
|
|
|
type FieldReader interface {
|
2015-01-03 18:13:46 +01:00
|
|
|
ReadField([]string) (FieldReadResult, error)
|
2014-12-19 21:47:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FieldReadResult encapsulates all the resulting data from reading
|
|
|
|
// a field.
|
|
|
|
type FieldReadResult struct {
|
|
|
|
// Value is the actual read value. NegValue is the _negative_ value
|
|
|
|
// or the items that should be removed (if they existed). NegValue
|
|
|
|
// doesn't make sense for primitives but is important for any
|
|
|
|
// container types such as maps, sets, lists.
|
2015-01-09 03:02:19 +01:00
|
|
|
Value interface{}
|
|
|
|
ValueProcessed interface{}
|
2014-12-19 21:47:35 +01:00
|
|
|
|
|
|
|
// Exists is true if the field was found in the data. False means
|
|
|
|
// it wasn't found if there was no error.
|
|
|
|
Exists bool
|
|
|
|
|
|
|
|
// Computed is true if the field was found but the value
|
|
|
|
// is computed.
|
|
|
|
Computed bool
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
2014-12-19 18:22:53 +01:00
|
|
|
|
2015-01-09 20:51:29 +01:00
|
|
|
// ValueOrZero returns the value of this result or the zero value of the
|
|
|
|
// schema type, ensuring a consistent non-nil return value.
|
|
|
|
func (r *FieldReadResult) ValueOrZero(s *Schema) interface{} {
|
|
|
|
if r.Value != nil {
|
|
|
|
return r.Value
|
|
|
|
}
|
|
|
|
|
2015-08-18 04:26:58 +02:00
|
|
|
return s.ZeroValue()
|
2015-01-09 20:51:29 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
// addrToSchema finds the final element schema for the given address
|
2015-01-09 03:02:19 +01:00
|
|
|
// and the given schema. It returns all the schemas that led to the final
|
|
|
|
// schema. These are in order of the address (out to in).
|
|
|
|
func addrToSchema(addr []string, schemaMap map[string]*Schema) []*Schema {
|
2015-01-03 18:13:46 +01:00
|
|
|
current := &Schema{
|
|
|
|
Type: typeObject,
|
|
|
|
Elem: schemaMap,
|
|
|
|
}
|
|
|
|
|
2015-01-10 02:43:44 +01:00
|
|
|
// If we aren't given an address, then the user is requesting the
|
|
|
|
// full object, so we return the special value which is the full object.
|
2015-01-10 00:07:02 +01:00
|
|
|
if len(addr) == 0 {
|
|
|
|
return []*Schema{current}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*Schema, 0, len(addr))
|
2015-01-03 18:13:46 +01:00
|
|
|
for len(addr) > 0 {
|
|
|
|
k := addr[0]
|
|
|
|
addr = addr[1:]
|
|
|
|
|
|
|
|
REPEAT:
|
2015-01-09 03:02:19 +01:00
|
|
|
// We want to trim off the first "typeObject" since its not a
|
|
|
|
// real lookup that people do. i.e. []string{"foo"} in a structure
|
|
|
|
// isn't {typeObject, typeString}, its just a {typeString}.
|
|
|
|
if len(result) > 0 || current.Type != typeObject {
|
|
|
|
result = append(result, current)
|
2015-01-03 18:13:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
switch t := current.Type; t {
|
2015-05-27 03:52:36 +02:00
|
|
|
case TypeBool, TypeInt, TypeFloat, TypeString:
|
2015-01-03 18:13:46 +01:00
|
|
|
if len(addr) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-27 03:52:36 +02:00
|
|
|
case TypeList, TypeSet:
|
2016-11-23 19:43:56 +01:00
|
|
|
isIndex := len(addr) > 0 && addr[0] == "#"
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
switch v := current.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
current = &Schema{
|
|
|
|
Type: typeObject,
|
|
|
|
Elem: v.Schema,
|
|
|
|
}
|
|
|
|
case *Schema:
|
|
|
|
current = v
|
2016-11-23 19:43:56 +01:00
|
|
|
case ValueType:
|
|
|
|
current = &Schema{Type: v}
|
2015-01-03 18:13:46 +01:00
|
|
|
default:
|
2016-11-23 19:43:56 +01:00
|
|
|
// we may not know the Elem type and are just looking for the
|
|
|
|
// index
|
|
|
|
if isIndex {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addr) == 0 {
|
|
|
|
// we've processed the address, so return what we've
|
|
|
|
// collected
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addr) == 1 {
|
|
|
|
if _, err := strconv.Atoi(addr[0]); err == nil {
|
|
|
|
// we're indexing a value without a schema. This can
|
|
|
|
// happen if the list is nested in another schema type.
|
|
|
|
// Default to a TypeString like we do with a map
|
|
|
|
current = &Schema{Type: TypeString}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:51:16 +02:00
|
|
|
// If we only have one more thing and the next thing
|
2015-01-09 03:02:19 +01:00
|
|
|
// is a #, then we're accessing the index which is always
|
|
|
|
// an int.
|
2016-11-23 19:43:56 +01:00
|
|
|
if isIndex {
|
2015-01-03 18:13:46 +01:00
|
|
|
current = &Schema{Type: TypeInt}
|
2015-01-09 03:02:19 +01:00
|
|
|
break
|
2015-01-03 18:13:46 +01:00
|
|
|
}
|
2016-11-23 19:43:56 +01:00
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
case TypeMap:
|
|
|
|
if len(addr) > 0 {
|
2016-11-23 19:43:56 +01:00
|
|
|
switch v := current.Elem.(type) {
|
|
|
|
case ValueType:
|
|
|
|
current = &Schema{Type: v}
|
2018-03-14 22:50:41 +01:00
|
|
|
case *Schema:
|
|
|
|
current, _ = current.Elem.(*Schema)
|
2016-11-23 19:43:56 +01:00
|
|
|
default:
|
|
|
|
// maps default to string values. This is all we can have
|
|
|
|
// if this is nested in another list or map.
|
|
|
|
current = &Schema{Type: TypeString}
|
|
|
|
}
|
2015-01-03 18:13:46 +01:00
|
|
|
}
|
|
|
|
case typeObject:
|
2015-01-09 03:02:19 +01:00
|
|
|
// If we're already in the object, then we want to handle Sets
|
|
|
|
// and Lists specially. Basically, their next key is the lookup
|
|
|
|
// key (the set value or the list element). For these scenarios,
|
|
|
|
// we just want to skip it and move to the next element if there
|
|
|
|
// is one.
|
|
|
|
if len(result) > 0 {
|
|
|
|
lastType := result[len(result)-2].Type
|
|
|
|
if lastType == TypeSet || lastType == TypeList {
|
|
|
|
if len(addr) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
k = addr[0]
|
|
|
|
addr = addr[1:]
|
|
|
|
}
|
2015-01-03 18:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m := current.Elem.(map[string]*Schema)
|
|
|
|
val, ok := m[k]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
current = val
|
|
|
|
goto REPEAT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-06-30 17:48:52 +02:00
|
|
|
// readListField is a generic method for reading a list field out of a
|
|
|
|
// a FieldReader. It does this based on the assumption that there is a key
|
|
|
|
// "foo.#" for a list "foo" and that the indexes are "foo.0", "foo.1", etc.
|
|
|
|
// after that point.
|
|
|
|
func readListField(
|
|
|
|
r FieldReader, addr []string, schema *Schema) (FieldReadResult, error) {
|
|
|
|
addrPadded := make([]string, len(addr)+1)
|
|
|
|
copy(addrPadded, addr)
|
|
|
|
addrPadded[len(addrPadded)-1] = "#"
|
|
|
|
|
|
|
|
// Get the number of elements in the list
|
|
|
|
countResult, err := r.ReadField(addrPadded)
|
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
if !countResult.Exists {
|
|
|
|
// No count, means we have no list
|
|
|
|
countResult.Value = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an empty list, then return an empty list
|
|
|
|
if countResult.Computed || countResult.Value.(int) == 0 {
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: []interface{}{},
|
|
|
|
Exists: countResult.Exists,
|
|
|
|
Computed: countResult.Computed,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through each count, and get the item value out of it
|
|
|
|
result := make([]interface{}, countResult.Value.(int))
|
|
|
|
for i, _ := range result {
|
|
|
|
is := strconv.FormatInt(int64(i), 10)
|
|
|
|
addrPadded[len(addrPadded)-1] = is
|
|
|
|
rawResult, err := r.ReadField(addrPadded)
|
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
if !rawResult.Exists {
|
|
|
|
// This should never happen, because by the time the data
|
|
|
|
// gets to the FieldReaders, all the defaults should be set by
|
|
|
|
// Schema.
|
|
|
|
rawResult.Value = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = rawResult.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: result,
|
|
|
|
Exists: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-12-19 18:22:53 +01:00
|
|
|
// readObjectField is a generic method for reading objects out of FieldReaders
|
|
|
|
// based on the assumption that building an address of []string{k, FIELD}
|
|
|
|
// will result in the proper field data.
|
|
|
|
func readObjectField(
|
|
|
|
r FieldReader,
|
2015-01-03 18:13:46 +01:00
|
|
|
addr []string,
|
2014-12-19 21:47:35 +01:00
|
|
|
schema map[string]*Schema) (FieldReadResult, error) {
|
2014-12-19 18:22:53 +01:00
|
|
|
result := make(map[string]interface{})
|
2015-01-09 20:51:29 +01:00
|
|
|
exists := false
|
|
|
|
for field, s := range schema {
|
2015-01-03 18:13:46 +01:00
|
|
|
addrRead := make([]string, len(addr), len(addr)+1)
|
|
|
|
copy(addrRead, addr)
|
|
|
|
addrRead = append(addrRead, field)
|
|
|
|
rawResult, err := r.ReadField(addrRead)
|
2014-12-19 18:22:53 +01:00
|
|
|
if err != nil {
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{}, err
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
2015-01-09 20:51:29 +01:00
|
|
|
if rawResult.Exists {
|
|
|
|
exists = true
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2015-01-09 20:51:29 +01:00
|
|
|
result[field] = rawResult.ValueOrZero(s)
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
|
|
|
Value: result,
|
2015-01-09 20:51:29 +01:00
|
|
|
Exists: exists,
|
2014-12-19 21:47:35 +01:00
|
|
|
}, nil
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 21:12:29 +01:00
|
|
|
// convert map values to the proper primitive type based on schema.Elem
|
2018-03-14 22:50:41 +01:00
|
|
|
func mapValuesToPrimitive(k string, m map[string]interface{}, schema *Schema) error {
|
|
|
|
elemType, err := getValueType(k, schema)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-11-17 21:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch elemType {
|
|
|
|
case TypeInt, TypeFloat, TypeBool:
|
|
|
|
for k, v := range m {
|
|
|
|
vs, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := stringToPrimitive(vs, false, &Schema{Type: elemType})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-19 18:22:53 +01:00
|
|
|
func stringToPrimitive(
|
|
|
|
value string, computed bool, schema *Schema) (interface{}, error) {
|
|
|
|
var returnVal interface{}
|
|
|
|
switch schema.Type {
|
|
|
|
case TypeBool:
|
|
|
|
if value == "" {
|
|
|
|
returnVal = false
|
|
|
|
break
|
|
|
|
}
|
2016-11-02 21:25:23 +01:00
|
|
|
if computed {
|
|
|
|
break
|
|
|
|
}
|
2014-12-19 18:22:53 +01:00
|
|
|
|
|
|
|
v, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-11 01:04:01 +01:00
|
|
|
returnVal = v
|
|
|
|
case TypeFloat:
|
|
|
|
if value == "" {
|
|
|
|
returnVal = 0.0
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if computed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 18:22:53 +01:00
|
|
|
returnVal = v
|
|
|
|
case TypeInt:
|
|
|
|
if value == "" {
|
|
|
|
returnVal = 0
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if computed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := strconv.ParseInt(value, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
returnVal = int(v)
|
|
|
|
case TypeString:
|
|
|
|
returnVal = value
|
|
|
|
default:
|
2015-01-11 01:04:01 +01:00
|
|
|
panic(fmt.Sprintf("Unknown type: %s", schema.Type))
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return returnVal, nil
|
|
|
|
}
|