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 {
|
|
|
|
Map map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MapFieldReader) ReadField(
|
2014-12-19 21:47:35 +01:00
|
|
|
address []string, schema *Schema) (FieldReadResult, error) {
|
2014-12-19 12:15:04 +01:00
|
|
|
k := strings.Join(address, ".")
|
|
|
|
|
|
|
|
switch schema.Type {
|
|
|
|
case TypeBool:
|
|
|
|
fallthrough
|
|
|
|
case TypeInt:
|
|
|
|
fallthrough
|
|
|
|
case TypeString:
|
|
|
|
return r.readPrimitive(k, schema)
|
|
|
|
case TypeList:
|
2014-12-19 18:22:53 +01:00
|
|
|
return readListField(r, k, schema)
|
2014-12-19 12:15:04 +01:00
|
|
|
case TypeMap:
|
|
|
|
return r.readMap(k)
|
|
|
|
case TypeSet:
|
|
|
|
return r.readSet(k, schema)
|
|
|
|
case typeObject:
|
2014-12-19 18:22:53 +01:00
|
|
|
return readObjectField(r, k, schema.Elem.(map[string]*Schema))
|
2014-12-19 12:15:04 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
prefix := k + "."
|
|
|
|
for k, v := range r.Map {
|
|
|
|
if !strings.HasPrefix(k, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result[k[len(prefix):]] = v
|
|
|
|
resultSet = true
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2014-12-19 21:47:35 +01:00
|
|
|
k string, schema *Schema) (FieldReadResult, error) {
|
2014-12-19 12:15:04 +01:00
|
|
|
result, ok := r.Map[k]
|
|
|
|
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(
|
2014-12-19 21:47:35 +01:00
|
|
|
k string, schema *Schema) (FieldReadResult, error) {
|
2014-12-19 12:15:04 +01:00
|
|
|
// Get the number of elements in the list
|
2014-12-19 21:47:35 +01:00
|
|
|
countRaw, err := r.readPrimitive(k+".#", &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
|
|
|
|
set := &Set{F: schema.Set}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
Exists: true,
|
|
|
|
Computed: countRaw.Computed,
|
|
|
|
}, nil
|
2014-12-19 12:15:04 +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 the map and find all the set items
|
|
|
|
prefix := k + "."
|
|
|
|
for k, _ := range r.Map {
|
|
|
|
if !strings.HasPrefix(k, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(k, prefix+"#") {
|
|
|
|
// Ignore the count field
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the key, since it might be a sub-object like "idx.field"
|
|
|
|
parts := strings.Split(k[len(prefix):], ".")
|
|
|
|
idx := parts[0]
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
raw, err := r.ReadField([]string{prefix + idx}, elemSchema)
|
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 !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)
|
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
|
|
|
}
|