Skip to content

Commit

Permalink
fix update codegen
Browse files Browse the repository at this point in the history
Signed-off-by: Hector Fernandez <hector@chainguard.dev>
  • Loading branch information
hectorj2f committed Dec 1, 2023
1 parent c970af7 commit 40d46e9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 43 deletions.
46 changes: 3 additions & 43 deletions third_party/VENDOR-LICENSE/github.com/hashicorp/hcl/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func expandObject(node ast.Node, result reflect.Value) ast.Node {
// we need to un-flatten the ast enough to decode
newNode := &ast.ObjectItem{
Keys: []*ast.ObjectKey{
{
&ast.ObjectKey{
Token: keyToken,
},
},
Expand Down Expand Up @@ -628,24 +628,6 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
decodedFields := make([]string, 0, len(fields))
decodedFieldsVal := 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(map[string][]token.Pos, 0)
for i, item := range list.Items {
for _, k := range item.Keys {
// isNestedJSON returns true for e.g. bar in
// { "foo": { "bar": {...} } }
// This isn't an unused node key, so we want to skip it
isNestedJSON := i > 0 && len(item.Keys) > 1
if !isNestedJSON && (k.Token.JSON || k.Token.Type == token.IDENT) {
fn := k.Token.Value().(string)
sl := unusedNodeKeys[fn]
unusedNodeKeys[fn] = append(sl, k.Token.Pos)
}
}
}

for _, f := range fields {
field, fieldValue := f.field, f.val
if !fieldValue.IsValid() {
Expand Down Expand Up @@ -679,7 +661,7 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)

fieldValue.SetString(item.Keys[0].Token.Value().(string))
continue
case "unusedKeyPositions":
case "unusedKeys":
unusedKeysVal = append(unusedKeysVal, fieldValue)
continue
}
Expand All @@ -700,9 +682,8 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
continue
}

// Track the used keys
// Track the used key
usedKeys[fieldName] = struct{}{}
unusedNodeKeys = removeCaseFold(unusedNodeKeys, fieldName)

// Create the field name and decode. We range over the elements
// because we actually want the value.
Expand Down Expand Up @@ -735,13 +716,6 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
}
}

if len(unusedNodeKeys) > 0 {
// like decodedFields, populated the unusedKeys field(s)
for _, v := range unusedKeysVal {
v.Set(reflect.ValueOf(unusedNodeKeys))
}
}

return nil
}

Expand All @@ -753,17 +727,3 @@ func findNodeType() reflect.Type {
value := reflect.ValueOf(nodeContainer).FieldByName("Node")
return value.Type()
}

func removeCaseFold(xs map[string][]token.Pos, y string) map[string][]token.Pos {
var toDel []string

for i := range xs {
if strings.EqualFold(i, y) {
toDel = append(toDel, i)
}
}
for _, i := range toDel {
delete(xs, i)
}
return xs
}

0 comments on commit 40d46e9

Please sign in to comment.