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 {
|
2014-12-19 21:47:35 +01:00
|
|
|
ReadField([]string, *Schema) (FieldReadResult, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
Value interface{}
|
|
|
|
NegValue interface{}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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(
|
2014-12-19 21:47:35 +01:00
|
|
|
r FieldReader, k string, schema *Schema) (FieldReadResult, error) {
|
2014-12-19 18:22:53 +01:00
|
|
|
// Get the number of elements in the list
|
2014-12-19 21:47:35 +01:00
|
|
|
countResult, err := r.ReadField([]string{k + ".#"}, &Schema{Type: TypeInt})
|
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
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if !countResult.Exists {
|
2014-12-19 18:22:53 +01:00
|
|
|
// No count, means we have no list
|
2014-12-19 21:47:35 +01:00
|
|
|
countResult.Value = 0
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an empty list, then return an empty list
|
2014-12-19 21:47:35 +01:00
|
|
|
if countResult.Computed || countResult.Value.(int) == 0 {
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: []interface{}{},
|
|
|
|
Exists: true,
|
|
|
|
Computed: countResult.Computed,
|
|
|
|
}, nil
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the schema for the elements
|
|
|
|
var elemSchema *Schema
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
elemSchema = &Schema{
|
|
|
|
Type: typeObject,
|
|
|
|
Elem: t.Schema,
|
|
|
|
}
|
|
|
|
case *Schema:
|
|
|
|
elemSchema = t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through each count, and get the item value out of it
|
2014-12-19 21:47:35 +01:00
|
|
|
result := make([]interface{}, countResult.Value.(int))
|
2014-12-19 18:22:53 +01:00
|
|
|
for i, _ := range result {
|
|
|
|
is := strconv.FormatInt(int64(i), 10)
|
2014-12-19 21:47:35 +01:00
|
|
|
rawResult, err := r.ReadField([]string{k, is}, elemSchema)
|
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
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if !rawResult.Exists {
|
2014-12-19 18:22:53 +01:00
|
|
|
// This should never happen, because by the time the data
|
|
|
|
// gets to the FieldReaders, all the defaults should be set by
|
|
|
|
// Schema.
|
2014-12-19 21:47:35 +01:00
|
|
|
rawResult.Value = nil
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
result[i] = rawResult.Value
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
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,
|
|
|
|
k 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{})
|
|
|
|
for field, schema := range schema {
|
2014-12-19 21:47:35 +01:00
|
|
|
rawResult, err := r.ReadField([]string{k, field}, schema)
|
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
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if !rawResult.Exists {
|
2014-12-19 18:22:53 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
result[field] = rawResult.Value
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
|
|
|
Value: result,
|
|
|
|
Exists: true,
|
|
|
|
}, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnVal, nil
|
|
|
|
}
|