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

Function ExprAsKeyword

traversal_for_expr.go:111–127  ·  view source on GitHub ↗

ExprAsKeyword attempts to interpret the given expression as a static keyword, returning the keyword string if possible, and the empty string if not. A static keyword, for the sake of this function, is a single identifier. For example, the following attribute has an expression that would produce the

(expr Expression)

Source from the content-addressed store, hash-verified

109// a fixed set of keywords is used in a structural way in a special attribute
110// to affect the further processing of a block.
111func ExprAsKeyword(expr Expression) string {
112 type asTraversal interface {
113 AsTraversal() Traversal
114 }
115
116 physExpr := UnwrapExpressionUntil(expr, func(expr Expression) bool {
117 _, supported := expr.(asTraversal)
118 return supported
119 })
120
121 if asT, supported := physExpr.(asTraversal); supported {
122 if traversal := asT.AsTraversal(); len(traversal) == 1 {
123 return traversal.RootName()
124 }
125 }
126 return ""
127}

Callers 1

TestExprAsKeywordFunction · 0.85

Calls 3

UnwrapExpressionUntilFunction · 0.85
AsTraversalMethod · 0.65
RootNameMethod · 0.45

Tested by 1

TestExprAsKeywordFunction · 0.68