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 {
|
|
|
|
case TypeBool:
|
|
|
|
fallthrough
|
2015-01-11 01:04:01 +01:00
|
|
|
case TypeFloat:
|
|
|
|
fallthrough
|
2014-12-19 12:15:04 +01:00
|
|
|
case TypeInt:
|
|
|
|
fallthrough
|
|
|
|
case TypeString:
|
2015-01-09 03:02:19 +01:00
|
|
|
return r.readPrimitive(address, schema)
|
2014-12-19 12:15:04 +01:00
|
|
|
case TypeList:
|
2015-01-03 18:13:46 +01: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) {
|
|
|
|
result[k[len(prefix):]] = v
|
|
|
|
resultSet = true
|
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
|
|
|
|
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,
|
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
|
|
|
|
}
|