/
walk.go
349 lines (268 loc) · 9.46 KB
/
walk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package tftypes
// these functions are based heavily on github.com/zclconf/go-cty
// used under the MIT License
//
// Copyright (c) 2017-2018 Martin Atkins
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import (
"errors"
)
// stopWalkError is a well-known error for immediately stopping walk() without
// returning an actual error.
//
// The implementation of walk() will continue walking all attributes/elements
// within an object/collection since the boolean return value of the callback
// function is only intended to signal whether to stop descending into the same
// Value. Changing that behavior would be considered a breaking change.
//
// This could be considered for exporting to give external consumers better
// performance.
var stopWalkError = errors.New("walk stop requested")
// Walk traverses a Value, calling the passed function for every element and
// attribute in the Value. The AttributePath passed to the callback function
// will identify which attribute or element is currently being surfaced by the
// Walk, and the passed Value will be the element or attribute at that
// AttributePath. Returning true from the callback function will indicate that
// any attributes or elements of the surfaced Value should be walked, too;
// returning false short-circuits the walk at that element or attribute, and
// does not visit any of its descendants. The return value of the callback does
// not matter when the Value that has been surfaced has no elements or
// attributes. Walk uses a depth-first traversal.
func Walk(val Value, cb func(*AttributePath, Value) (bool, error)) error {
_, err := walk(NewAttributePath(), val, cb)
return err
}
// walk is the internal implementation of Walk(). It includes a bool return for
// whether callers should continue walking any remaining Value.
func walk(path *AttributePath, val Value, cb func(*AttributePath, Value) (bool, error)) (bool, error) {
shouldContinue, err := cb(path, val)
if errors.Is(err, stopWalkError) {
return false, nil
}
if err != nil {
return false, path.NewError(err)
}
if !shouldContinue {
// The callback bool return is intended to signal that this Value should
// no longer be descended. Changing this behavior is a breaking change.
// A stopWalkError can be used to signal that all remaining Value can be
// skipped.
return true, nil
}
if val.IsNull() || !val.IsKnown() {
return true, nil
}
switch val.Type().(type) {
case List, Tuple:
v, ok := val.value.([]Value)
if !ok {
return false, path.NewErrorf("cannot convert %T into []tftypes.Value", val.value)
}
for pos, el := range v {
elementPath := path.WithElementKeyInt(pos)
shouldContinue, err := walk(elementPath, el, cb)
if err != nil {
return false, elementPath.NewError(err)
}
if !shouldContinue {
return false, nil
}
}
case Map:
v, ok := val.value.(map[string]Value)
if !ok {
return false, path.NewErrorf("cannot convert %T into map[string]tftypes.Value", val.value)
}
for k, el := range v {
elementPath := path.WithElementKeyString(k)
shouldContinue, err := walk(elementPath, el, cb)
if err != nil {
return false, elementPath.NewError(err)
}
if !shouldContinue {
return false, nil
}
}
case Object:
v, ok := val.value.(map[string]Value)
if !ok {
return false, path.NewErrorf("cannot convert %T into map[string]tftypes.Value", val.value)
}
for k, el := range v {
attributePath := path.WithAttributeName(k)
shouldContinue, err := walk(attributePath, el, cb)
if err != nil {
return false, attributePath.NewError(err)
}
if !shouldContinue {
return false, nil
}
}
case Set:
v, ok := val.value.([]Value)
if !ok {
return false, path.NewErrorf("cannot convert %T into []tftypes.Value", val.value)
}
for _, el := range v {
elementPath := path.WithElementKeyValue(el)
shouldContinue, err := walk(elementPath, el, cb)
if err != nil {
return false, elementPath.NewError(err)
}
if !shouldContinue {
return false, nil
}
}
}
return true, nil
}
// Transform uses a callback to mutate a Value. Each element or attribute will
// be visited in turn, with the AttributePath and Value surfaced to the
// callback, as in Walk. Unlike in Walk, the callback returns a Value instead
// of a boolean; this is the Value that will be stored at that AttributePath.
// The callback must return the passed Value unmodified if it wishes to not
// mutate a Value. Elements and attributes of a Value will be passed to the
// callback prior to the Value they belong to being passed to the callback,
// which means a callback can overwrite its own modifications. Values passed to
// the callback will always reflect the results of earlier callback calls.
func Transform(val Value, cb func(*AttributePath, Value) (Value, error)) (Value, error) {
return transform(NewAttributePath(), val, cb)
}
func transform(path *AttributePath, val Value, cb func(*AttributePath, Value) (Value, error)) (Value, error) {
switch val.Type().(type) {
case nil:
return val, path.NewError(errors.New("invalid transform: value missing type"))
}
newVal, err := transformUnderlying(path, val, cb)
if err != nil {
return val, err
}
res, err := cb(path, newVal)
if err != nil {
return res, path.NewError(err)
}
newTy := newVal.Type()
if newTy == nil {
return val, path.NewError(errors.New("invalid transform: new value missing type"))
}
if !newTy.UsableAs(val.Type()) {
return val, path.NewError(errors.New("invalid transform: value changed type"))
}
return res, err
}
// transformUnderlying returns the Value with any underlying attribute or
// element transformations completed.
func transformUnderlying(path *AttributePath, val Value, cb func(*AttributePath, Value) (Value, error)) (Value, error) {
// If the Value is null or unknown, there is nothing to descend.
if val.IsNull() || !val.IsKnown() {
return val, nil
}
switch val.Type().(type) {
case List, Tuple:
elements, ok := val.value.([]Value)
if !ok {
return val, path.NewErrorf("cannot convert %T into []tftypes.Value", val.value)
}
if len(elements) == 0 {
return val, nil
}
newElements := make([]Value, 0, len(elements))
for index, element := range elements {
elementPath := path.WithElementKeyInt(index)
newElement, err := transform(elementPath, element, cb)
if err != nil {
return val, elementPath.NewError(err)
}
newElements = append(newElements, newElement)
}
newVal, err := newValue(val.Type(), newElements)
if err != nil {
return val, path.NewError(err)
}
return newVal, nil
case Map:
elements, ok := val.value.(map[string]Value)
if !ok {
return val, path.NewErrorf("cannot convert %T into map[string]tftypes.Value", val.value)
}
if len(elements) == 0 {
return val, nil
}
newElements := make(map[string]Value, len(elements))
for key, element := range elements {
elementPath := path.WithElementKeyString(key)
newElement, err := transform(elementPath, element, cb)
if err != nil {
return val, elementPath.NewError(err)
}
newElements[key] = newElement
}
newVal, err := newValue(val.Type(), newElements)
if err != nil {
return val, path.NewError(err)
}
return newVal, nil
case Object:
attributes, ok := val.value.(map[string]Value)
if !ok {
return val, path.NewErrorf("cannot convert %T into map[string]tftypes.Value", val.value)
}
if len(attributes) == 0 {
return val, nil
}
newAttributes := make(map[string]Value, len(attributes))
for name, attribute := range attributes {
attributePath := path.WithAttributeName(name)
newAttribute, err := transform(attributePath, attribute, cb)
if err != nil {
return val, attributePath.NewError(err)
}
newAttributes[name] = newAttribute
}
newVal, err := newValue(val.Type(), newAttributes)
if err != nil {
return val, path.NewError(err)
}
return newVal, nil
case Set:
elements, ok := val.value.([]Value)
if !ok {
return val, path.NewErrorf("cannot convert %T into []tftypes.Value", val.value)
}
if len(elements) == 0 {
return val, nil
}
newElements := make([]Value, 0, len(elements))
for _, element := range elements {
elementPath := path.WithElementKeyValue(element)
newElement, err := transform(elementPath, element, cb)
if err != nil {
return val, elementPath.NewError(err)
}
newElements = append(newElements, newElement)
}
newVal, err := newValue(val.Type(), newElements)
if err != nil {
return val, path.NewError(err)
}
return newVal, nil
}
return val, nil
}