fix hcl:"field,unusedKeys"
populate with extra keys from the AST
This commit is contained in:
parent
258b8826b3
commit
fa8f3652ce
32
decoder.go
32
decoder.go
@ -631,10 +631,19 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
// fill unusedNodeKeys with keys from the AST
|
||||||
|
// a slice because we have to do equals case fold to match Filter
|
||||||
|
unusedNodeKeys := make([]string, 0)
|
||||||
|
for _, item := range list.Items {
|
||||||
|
for _, k := range item.Keys {
|
||||||
|
unusedNodeKeys = append(unusedNodeKeys, k.Token.Value().(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, f := range fields {
|
for _, f := range fields {
|
||||||
field, fieldValue := f.field, f.val
|
field, fieldValue := f.field, f.val
|
||||||
if !fieldValue.IsValid() {
|
if !fieldValue.IsValid() {
|
||||||
@ -689,8 +698,8 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Track the used key
|
// Track the used keys
|
||||||
usedKeys[fieldName] = struct{}{}
|
unusedNodeKeys = removeCaseFold(unusedNodeKeys, fieldName)
|
||||||
|
|
||||||
// Create the field name and decode. We range over the elements
|
// Create the field name and decode. We range over the elements
|
||||||
// because we actually want the value.
|
// because we actually want the value.
|
||||||
@ -723,6 +732,14 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(unusedNodeKeys) > 0 {
|
||||||
|
// like decodedFields, populated the unusedKeys field(s)
|
||||||
|
sort.Strings(unusedNodeKeys)
|
||||||
|
for _, v := range unusedKeysVal {
|
||||||
|
v.Set(reflect.ValueOf(unusedNodeKeys))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -734,3 +751,12 @@ func findNodeType() reflect.Type {
|
|||||||
value := reflect.ValueOf(nodeContainer).FieldByName("Node")
|
value := reflect.ValueOf(nodeContainer).FieldByName("Node")
|
||||||
return value.Type()
|
return value.Type()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removeCaseFold(xs []string, y string) []string {
|
||||||
|
for i, x := range xs {
|
||||||
|
if strings.EqualFold(x, y) {
|
||||||
|
return append(xs[:i], xs[i+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return xs
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user