-
Notifications
You must be signed in to change notification settings - Fork 91
/
interfaces.go
327 lines (289 loc) · 10.8 KB
/
interfaces.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
package reflect
import (
"context"
"fmt"
"reflect"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Unknownable is an interface for types that can be explicitly set to known or
// unknown.
type Unknownable interface {
SetUnknown(context.Context, bool) error
SetValue(context.Context, interface{}) error
GetUnknown(context.Context) bool
GetValue(context.Context) interface{}
}
// NewUnknownable creates a zero value of `target` (or the concrete type it's
// referencing, if it's a pointer) and calls its SetUnknown method.
//
// It is meant to be called through Into, not directly.
func NewUnknownable(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path *tftypes.AttributePath) (reflect.Value, diag.Diagnostics) {
var diags diag.Diagnostics
receiver := pointerSafeZeroValue(ctx, target)
method := receiver.MethodByName("SetUnknown")
if !method.IsValid() {
err := fmt.Errorf("cannot find SetUnknown method on type %s", receiver.Type().String())
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return target, diags
}
results := method.Call([]reflect.Value{
reflect.ValueOf(ctx),
reflect.ValueOf(!val.IsKnown()),
})
err := results[0].Interface()
if err != nil {
var underlyingErr error
switch e := err.(type) {
case error:
underlyingErr = e
default:
underlyingErr = fmt.Errorf("unknown error type %T: %v", e, e)
}
underlyingErr = fmt.Errorf("reflection error: %w", underlyingErr)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert into a value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+underlyingErr.Error(),
)
return target, diags
}
return receiver, diags
}
// FromUnknownable creates an attr.Value from the data in an Unknownable.
//
// It is meant to be called through FromValue, not directly.
func FromUnknownable(ctx context.Context, typ attr.Type, val Unknownable, path *tftypes.AttributePath) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
if val.GetUnknown(ctx) {
tfVal := tftypes.NewValue(typ.TerraformType(ctx), tftypes.UnknownValue)
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)
if diags.HasError() {
return nil, diags
}
}
res, err := typ.ValueFromTerraform(ctx, tfVal)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return res, nil
}
err := tftypes.ValidateValue(typ.TerraformType(ctx), val.GetValue(ctx))
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfVal := tftypes.NewValue(typ.TerraformType(ctx), val.GetValue(ctx))
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)
if diags.HasError() {
return nil, diags
}
}
res, err := typ.ValueFromTerraform(ctx, tfVal)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return res, nil
}
// Nullable is an interface for types that can be explicitly set to null.
type Nullable interface {
SetNull(context.Context, bool) error
SetValue(context.Context, interface{}) error
GetNull(context.Context) bool
GetValue(context.Context) interface{}
}
// NewNullable creates a zero value of `target` (or the concrete type it's
// referencing, if it's a pointer) and calls its SetNull method.
//
// It is meant to be called through Into, not directly.
func NewNullable(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path *tftypes.AttributePath) (reflect.Value, diag.Diagnostics) {
var diags diag.Diagnostics
receiver := pointerSafeZeroValue(ctx, target)
method := receiver.MethodByName("SetNull")
if !method.IsValid() {
err := fmt.Errorf("cannot find SetNull method on type %s", receiver.Type().String())
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return target, diags
}
results := method.Call([]reflect.Value{
reflect.ValueOf(ctx),
reflect.ValueOf(val.IsNull()),
})
err := results[0].Interface()
if err != nil {
var underlyingErr error
switch e := err.(type) {
case error:
underlyingErr = e
default:
underlyingErr = fmt.Errorf("unknown error type: %T", e)
}
underlyingErr = fmt.Errorf("reflection error: %w", underlyingErr)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert into a value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+underlyingErr.Error(),
)
return target, diags
}
return receiver, diags
}
// FromNullable creates an attr.Value from the data in a Nullable.
//
// It is meant to be called through FromValue, not directly.
func FromNullable(ctx context.Context, typ attr.Type, val Nullable, path *tftypes.AttributePath) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
if val.GetNull(ctx) {
tfVal := tftypes.NewValue(typ.TerraformType(ctx), nil)
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)
if diags.HasError() {
return nil, diags
}
}
res, err := typ.ValueFromTerraform(ctx, tfVal)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return res, nil
}
err := tftypes.ValidateValue(typ.TerraformType(ctx), val.GetValue(ctx))
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfVal := tftypes.NewValue(typ.TerraformType(ctx), val.GetValue(ctx))
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)
if diags.HasError() {
return nil, diags
}
}
res, err := typ.ValueFromTerraform(ctx, tfVal)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return res, diags
}
// NewValueConverter creates a zero value of `target` (or the concrete type
// it's referencing, if it's a pointer) and calls its FromTerraform5Value
// method.
//
// It is meant to be called through Into, not directly.
func NewValueConverter(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path *tftypes.AttributePath) (reflect.Value, diag.Diagnostics) {
var diags diag.Diagnostics
receiver := pointerSafeZeroValue(ctx, target)
method := receiver.MethodByName("FromTerraform5Value")
if !method.IsValid() {
err := fmt.Errorf("could not find FromTerraform5Type method on type %s", receiver.Type().String())
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert into a value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return target, diags
}
results := method.Call([]reflect.Value{reflect.ValueOf(val)})
err := results[0].Interface()
if err != nil {
var underlyingErr error
switch e := err.(type) {
case error:
underlyingErr = e
default:
underlyingErr = fmt.Errorf("unknown error type: %T", e)
}
underlyingErr = fmt.Errorf("reflection error: %w", underlyingErr)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert into a value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+underlyingErr.Error(),
)
return target, diags
}
return receiver, diags
}
// FromValueCreator creates an attr.Value from the data in a
// tftypes.ValueCreator, calling its ToTerraform5Value method and converting
// the result to an attr.Value using `typ`.
//
// It is meant to be called from FromValue, not directly.
func FromValueCreator(ctx context.Context, typ attr.Type, val tftypes.ValueCreator, path *tftypes.AttributePath) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
raw, err := val.ToTerraform5Value()
if err != nil {
return nil, append(diags, toTerraform5ValueErrorDiag(err, path))
}
err = tftypes.ValidateValue(typ.TerraformType(ctx), raw)
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfVal := tftypes.NewValue(typ.TerraformType(ctx), raw)
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)
if diags.HasError() {
return nil, diags
}
}
res, err := typ.ValueFromTerraform(ctx, tfVal)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return res, diags
}
// NewAttributeValue creates a new reflect.Value by calling the
// ValueFromTerraform method on `typ`. It will return an error if the returned
// `attr.Value` is not the same type as `target`.
//
// It is meant to be called through Into, not directly.
func NewAttributeValue(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path *tftypes.AttributePath) (reflect.Value, diag.Diagnostics) {
var diags diag.Diagnostics
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, val, path)...)
if diags.HasError() {
return target, diags
}
}
res, err := typ.ValueFromTerraform(ctx, val)
if err != nil {
return target, append(diags, valueFromTerraformErrorDiag(err, path))
}
if reflect.TypeOf(res) != target.Type() {
diags.Append(diag.WithPath(path, DiagNewAttributeValueIntoWrongType{
ValType: reflect.TypeOf(res),
TargetType: target.Type(),
SchemaType: typ,
}))
return target, diags
}
return reflect.ValueOf(res), diags
}
// FromAttributeValue creates an attr.Value from an attr.Value. It just returns
// the attr.Value it is passed, but reserves the right in the future to do some
// validation on that attr.Value to make sure it matches the type produced by
// `typ`.
//
// It is meant to be called through FromValue, not directly.
func FromAttributeValue(ctx context.Context, typ attr.Type, val attr.Value, path *tftypes.AttributePath) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
tfVal, err := val.ToTerraformValue(ctx)
if err != nil {
return val, append(diags, toTerraformValueErrorDiag(err, path))
}
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)
if diags.HasError() {
return val, diags
}
}
return val, diags
}