MCPcopy
hub / github.com/hashicorp/hcl / NewExpressionAbsTraversal

Function NewExpressionAbsTraversal

hclwrite/ast_expression.go:72–128  ·  view source on GitHub ↗

NewExpressionAbsTraversal constructs an expression that represents the given traversal, which must be absolute or this function will panic.

(traversal hcl.Traversal)

Source from the content-addressed store, hash-verified

70// NewExpressionAbsTraversal constructs an expression that represents the
71// given traversal, which must be absolute or this function will panic.
72func NewExpressionAbsTraversal(traversal hcl.Traversal) *Expression {
73 if traversal.IsRelative() {
74 panic("can't construct expression from relative traversal")
75 }
76
77 physT := newTraversal()
78 rootName := traversal.RootName()
79 steps := traversal[1:]
80
81 {
82 tn := newTraverseName()
83 tn.name = tn.children.Append(newIdentifier(&Token{
84 Type: hclsyntax.TokenIdent,
85 Bytes: []byte(rootName),
86 }))
87 physT.steps.Add(physT.children.Append(tn))
88 }
89
90 for _, step := range steps {
91 switch ts := step.(type) {
92 case hcl.TraverseAttr:
93 tn := newTraverseName()
94 tn.children.AppendUnstructuredTokens(Tokens{
95 {
96 Type: hclsyntax.TokenDot,
97 Bytes: []byte{'.'},
98 },
99 })
100 tn.name = tn.children.Append(newIdentifier(&Token{
101 Type: hclsyntax.TokenIdent,
102 Bytes: []byte(ts.Name),
103 }))
104 physT.steps.Add(physT.children.Append(tn))
105 case hcl.TraverseIndex:
106 ti := newTraverseIndex()
107 ti.children.AppendUnstructuredTokens(Tokens{
108 {
109 Type: hclsyntax.TokenOBrack,
110 Bytes: []byte{'['},
111 },
112 })
113 indexExpr := NewExpressionLiteral(ts.Key)
114 ti.key = ti.children.Append(indexExpr)
115 ti.children.AppendUnstructuredTokens(Tokens{
116 {
117 Type: hclsyntax.TokenCBrack,
118 Bytes: []byte{']'},
119 },
120 })
121 physT.steps.Add(physT.children.Append(ti))
122 }
123 }
124
125 expr := newExpression()
126 expr.absTraversals.Add(expr.children.Append(physT))
127 return expr
128}
129

Callers 1

SetAttributeTraversalMethod · 0.85

Calls 11

newTraversalFunction · 0.85
newTraverseNameFunction · 0.85
newIdentifierFunction · 0.85
newTraverseIndexFunction · 0.85
NewExpressionLiteralFunction · 0.85
newExpressionFunction · 0.85
IsRelativeMethod · 0.80
AddMethod · 0.80
RootNameMethod · 0.45
AppendMethod · 0.45

Tested by

no test coverage detected