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{})
| 314 | // may still be accessed by a careful caller for static analysis and editor |
| 315 | // integration use-cases. |
| 316 | func 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 | } |