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

Method expandBlocks

ext/dynblock/expand_body.go:169–252  ·  view source on GitHub ↗
(schema *hcl.BodySchema, rawBlocks hcl.Blocks, partial bool)

Source from the content-addressed store, hash-verified

167}
168
169func (b *expandBody) expandBlocks(schema *hcl.BodySchema, rawBlocks hcl.Blocks, partial bool) (hcl.Blocks, hcl.Diagnostics) {
170 var blocks hcl.Blocks
171 var diags hcl.Diagnostics
172
173 for _, rawBlock := range rawBlocks {
174 switch rawBlock.Type {
175 case "dynamic":
176 realBlockType := rawBlock.Labels[0]
177 if _, hidden := b.hiddenBlocks[realBlockType]; hidden {
178 continue
179 }
180
181 var blockS *hcl.BlockHeaderSchema
182 for _, candidate := range schema.Blocks {
183 if candidate.Type == realBlockType {
184 blockS = &candidate
185 break
186 }
187 }
188 if blockS == nil {
189 // Not a block type that the caller requested.
190 if !partial {
191 diags = append(diags, &hcl.Diagnostic{
192 Severity: hcl.DiagError,
193 Summary: "Unsupported block type",
194 Detail: fmt.Sprintf("Blocks of type %q are not expected here.", realBlockType),
195 Subject: &rawBlock.LabelRanges[0],
196 })
197 }
198 continue
199 }
200
201 spec, specDiags := b.decodeSpec(blockS, rawBlock)
202 diags = append(diags, specDiags...)
203 if specDiags.HasErrors() {
204 continue
205 }
206
207 forEachVal, marks := spec.forEachVal.Unmark()
208 if forEachVal.IsKnown() {
209 for it := forEachVal.ElementIterator(); it.Next(); {
210 key, value := it.Element()
211 i := b.iteration.MakeChild(spec.iteratorName, key, value)
212
213 block, blockDiags := spec.newBlock(i, b.forEachCtx)
214 diags = append(diags, blockDiags...)
215 if block != nil {
216 // Attach our new iteration context so that attributes
217 // and other nested blocks can refer to our iterator.
218 block.Body = b.expandChild(block.Body, i, marks)
219 blocks = append(blocks, block)
220 }
221 }
222 } else {
223 // If our top-level iteration value isn't known then we
224 // substitute an unknownBody, which will cause the entire block
225 // to evaluate to an unknown value.
226 i := b.iteration.MakeChild(spec.iteratorName, cty.DynamicVal, cty.DynamicVal)

Callers 2

ContentMethod · 0.95
PartialContentMethod · 0.95

Calls 5

decodeSpecMethod · 0.95
expandChildMethod · 0.95
HasErrorsMethod · 0.80
MakeChildMethod · 0.80
newBlockMethod · 0.80

Tested by

no test coverage detected