Skip to content

Commit

Permalink
decoder: Use AttrStep syntax to target Object attributes if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 22, 2023
1 parent d206335 commit 71ae3ab
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions decoder/expr_object_ref_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,27 @@ func (obj Object) ReferenceTargets(ctx context.Context, targetCtx *TargetContext
}

elemCtx := targetCtx.Copy()
elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.IndexStep{
Key: cty.StringVal(keyName),
})
if elemCtx.ParentLocalAddress != nil {
elemCtx.ParentLocalAddress = append(elemCtx.ParentLocalAddress, lang.IndexStep{
Key: cty.StringVal(keyName),

if hclsyntax.ValidIdentifier(name) {
// Prefer simpler syntax - e.g. myobj.attribute if possible
elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.AttrStep{
Name: name,
})
if elemCtx.ParentLocalAddress != nil {
elemCtx.ParentLocalAddress = append(elemCtx.ParentLocalAddress, lang.AttrStep{
Name: name,
})
}
} else {
// Fall back to indexing syntax - e.g. myobj["attr-foo"]
elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.IndexStep{
Key: cty.StringVal(name),
})
if elemCtx.ParentLocalAddress != nil {
elemCtx.ParentLocalAddress = append(elemCtx.ParentLocalAddress, lang.IndexStep{
Key: cty.StringVal(name),
})
}
}

attrTargets = append(attrTargets, e.ReferenceTargets(ctx, elemCtx)...)
Expand Down

0 comments on commit 71ae3ab

Please sign in to comment.