Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj-snyk committed Nov 22, 2023
1 parent 4682096 commit 8b239b6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
8 changes: 5 additions & 3 deletions pkg/hcl_interpreter/accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,19 @@ func traversalToAccessor(traversal hcl.Traversal) (accessor, error) {
return parts, nil
}

// toLocalName tries to convert the accessor to a local name starting from the
// front. As soon as a non-string part is encountered, we stop and return the
// trailing accessor as well.
func (a accessor) toLocalName() (LocalName, accessor) {
name := make(LocalName, 0)
trailing := make(accessor, len(a))
copy(trailing, a)
numeric := false
for !numeric && len(trailing) > 0 {
for len(trailing) > 0 {
if str, ok := trailing[0].(string); ok {
name = append(name, str)
trailing = trailing[1:]
} else {
numeric = true
break
}
}
return name, trailing
Expand Down
16 changes: 8 additions & 8 deletions pkg/hcl_interpreter/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ func (name LocalName) Equals(other LocalName) bool {
}

func (name LocalName) ToAccessor() accessor {
accessor := make(accessor, len(name))
for i := range name {
accessor[i] = name[i]
}
return accessor
accessor := make(accessor, len(name))
for i := range name {
accessor[i] = name[i]
}
return accessor
}

type FullName struct {
Expand Down Expand Up @@ -225,14 +225,14 @@ func (name FullName) AsResourceName() (*FullName, LocalName) {
return nil, nil
}

// TODO: Refactor to TraversalToName?
// TODO: Return trailing part
// TraversalToLocalName returns the leading LocalName (strictly-string) part of
// a traversal as well as the trailing part (string-or-int).
func TraversalToLocalName(traversal hcl.Traversal) (LocalName, accessor, error) {
// Just delegate the conversion to the accessor.
accessor, err := traversalToAccessor(traversal)
if err != nil {
return nil, nil, err
}

name, trailing := accessor.toLocalName()
return name, trailing, nil
}
Expand Down
21 changes: 14 additions & 7 deletions pkg/hcl_interpreter/phantom_attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,23 @@ func (pa *phantomAttrs) analyze(name FullName, term Term) {
if asResourceName, resourceTrailing := full.AsResourceName(); asResourceName != nil {
attrs := map[string]struct{}{}

trailingAccessor := resourceTrailing.ToAccessor()
trailingAccessor = append(trailingAccessor, localTrailing...)
if pa.isMultiResource(*asResourceName) && len(trailingAccessor) > 0 {
trailingAccessor = trailingAccessor[1:]
}
trailing, _ := trailingAccessor.toLocalName()
if len(trailing) > 0 {
// Construct the trailing part from the resourceTrailing
// and localTrailing parts defined above.
trailingAccessor := resourceTrailing.ToAccessor()
trailingAccessor = append(trailingAccessor, localTrailing...)

// We need to strip one element from the index if this is a
// multi-resource to drop the index or key.
if pa.isMultiResource(*asResourceName) && len(trailingAccessor) > 0 {
trailingAccessor = trailingAccessor[1:]
}

// Store the trailing part that was accessed in attrs.
if trailing, _ := trailingAccessor.toLocalName(); len(trailing) > 0 {
attrs[LocalNameToString(trailing)] = struct{}{}
}

// Store the other attrs.
for _, attr := range exprAttrs {
attrs[LocalNameToString(attr)] = struct{}{}
}
Expand Down

0 comments on commit 8b239b6

Please sign in to comment.