decoding structs a bit

This commit is contained in:
Mitchell Hashimoto 2014-08-11 16:51:52 -07:00
parent 61e6b8179b
commit 719a177dba
2 changed files with 41 additions and 20 deletions

View File

@ -334,11 +334,10 @@ func (d *decoder) decodeStruct(name string, o *hcl.Object, result reflect.Value)
} }
} }
//usedKeys := make(map[string]struct{}) usedKeys := make(map[string]struct{})
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 fieldType, field := range fields {
if !field.IsValid() { if !field.IsValid() {
// This should never happen // This should never happen
@ -374,8 +373,8 @@ func (d *decoder) decodeStruct(name string, o *hcl.Object, result reflect.Value)
} }
// Find the element matching this name // Find the element matching this name
elems := obj.Get(fieldName, true) obj := o.Get(fieldName, true)
if len(elems) == 0 { if obj == nil {
continue continue
} }
@ -384,26 +383,12 @@ func (d *decoder) decodeStruct(name string, o *hcl.Object, result reflect.Value)
// Create the field name and decode // Create the field name and decode
fieldName = fmt.Sprintf("%s.%s", name, fieldName) fieldName = fmt.Sprintf("%s.%s", name, fieldName)
for _, elem := range elems { if err := d.decode(fieldName, obj, field); err != nil {
// If it is a sub-object, go through all the fields return err
if obj, ok := elem.(ast.ObjectNode); ok {
for _, elem := range obj.Elem {
if err := d.decode(fieldName, elem.Value, field); err != nil {
return err
}
}
continue
}
if err := d.decode(fieldName, elem, field); err != nil {
return err
}
} }
decodedFields = append(decodedFields, fieldType.Name) decodedFields = append(decodedFields, fieldType.Name)
} }
*/
for _, v := range decodedFieldsVal { for _, v := range decodedFieldsVal {
v.Set(reflect.ValueOf(decodedFields)) v.Set(reflect.ValueOf(decodedFields))

View File

@ -1,5 +1,9 @@
package hcl package hcl
import (
"strings"
)
// ValueType is an enum represnting the type of a value in // ValueType is an enum represnting the type of a value in
// a LiteralNode. // a LiteralNode.
type ValueType byte type ValueType byte
@ -24,6 +28,38 @@ type Object struct {
Next *Object Next *Object
} }
// Get gets all the objects that match the given key.
//
// It returns the resulting objects as a single Object structure with
// the linked list populated.
func (o *Object) Get(k string, insensitive bool) *Object {
if o.Type != ValueTypeObject {
return nil
}
var current, result *Object
m := o.Value.(map[string]*Object)
for _, o := range m {
if o.Key != k {
if !insensitive || !strings.EqualFold(o.Key, k) {
continue
}
}
o2 := *o
o2.Next = nil
if result == nil {
result = &o2
current = result
} else {
current.Next = &o2
current = current.Next
}
}
return result
}
// ObjectList is a list of objects. // ObjectList is a list of objects.
type ObjectList []*Object type ObjectList []*Object