Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/schema: Make nested Set(s) in List(s) work (2) #7485

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions helper/schema/field_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,60 +130,6 @@ func addrToSchema(addr []string, schemaMap map[string]*Schema) []*Schema {
return result
}

// 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
}

// 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.
Expand Down
56 changes: 54 additions & 2 deletions helper/schema/field_reader_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (r *ConfigFieldReader) readField(
case TypeBool, TypeFloat, TypeInt, TypeString:
return r.readPrimitive(k, schema)
case TypeList:
return readListField(&nestedConfigFieldReader{r}, address, schema)
return r.readList(address, schema)
case TypeMap:
return r.readMap(k)
case TypeSet:
Expand Down Expand Up @@ -216,13 +216,65 @@ func (r *ConfigFieldReader) readPrimitive(
}, nil
}

func (r *ConfigFieldReader) readList(
address []string, schema *Schema) (FieldReadResult, error) {

addrPadded := make([]string, len(address)+1)
copy(addrPadded, address)

// Get the number of elements in the list
addrPadded[len(addrPadded)-1] = "#"
countResult, err := r.readPrimitive(
strings.Join(addrPadded, "."), &Schema{Type: TypeInt})
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 {
idx := strconv.Itoa(i)
addrPadded[len(addrPadded)-1] = idx
rawResult, err := r.readField(addrPadded, true)
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.
panic("missing field in list: " + strings.Join(addrPadded, "."))
}

result[i] = rawResult.Value
}

return FieldReadResult{
Value: result,
Exists: true,
}, nil
}

func (r *ConfigFieldReader) readSet(
address []string, schema *Schema) (FieldReadResult, error) {
indexMap := make(map[string]int)
// Create the set that will be our result
set := schema.ZeroValue().(*Set)

raw, err := readListField(&nestedConfigFieldReader{r}, address, schema)
raw, err := r.readList(address, schema)
if err != nil {
return FieldReadResult{}, err
}
Expand Down
Loading