TraverseAbs applies the receiving traversal to the given eval context, returning the resulting value. This is supported only for absolute traversals, and will panic if applied to a relative traversal.
(ctx *EvalContext)
| 63 | // returning the resulting value. This is supported only for absolute |
| 64 | // traversals, and will panic if applied to a relative traversal. |
| 65 | func (t Traversal) TraverseAbs(ctx *EvalContext) (cty.Value, Diagnostics) { |
| 66 | if t.IsRelative() { |
| 67 | panic("can't use TraverseAbs on a relative traversal") |
| 68 | } |
| 69 | |
| 70 | split := t.SimpleSplit() |
| 71 | root := split.Abs[0].(TraverseRoot) |
| 72 | name := root.Name |
| 73 | |
| 74 | thisCtx := ctx |
| 75 | hasNonNil := false |
| 76 | for thisCtx != nil { |
| 77 | if thisCtx.Variables == nil { |
| 78 | thisCtx = thisCtx.parent |
| 79 | continue |
| 80 | } |
| 81 | hasNonNil = true |
| 82 | val, exists := thisCtx.Variables[name] |
| 83 | if exists { |
| 84 | return split.Rel.TraverseRel(val) |
| 85 | } |
| 86 | thisCtx = thisCtx.parent |
| 87 | } |
| 88 | |
| 89 | if !hasNonNil { |
| 90 | return cty.DynamicVal, Diagnostics{ |
| 91 | { |
| 92 | Severity: DiagError, |
| 93 | Summary: "Variables not allowed", |
| 94 | Detail: "Variables may not be used here.", |
| 95 | Subject: &root.SrcRange, |
| 96 | }, |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | suggestions := make([]string, 0, len(ctx.Variables)) |
| 101 | thisCtx = ctx |
| 102 | for thisCtx != nil { |
| 103 | for k := range thisCtx.Variables { |
| 104 | suggestions = append(suggestions, k) |
| 105 | } |
| 106 | thisCtx = thisCtx.parent |
| 107 | } |
| 108 | suggestion := nameSuggestion(name, suggestions) |
| 109 | if suggestion != "" { |
| 110 | suggestion = fmt.Sprintf(" Did you mean %q?", suggestion) |
| 111 | } |
| 112 | |
| 113 | return cty.DynamicVal, Diagnostics{ |
| 114 | { |
| 115 | Severity: DiagError, |
| 116 | Summary: "Unknown variable", |
| 117 | Detail: fmt.Sprintf("There is no variable named %q.%s", name, suggestion), |
| 118 | Subject: &root.SrcRange, |
| 119 | }, |
| 120 | } |
| 121 | } |
| 122 |
no test coverage detected