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

Function DecodeExpression

gohcl/decode.go:316–348  ·  gohcl/decode.go::DecodeExpression

DecodeExpression extracts the value of the given expression into the given value. This value must be something that gocty is able to decode into, since the final decoding is delegated to that package. The given EvalContext is used to resolve any variables or functions in expressions encountered whi

(expr hcl.Expression, ctx *hcl.EvalContext, val interface{})

Source from the content-addressed store, hash-verified

314// may still be accessed by a careful caller for static analysis and editor
315// integration use-cases.
316func DecodeExpression(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics {
317 srcVal, diags := expr.Value(ctx)
318
319 convTy, err := gocty.ImpliedType(val)
320 if err != nil {
321 panic(fmt.Sprintf("unsuitable DecodeExpression target: %s", err))
322 }
323
324 srcVal, err = convert.Convert(srcVal, convTy)
325 if err != nil {
326 diags = append(diags, &hcl.Diagnostic{
327 Severity: hcl.DiagError,
328 Summary: "Unsuitable value type",
329 Detail: fmt.Sprintf("Unsuitable value: %s", err.Error()),
330 Subject: expr.StartRange().Ptr(),
331 Context: expr.Range().Ptr(),
332 })
333 return diags
334 }
335
336 err = gocty.FromCtyValue(srcVal, val)
337 if err != nil {
338 diags = append(diags, &hcl.Diagnostic{
339 Severity: hcl.DiagError,
340 Summary: "Unsuitable value type",
341 Detail: fmt.Sprintf("Unsuitable value: %s", err.Error()),
342 Subject: expr.StartRange().Ptr(),
343 Context: expr.Range().Ptr(),
344 })
345 }
346
347 return diags
348}

Callers 3

TestDecodeExpressionFunction · 0.85
decodeBodyToStructFunction · 0.85
decodeBodyToMapFunction · 0.85

Calls 5

PtrMethod · 0.80
ValueMethod · 0.65
StartRangeMethod · 0.65
RangeMethod · 0.65
ErrorMethod · 0.45

Tested by 1

TestDecodeExpressionFunction · 0.68