This commit is contained in:
Frank Schroeder 2017-09-26 12:42:03 +02:00
parent a1d65cbe78
commit 3396f11cfd
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
1 changed files with 18 additions and 23 deletions

View File

@ -56,7 +56,7 @@ func DecodeObject(out interface{}, n ast.Node) error {
n = f.Node n = f.Node
} }
d := &decoder{} var d decoder
return d.decode("root", n, val.Elem()) return d.decode("root", n, val.Elem())
} }
@ -573,12 +573,11 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
// Compile the list of all the fields that we're going to be decoding // Compile the list of all the fields that we're going to be decoding
// from all the structs. // from all the structs.
// fields := make(map[*reflect.StructField]reflect.Value) type field struct {
type x struct { field reflect.StructField
t reflect.StructField val reflect.Value
v reflect.Value
} }
fields := []x{} fields := []field{}
for len(structs) > 0 { for len(structs) > 0 {
structVal := structs[0] structVal := structs[0]
structs = structs[1:] structs = structs[1:]
@ -621,8 +620,7 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
} }
// Normal struct field, store it away // Normal struct field, store it away
fields = append(fields, x{fieldType, structVal.Field(i)}) fields = append(fields, field{fieldType, structVal.Field(i)})
// fields[&fieldType] = structVal.Field(i)
} }
} }
@ -630,28 +628,27 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
decodedFields := make([]string, 0, len(fields)) decodedFields := make([]string, 0, len(fields))
decodedFieldsVal := make([]reflect.Value, 0) decodedFieldsVal := make([]reflect.Value, 0)
unusedKeysVal := make([]reflect.Value, 0) unusedKeysVal := make([]reflect.Value, 0)
// for fieldType, field := range fields { for _, f := range fields {
for _, x := range fields { field, fieldValue := f.field, f.val
fieldType, field := x.t, x.v if !fieldValue.IsValid() {
if !field.IsValid() {
// This should never happen // This should never happen
panic("field is not valid") panic("field is not valid")
} }
// If we can't set the field, then it is unexported or something, // If we can't set the field, then it is unexported or something,
// and we just continue onwards. // and we just continue onwards.
if !field.CanSet() { if !fieldValue.CanSet() {
continue continue
} }
fieldName := fieldType.Name fieldName := field.Name
tagValue := fieldType.Tag.Get(tagName) tagValue := field.Tag.Get(tagName)
tagParts := strings.SplitN(tagValue, ",", 2) tagParts := strings.SplitN(tagValue, ",", 2)
if len(tagParts) >= 2 { if len(tagParts) >= 2 {
switch tagParts[1] { switch tagParts[1] {
case "decodedFields": case "decodedFields":
decodedFieldsVal = append(decodedFieldsVal, field) decodedFieldsVal = append(decodedFieldsVal, fieldValue)
continue continue
case "key": case "key":
if item == nil { if item == nil {
@ -662,10 +659,10 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
} }
} }
field.SetString(item.Keys[0].Token.Value().(string)) fieldValue.SetString(item.Keys[0].Token.Value().(string))
continue continue
case "unusedKeys": case "unusedKeys":
unusedKeysVal = append(unusedKeysVal, field) unusedKeysVal = append(unusedKeysVal, fieldValue)
continue continue
} }
} }
@ -677,9 +674,7 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
// Determine the element we'll use to decode. If it is a single // Determine the element we'll use to decode. If it is a single
// match (only object with the field), then we decode it exactly. // match (only object with the field), then we decode it exactly.
// If it is a prefix match, then we decode the matches. // If it is a prefix match, then we decode the matches.
// d.mu.Lock()
filter := list.Filter(fieldName) filter := list.Filter(fieldName)
// d.mu.Unlock()
prefixMatches := filter.Children() prefixMatches := filter.Children()
matches := filter.Elem() matches := filter.Elem()
@ -694,7 +689,7 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
// because we actually want the value. // because we actually want the value.
fieldName = fmt.Sprintf("%s.%s", name, fieldName) fieldName = fmt.Sprintf("%s.%s", name, fieldName)
if len(prefixMatches.Items) > 0 { if len(prefixMatches.Items) > 0 {
if err := d.decode(fieldName, prefixMatches, field); err != nil { if err := d.decode(fieldName, prefixMatches, fieldValue); err != nil {
return err return err
} }
} }
@ -704,12 +699,12 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
decodeNode = &ast.ObjectList{Items: ot.List.Items} decodeNode = &ast.ObjectList{Items: ot.List.Items}
} }
if err := d.decode(fieldName, decodeNode, field); err != nil { if err := d.decode(fieldName, decodeNode, fieldValue); err != nil {
return err return err
} }
} }
decodedFields = append(decodedFields, fieldType.Name) decodedFields = append(decodedFields, field.Name)
} }
if len(decodedFieldsVal) > 0 { if len(decodedFieldsVal) > 0 {