decoding structs a bit
This commit is contained in:
parent
61e6b8179b
commit
719a177dba
25
decoder.go
25
decoder.go
@ -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))
|
||||
decodedFieldsVal := make([]reflect.Value, 0)
|
||||
unusedKeysVal := make([]reflect.Value, 0)
|
||||
/*
|
||||
for fieldType, field := range fields {
|
||||
if !field.IsValid() {
|
||||
// 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
|
||||
elems := obj.Get(fieldName, true)
|
||||
if len(elems) == 0 {
|
||||
obj := o.Get(fieldName, true)
|
||||
if obj == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
@ -384,26 +383,12 @@ func (d *decoder) decodeStruct(name string, o *hcl.Object, result reflect.Value)
|
||||
|
||||
// Create the field name and decode
|
||||
fieldName = fmt.Sprintf("%s.%s", name, fieldName)
|
||||
for _, elem := range elems {
|
||||
// If it is a sub-object, go through all the fields
|
||||
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
|
||||
}
|
||||
if err := d.decode(fieldName, obj, field); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
decodedFields = append(decodedFields, fieldType.Name)
|
||||
}
|
||||
*/
|
||||
|
||||
for _, v := range decodedFieldsVal {
|
||||
v.Set(reflect.ValueOf(decodedFields))
|
||||
|
@ -1,5 +1,9 @@
|
||||
package hcl
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ValueType is an enum represnting the type of a value in
|
||||
// a LiteralNode.
|
||||
type ValueType byte
|
||||
@ -24,6 +28,38 @@ type Object struct {
|
||||
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.
|
||||
type ObjectList []*Object
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user