/
apply_defaults.go
375 lines (344 loc) · 9.45 KB
/
apply_defaults.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright 2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package infer
import (
"fmt"
"os"
"reflect"
"strconv"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi-go-provider/internal/introspect"
)
// The object that controls default application.
type defaultsWalker struct {
// seen is the stack of types that defaultsWalker has descended into.
seen []reflect.Type
}
// Mark that we are visiting a type.
//
// The return value of of this function should be immediately deferred:
//
// defer d.visit(t)()
func (d *defaultsWalker) visit(t reflect.Type) func() {
d.seen = append(d.seen, t)
pop := func() {
d.seen = d.seen[:len(d.seen)-1]
}
return pop
}
func (d *defaultsWalker) hydrate(t reflect.Type) bool {
for _, v := range d.seen {
if v == t {
return false
}
}
return true
}
// apply default values to structs.
func (d *defaultsWalker) apply(v reflect.Value) (bool, error) {
t := v.Type()
defer d.visit(t)()
// We get the set of default types that could be applied to v.
a := getAnnotated(t)
fields := map[string]reflect.Value{}
optional := map[string]bool{}
for _, field := range reflect.VisibleFields(v.Type()) {
tag, err := introspect.ParseTag(field)
if err != nil {
return false, err
}
if tag.Internal {
continue
}
optional[tag.Name] = tag.Optional
fields[tag.Name] = v.FieldByIndex(field.Index)
}
// We not apply the defaults we calculated:
//
// We start by attempting to apply environmental values in order. If no
// environmental values are set to a non "" value, we then set from in-memory
// values.
// If v is a nil valued struct with defaults, we hydrate it and apply the
// default. If not, we leave it nil.
var didSet bool
defaultEnvs:
for k, envVars := range a.DefaultEnvs {
value, ok := fields[k]
if ok && !value.IsZero() {
continue
}
for _, env := range envVars {
envValue := os.Getenv(env)
if envValue == "" {
continue
}
err := setDefaultValueFromEnv(value, envValue)
if err != nil {
return false, err
}
didSet = true
continue defaultEnvs
}
}
for k, inMemoryDefault := range a.Defaults {
value, ok := fields[k]
if ok && !value.IsZero() {
continue
}
err := setDefaultFromMemory(value, inMemoryDefault)
if err != nil {
return false, err
}
didSet = true
}
// Default values only apply to primitive types, but this struct could have fields
// that itself has default values. We need to traverse those.
//
// We only recurse on pulumi tagged fields.
for k, v := range fields {
// We do not apply defaults (or hydrate) a field `K: *T` when `K` is
// optional and `*T` is nil. This shields us from the hydrating an
// optional struct with a required value, which fails de-serialization.
if optional[k] && isNilStructPtr(v) {
continue
}
didSetRec, err := d.walk(v)
if err != nil {
return false, err
}
if didSetRec {
didSet = true
}
}
return didSet, nil
}
// isNilStructPtr checks if v is a nil pointer to a struct.
func isNilStructPtr(v reflect.Value) bool {
for v.Kind() == reflect.Pointer && !v.IsNil() {
v = v.Elem()
}
t := v.Type()
for t.Kind() == reflect.Pointer {
t = t.Elem()
}
return v.Kind() == reflect.Pointer && t.Kind() == reflect.Struct
}
// walk is responsible for calling applyDefaults on all structs in reachable from value.
func (d *defaultsWalker) walk(v reflect.Value) (didSet bool, _ error) {
tDeferenced := v.Type()
for tDeferenced.Kind() == reflect.Pointer {
tDeferenced = tDeferenced.Elem()
}
switch tDeferenced.Kind() {
// We apply defaults to each value in slice.
case reflect.Slice:
// Dereference to the underlying slice
for v.Kind() == reflect.Pointer && !v.IsNil() {
v = v.Elem()
}
// Either we have a type *[]T, **[]T, etc. and a pointer is nil,
// or we have reached the slice and the slice itself is nil. Both
// cases prevent us finding any more structs, so we are done.
if v.IsNil() {
return didSet, nil
}
for i := 0; i < v.Len(); i++ {
didSetRec, err := d.walk(v.Index(i))
if err != nil {
return false, err
}
if didSetRec {
didSet = true
}
}
return didSet, nil
case reflect.Array:
// Dereference to the underlying slice
for v.Kind() == reflect.Pointer {
if v.IsNil() {
return false, nil
}
v = v.Elem()
}
// Arrays cannot be nil, so we don't (and can't) perform a nil
// check here.
for i := 0; i < v.Len(); i++ {
didSetRec, err := d.walk(v.Index(i))
if err != nil {
return false, err
}
if didSetRec {
didSet = true
}
}
return didSet, nil
case reflect.Map:
// Dereference to the underlying map
for v.Kind() == reflect.Pointer && !v.IsNil() {
v = v.Elem()
}
// Either we have a type *map[K]V, **map[K]V, etc. and a pointer
// is nil, or we have reached the map and the map itself is
// nil. Both cases prevent us finding any more structs, so we are
// done.
if v.IsNil() {
return false, nil
}
iter := v.MapRange()
for iter.Next() {
value := reflect.New(iter.Value().Type()).Elem()
value.Set(iter.Value())
s := hydratedValue(value)
didSetRec, err := d.walk(s)
if err != nil {
return false, err
}
if didSetRec {
v.SetMapIndex(iter.Key(), s)
didSet = true
}
}
return didSet, nil
case reflect.Struct:
// Copying what Go SDKs do, we only populate structs that are accessible
// by value or by filled pointers.
structCopy := reflect.New(v.Type()).Elem()
structCopy.Set(v)
var s reflect.Value
if d.hydrate(tDeferenced) {
// We should hydrate the value and apply defaults to it.
s = hydratedValue(structCopy)
} else {
// We should fill a live value, but not hydrate a new one. Set s
// to the copy, assuming that we will have a non-nil struct.
s = structCopy
// Now check that we have a non-nil struct.
for structCopy.Kind() == reflect.Pointer {
if structCopy.IsNil() {
return false, nil
}
structCopy = structCopy.Elem()
}
}
didSet, err := d.apply(derefNonNil(s))
if err != nil {
return false, err
}
if didSet {
v.Set(s)
}
return didSet, nil
// This is a primitive type. That means that:
//
// 1. It is not a struct.
// 2. It cannot contain a struct.
//
// That means there are not any defaults to apply, so we're done.
default:
return false, nil
}
}
// applyDefaults recursively applies the default values provided by [introspect.Annotator].
func applyDefaults[T any](value *T) error {
v := reflect.ValueOf(value).Elem()
contract.Assertf(v.CanSet(), "Cannot accept an un-editable pointer")
var walker defaultsWalker
_, err := walker.walk(v)
return err
}
// setDefaultFromMemory applies an in-memory default value to a field that can accept one.
//
// field must be CanSet and value must either be a primitive, or point to one.
func setDefaultFromMemory(field reflect.Value, value any) error {
if value == nil {
return nil
}
// We will set field to a primitive value, we can freely provide hydration.
field.Set(hydratedValue(field))
field = derefNonNil(field)
v := reflect.ValueOf(value)
if v.CanConvert(field.Type()) {
field.Set(v.Convert(field.Type()))
return nil
}
return fmt.Errorf("cannot set field of type '%s' to default value %q (%[2]T)",
field.Type(), value)
}
// setDefaultValueFromEnv applies a default value source from an environmental variable to
// a field.
//
// field must be CanSet and value must either be a primitive, or point to one.
func setDefaultValueFromEnv(field reflect.Value, value string) error {
typ := field.Type()
for typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
switch typ.Kind() {
case reflect.String:
return setDefaultFromMemory(field, value)
case reflect.Float64:
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
return setDefaultFromMemory(field, f)
case reflect.Int:
i, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return err
}
return setDefaultFromMemory(field, i)
case reflect.Bool:
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
return setDefaultFromMemory(field, b)
}
return fmt.Errorf("unable to apply default value %q to field of type %s",
value, typ)
}
// hydratedValue takes a (possibly ptr) and returns a fully hydrated value of the same
// type. For example:
//
// T{} -> T{}
// T{N: 1} -> T{N: 1}
// &T{N: 2} -> &T{N: 2}
// (*T)(nil) -> &T{}
// (**T)(nil) -> &&T{}
func hydratedValue(value reflect.Value) reflect.Value {
// root := *&T where T = typeof(value)
//
// This creates an addressable value of the same type as value
root := value
for value.Kind() == reflect.Pointer {
// We have *T, so we need to construct a T and set the value to it.
if value.IsNil() {
// This is the reflect equivalent of:
//
// var v typeof(value)
// *elem := value
v := reflect.New(value.Type().Elem())
value.Set(v)
}
value = value.Elem()
}
return root
}
func derefNonNil(value reflect.Value) reflect.Value {
for value.Kind() == reflect.Pointer {
value = value.Elem()
}
return value
}