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

Method Visit

ext/dynblock/variables.go:73–173  ·  view source on GitHub ↗

Visit returns the variable traversals required for any "dynamic" blocks directly in the body associated with this node, and also returns any child nodes that must be visited in order to continue the walk. Each child node has its associated block type name given in its BlockTypeName field, which the

(schema *hcl.BodySchema)

Source from the content-addressed store, hash-verified

71// schema for the content of each child node and pass it to the child node's
72// own Visit method to continue the walk recursively.
73func (n WalkVariablesNode) Visit(schema *hcl.BodySchema) (vars []hcl.Traversal, children []WalkVariablesChild) {
74 extSchema := n.extendSchema(schema)
75 container, _, _ := n.body.PartialContent(extSchema)
76 if container == nil {
77 return vars, children
78 }
79
80 children = make([]WalkVariablesChild, 0, len(container.Blocks))
81
82 if n.includeContent {
83 for _, attr := range container.Attributes {
84 for _, traversal := range attr.Expr.Variables() {
85 var ours, inherited bool
86 if n.it != nil {
87 ours = traversal.RootName() == n.it.IteratorName
88 _, inherited = n.it.Inherited[traversal.RootName()]
89 }
90
91 if !ours && !inherited {
92 vars = append(vars, traversal)
93 }
94 }
95 }
96 }
97
98 for _, block := range container.Blocks {
99 switch block.Type {
100
101 case "dynamic":
102 blockTypeName := block.Labels[0]
103 inner, _, _ := block.Body.PartialContent(variableDetectionInnerSchema)
104 if inner == nil {
105 continue
106 }
107
108 iteratorName := blockTypeName
109 if attr, exists := inner.Attributes["iterator"]; exists {
110 iterTraversal, _ := hcl.AbsTraversalForExpr(attr.Expr)
111 if len(iterTraversal) == 0 {
112 // Ignore this invalid dynamic block, since it'll produce
113 // an error if someone tries to extract content from it
114 // later anyway.
115 continue
116 }
117 iteratorName = iterTraversal.RootName()
118 }
119 blockIt := n.it.MakeChild(iteratorName, cty.DynamicVal, cty.DynamicVal)
120
121 if attr, exists := inner.Attributes["for_each"]; exists {
122 // Filter out iterator names inherited from parent blocks
123 for _, traversal := range attr.Expr.Variables() {
124 if _, inherited := blockIt.Inherited[traversal.RootName()]; !inherited {
125 vars = append(vars, traversal)
126 }
127 }
128 }
129 if attr, exists := inner.Attributes["labels"]; exists {
130 // Filter out both our own iterator name _and_ those inherited

Callers 1

walkVariablesWithHCLDecFunction · 0.80

Calls 5

extendSchemaMethod · 0.95
MakeChildMethod · 0.80
PartialContentMethod · 0.65
VariablesMethod · 0.65
RootNameMethod · 0.45

Tested by

no test coverage detected