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

Method decodeSpec

ext/dynblock/expand_spec.go:25–167  ·  view source on GitHub ↗
(blockS *hcl.BlockHeaderSchema, rawSpec *hcl.Block)

Source from the content-addressed store, hash-verified

23}
24
25func (b *expandBody) decodeSpec(blockS *hcl.BlockHeaderSchema, rawSpec *hcl.Block) (*expandSpec, hcl.Diagnostics) {
26 var diags hcl.Diagnostics
27
28 var schema *hcl.BodySchema
29 if len(blockS.LabelNames) != 0 {
30 schema = dynamicBlockBodySchemaLabels
31 } else {
32 schema = dynamicBlockBodySchemaNoLabels
33 }
34
35 specContent, specDiags := rawSpec.Body.Content(schema)
36 diags = append(diags, specDiags...)
37 if specDiags.HasErrors() {
38 return nil, diags
39 }
40
41 //// iterator attribute
42
43 iteratorName := blockS.Type
44 if iteratorAttr := specContent.Attributes["iterator"]; iteratorAttr != nil {
45 itTraversal, itDiags := hcl.AbsTraversalForExpr(iteratorAttr.Expr)
46 diags = append(diags, itDiags...)
47 if itDiags.HasErrors() {
48 return nil, diags
49 }
50
51 if len(itTraversal) != 1 {
52 diags = append(diags, &hcl.Diagnostic{
53 Severity: hcl.DiagError,
54 Summary: "Invalid dynamic iterator name",
55 Detail: "Dynamic iterator must be a single variable name.",
56 Subject: itTraversal.SourceRange().Ptr(),
57 })
58 return nil, diags
59 }
60
61 iteratorName = itTraversal.RootName()
62 }
63
64 //// for_each attribute
65
66 eachAttr := specContent.Attributes["for_each"]
67 eachVal, eachDiags := eachAttr.Expr.Value(b.forEachCtx)
68 diags = append(diags, eachDiags...)
69 if diags.HasErrors() {
70 return nil, diags
71 }
72 for _, check := range b.checkForEach {
73 moreDiags := check(eachVal, eachAttr.Expr, b.forEachCtx)
74 diags = append(diags, moreDiags...)
75 if moreDiags.HasErrors() {
76 return nil, diags
77 }
78 }
79
80 unmarkedEachVal, _ := eachVal.Unmark()
81 if !unmarkedEachVal.CanIterateElements() && unmarkedEachVal.Type() != cty.DynamicPseudoType {
82 // We skip this error for DynamicPseudoType because that means we either

Callers 1

expandBlocksMethod · 0.95

Calls 9

HasErrorsMethod · 0.95
PtrMethod · 0.80
ContentMethod · 0.65
SourceRangeMethod · 0.65
ValueMethod · 0.65
RangeMethod · 0.65
ExprListMethod · 0.65
RootNameMethod · 0.45
TypeMethod · 0.45

Tested by

no test coverage detected