8 "github.com/hashicorp/hil/ast"
9 "github.com/mitchellh/reflectwalk"
12 // WalkFn is the type of function to pass to Walk. Modify fields within
13 // WalkData to control whether replacement happens.
14 type WalkFn func(*WalkData) error
16 // WalkData is the structure passed to the callback of the Walk function.
18 // This structure contains data passed in as well as fields that are expected
19 // to be written by the caller as a result. Please see the documentation for
20 // each field for more information.
21 type WalkData struct {
22 // Root is the parsed root of this HIL program
25 // Location is the location within the structure where this
26 // value was found. This can be used to modify behavior within
28 Location reflectwalk.Location
30 // The below two values must be set by the callback to have any effect.
32 // Replace, if true, will replace the value in the structure with
33 // ReplaceValue. It is up to the caller to make sure this is a string.
38 // Walk will walk an arbitrary Go structure and parse any string as an
39 // HIL program and call the callback cb to determine what to replace it
42 // This function is very useful for arbitrary HIL program interpolation
43 // across a complex configuration structure. Due to the heavy use of
44 // reflection in this function, it is recommend to write many unit tests
45 // with your typical configuration structures to hilp mitigate the risk
47 func Walk(v interface{}, cb WalkFn) error {
48 walker := &interpolationWalker{F: cb}
49 return reflectwalk.Walk(v, walker)
52 // interpolationWalker implements interfaces for the reflectwalk package
53 // (github.com/mitchellh/reflectwalk) that can be used to automatically
54 // execute a callback for an interpolation.
55 type interpolationWalker struct {
59 lastValue reflect.Value
60 loc reflectwalk.Location
68 func (w *interpolationWalker) Enter(loc reflectwalk.Location) error {
73 func (w *interpolationWalker) Exit(loc reflectwalk.Location) error {
74 w.loc = reflectwalk.None
78 w.cs = w.cs[:len(w.cs)-1]
79 case reflectwalk.MapValue:
80 w.key = w.key[:len(w.key)-1]
81 w.csKey = w.csKey[:len(w.csKey)-1]
82 case reflectwalk.Slice:
83 // Split any values that need to be split
85 w.cs = w.cs[:len(w.cs)-1]
86 case reflectwalk.SliceElem:
87 w.csKey = w.csKey[:len(w.csKey)-1]
93 func (w *interpolationWalker) Map(m reflect.Value) error {
94 w.cs = append(w.cs, m)
98 func (w *interpolationWalker) MapElem(m, k, v reflect.Value) error {
100 w.csKey = append(w.csKey, k)
101 w.key = append(w.key, k.String())
106 func (w *interpolationWalker) Slice(s reflect.Value) error {
107 w.cs = append(w.cs, s)
111 func (w *interpolationWalker) SliceElem(i int, elem reflect.Value) error {
112 w.csKey = append(w.csKey, reflect.ValueOf(i))
117 func (w *interpolationWalker) Primitive(v reflect.Value) error {
120 // We only care about strings
121 if v.Kind() == reflect.Interface {
125 if v.Kind() != reflect.String {
129 astRoot, err := Parse(v.String())
134 // If the AST we got is just a literal string value with the same
135 // value then we ignore it. We have to check if its the same value
136 // because it is possible to input a string, get out a string, and
137 // have it be different. For example: "foo-$${bar}" turns into
139 if n, ok := astRoot.(*ast.LiteralNode); ok {
140 if s, ok := n.Value.(string); ok && s == v.String() {
149 data := WalkData{Root: astRoot, Location: w.loc}
150 if err := w.F(&data); err != nil {
164 resultVal := reflect.ValueOf(data.ReplaceValue)
166 case reflectwalk.MapKey:
167 m := w.cs[len(w.cs)-1]
169 // Delete the old value
170 var zero reflect.Value
171 m.SetMapIndex(w.csData.(reflect.Value), zero)
173 // Set the new key with the existing value
174 m.SetMapIndex(resultVal, w.lastValue)
176 // Set the key to be the new key
178 case reflectwalk.MapValue:
179 // If we're in a map, then the only way to set a map value is
180 // to set it directly.
181 m := w.cs[len(w.cs)-1]
182 mk := w.csData.(reflect.Value)
183 m.SetMapIndex(mk, resultVal)
185 // Otherwise, we should be addressable
193 func (w *interpolationWalker) removeCurrent() {
194 // Append the key to the unknown keys
195 w.unknownKeys = append(w.unknownKeys, strings.Join(w.key, "."))
197 for i := 1; i <= len(w.cs); i++ {
198 c := w.cs[len(w.cs)-i]
201 // Zero value so that we delete the map key
202 var val reflect.Value
204 // Get the key and delete it
205 k := w.csData.(reflect.Value)
206 c.SetMapIndex(k, val)
211 panic("No container found for removeCurrent")
214 func (w *interpolationWalker) replaceCurrent(v reflect.Value) {
215 c := w.cs[len(w.cs)-2]
218 // Get the key and delete it
219 k := w.csKey[len(w.csKey)-1]
224 func (w *interpolationWalker) splitSlice() {
225 // Get the []interface{} slice so we can do some operations on
226 // it without dealing with reflection. We'll document each step
229 raw := w.cs[len(w.cs)-1]
230 switch v := raw.Interface().(type) {
233 case []map[string]interface{}:
236 panic("Unknown kind: " + raw.Kind().String())
239 // Check if we have any elements that we need to split. If not, then
240 // just return since we're done.
246 // Make a new result slice that is twice the capacity to fit our growth.
247 result := make([]interface{}, 0, len(s)*2)
249 // Go over each element of the original slice and start building up
250 // the resulting slice by splitting where we have to.
251 for _, v := range s {
254 // Not a string, so just set it
255 result = append(result, v)
259 // Not a string list, so just set it
260 result = append(result, sv)
263 // Our slice is now done, we have to replace the slice now
264 // with this new one that we have.
265 w.replaceCurrent(reflect.ValueOf(result))