ApplyPath is a helper function that applies a cty.Path to a value using the indexing and attribute access operations from HCL. This is similar to calling the path's own Apply method, but ApplyPath uses the more relaxed typing rules that apply to these operations in HCL, rather than cty's relatively
(val cty.Value, path cty.Path, srcRange *Range)
| 406 | // diagnostics, though nil can be provided if the calling application is going |
| 407 | // to ignore the subject of the returned diagnostics anyway. |
| 408 | func ApplyPath(val cty.Value, path cty.Path, srcRange *Range) (cty.Value, Diagnostics) { |
| 409 | var diags Diagnostics |
| 410 | |
| 411 | for _, step := range path { |
| 412 | var stepDiags Diagnostics |
| 413 | switch ts := step.(type) { |
| 414 | case cty.IndexStep: |
| 415 | val, stepDiags = Index(val, ts.Key, srcRange) |
| 416 | case cty.GetAttrStep: |
| 417 | val, stepDiags = GetAttr(val, ts.Name, srcRange) |
| 418 | default: |
| 419 | // Should never happen because the above are all of the step types. |
| 420 | diags = diags.Append(&Diagnostic{ |
| 421 | Severity: DiagError, |
| 422 | Summary: "Invalid path step", |
| 423 | Detail: fmt.Sprintf("Go type %T is not a valid path step. This is a bug in this program.", step), |
| 424 | Subject: srcRange, |
| 425 | }) |
| 426 | return cty.DynamicVal, diags |
| 427 | } |
| 428 | |
| 429 | diags = append(diags, stepDiags...) |
| 430 | if stepDiags.HasErrors() { |
| 431 | return cty.DynamicVal, diags |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return val, diags |
| 436 | } |