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

Method newBlock

ext/dynblock/expand_spec.go:169–253  ·  view source on GitHub ↗
(i *iteration, ctx *hcl.EvalContext)

Source from the content-addressed store, hash-verified

167}
168
169func (s *expandSpec) newBlock(i *iteration, ctx *hcl.EvalContext) (*hcl.Block, hcl.Diagnostics) {
170 var diags hcl.Diagnostics
171 var labels []string
172 var labelRanges []hcl.Range
173 lCtx := i.EvalContext(ctx)
174 for _, labelExpr := range s.labelExprs {
175 labelVal, labelDiags := labelExpr.Value(lCtx)
176 diags = append(diags, labelDiags...)
177 if labelDiags.HasErrors() {
178 return nil, diags
179 }
180
181 var convErr error
182 labelVal, convErr = convert.Convert(labelVal, cty.String)
183 if convErr != nil {
184 diags = append(diags, &hcl.Diagnostic{
185 Severity: hcl.DiagError,
186 Summary: "Invalid dynamic block label",
187 Detail: fmt.Sprintf("Cannot use this value as a dynamic block label: %s.", convErr),
188 Subject: labelExpr.Range().Ptr(),
189 Expression: labelExpr,
190 EvalContext: lCtx,
191 })
192 return nil, diags
193 }
194 if labelVal.IsNull() {
195 diags = append(diags, &hcl.Diagnostic{
196 Severity: hcl.DiagError,
197 Summary: "Invalid dynamic block label",
198 Detail: "Cannot use a null value as a dynamic block label.",
199 Subject: labelExpr.Range().Ptr(),
200 Expression: labelExpr,
201 EvalContext: lCtx,
202 })
203 return nil, diags
204 }
205 if !labelVal.IsKnown() {
206 diags = append(diags, &hcl.Diagnostic{
207 Severity: hcl.DiagError,
208 Summary: "Invalid dynamic block label",
209 Detail: "This value is not yet known. Dynamic block labels must be immediately-known values.",
210 Subject: labelExpr.Range().Ptr(),
211 Expression: labelExpr,
212 EvalContext: lCtx,
213 })
214 return nil, diags
215 }
216 if labelVal.IsMarked() {
217 // This situation is tricky because HCL just works generically
218 // with marks and so doesn't have any good language to talk about
219 // the meaning of specific mark types, but yet we cannot allow
220 // marked values here because the HCL API guarantees that a block's
221 // labels are always known static constant Go strings.
222 // Therefore this is a low-quality error message but at least
223 // better than panicking below when we call labelVal.AsString.
224 // If this becomes a problem then we could potentially add a new
225 // option for the public function [Expand] to allow calling
226 // applications to specify custom label validation functions that

Callers 1

expandBlocksMethod · 0.80

Calls 5

EvalContextMethod · 0.80
HasErrorsMethod · 0.80
PtrMethod · 0.80
ValueMethod · 0.65
RangeMethod · 0.65

Tested by

no test coverage detected