/
expr.go
41 lines (38 loc) · 1.04 KB
/
expr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Utilities for working with expressions.
package regulatf
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
// ExprAttributes tries to gather all attributes that are being used in a given
// expression. For example, for:
//
// aws_s3_bucket.bucket[count.index].acl
//
// It would return [acl].
func ExprAttributes(expr hcl.Expression) []LocalName {
names := []LocalName{}
if syn, ok := expr.(hclsyntax.Expression); ok {
hclsyntax.VisitAll(syn, func(node hclsyntax.Node) hcl.Diagnostics {
switch e := node.(type) {
case *hclsyntax.RelativeTraversalExpr:
if name, err := TraversalToLocalName(e.Traversal); err == nil {
names = append(names, name)
}
case *hclsyntax.IndexExpr:
if key := ExprToLiteralString(e.Key); key != nil {
name := LocalName{*key}
names = append(names, name)
}
}
return nil
})
}
return names
}
func ExprToLiteralString(expr hcl.Expression) *string {
if e, ok := expr.(*hclsyntax.LiteralValueExpr); ok {
return ValueToString(e.Val)
}
return nil
}