-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmorph.go
More file actions
743 lines (725 loc) · 25.4 KB
/
morph.go
File metadata and controls
743 lines (725 loc) · 25.4 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package morph
import (
"fmt"
"math/big"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// ValueToType transforms a value along a new type and returns a new value conforming to the given type
func ValueToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
if t == nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Invalid reference type for attribute",
Detail: fmt.Sprintf("Cannot convert value into 'nil' type at attribute: %s", attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
if v.IsNull() {
return newValue(t, nil, p)
}
switch {
case v.Type().Is(tftypes.String):
return morphStringToType(v, t, p)
case v.Type().Is(tftypes.Number):
return morphNumberToType(v, t, p)
case v.Type().Is(tftypes.Bool):
return morphBoolToType(v, t, p)
case v.Type().Is(tftypes.DynamicPseudoType):
return v, diags
case v.Type().Is(tftypes.List{}):
return morphListToType(v, t, p)
case v.Type().Is(tftypes.Tuple{}):
return morphTupleIntoType(v, t, p)
case v.Type().Is(tftypes.Set{}):
return morphSetToType(v, t, p)
case v.Type().Is(tftypes.Map{}):
return morphMapToType(v, t, p)
case v.Type().Is(tftypes.Object{}):
return morphObjectToType(v, t, p)
}
if !v.IsKnown() {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityWarning,
Summary: "Value that isn't fully known",
Detail: fmt.Sprintf("Type conversion cannot be performed on (partially) unknown value at attribute: %s", attributePathSummary(p)),
})
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Value incompatible with expected type",
Detail: fmt.Sprintf("Attribute: %s\n... of type:\n%+v\n...cannot be converted to type:\n%s", attributePathSummary(p), typeNameNoPrefix(v.Type()), typeNameNoPrefix(t)),
})
return tftypes.Value{}, diags
}
func morphBoolToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
if t.Is(tftypes.Bool) {
return v, diags
}
var bnat bool
err := v.As(&bnat)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Invalid Boolean value",
Detail: fmt.Sprintf("Error: %s\nat attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
switch {
case t.Is(tftypes.String):
return newValue(t, strconv.FormatBool(bnat), p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Value incompatible with expected type",
Detail: fmt.Sprintf("Cannot convert Bool values into type %s\n ...at attribute\n%s", typeNameNoPrefix(t), attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
func morphNumberToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
if t.Is(tftypes.Number) {
return v, diags
}
var vnat big.Float
err := v.As(&vnat)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Invalid Number value",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, p),
})
return tftypes.Value{}, diags
}
switch {
case t.Is(tftypes.String):
return newValue(t, vnat.String(), p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Value incompatible with expected type",
Detail: fmt.Sprintf("Cannot convert Number values into type %s\n ...at attribute\n%s", typeNameNoPrefix(t), attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
func morphStringToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
if t.Is(tftypes.String) {
return v, diags
}
var vnat string
err := v.As(&vnat)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to extract value of String attribute",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
switch {
case t.Is(tftypes.Number):
fv, err := strconv.ParseFloat(vnat, 64)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "String value doesn't parse as Number",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
nv := new(big.Float).SetFloat64(fv)
return newValue(t, nv, p)
case t.Is(tftypes.Bool):
bv, err := strconv.ParseBool(vnat)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "String value doesn't parse as Boolean",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
return newValue(t, bv, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Value incompatible with expected type",
Detail: fmt.Sprintf("Cannot transform String value into type %s\n ...at attribute\n%s", typeNameNoPrefix(t), attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
func morphListToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
var lvals []tftypes.Value
err := v.As(&lvals)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to extract value of List attribute",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
switch {
case t.Is(tftypes.List{}):
var nlvals []tftypes.Value = make([]tftypes.Value, len(lvals))
for i, v := range lvals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, t.(tftypes.List).ElementType, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Invalid List value element",
Detail: fmt.Sprintf("Error at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
nlvals[i] = nv
}
return newValue(t, nlvals, p)
case t.Is(tftypes.Tuple{}):
if len(t.(tftypes.Tuple).ElementTypes) != len(lvals) {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform List value into Tuple of different length",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
var tvals []tftypes.Value = make([]tftypes.Value, len(lvals))
for i, v := range lvals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, t.(tftypes.Tuple).ElementTypes[i], elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform List element into Tuple element type",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
tvals[i] = nv
}
return newValue(t, tvals, p)
case t.Is(tftypes.Set{}):
var svals []tftypes.Value = make([]tftypes.Value, len(lvals))
for i, v := range lvals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, t.(tftypes.Set).ElementType, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform List element into Set element type",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
svals[i] = nv
}
return newValue(t, svals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Cannot transform List value into unsupported type",
Detail: fmt.Sprintf("Required type %s, but got %s\n ...at attribute\n%s", typeNameNoPrefix(t), typeNameNoPrefix(v.Type()), attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
func morphTupleIntoType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
var tvals []tftypes.Value
err := v.As(&tvals)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to extract value of Tuple attribute",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
switch {
case t.Is(tftypes.Tuple{}):
var eltypes []tftypes.Type = make([]tftypes.Type, len(tvals))
var lvals []tftypes.Value = make([]tftypes.Value, len(tvals))
if len(tvals) != len(t.(tftypes.Tuple).ElementTypes) {
if len(t.(tftypes.Tuple).ElementTypes) > 1 {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Tuple value into Tuple of different length",
Detail: fmt.Sprintf("Error at attribute:\n%s", attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
// this is the special case workaround for non-uniform lists in OpenAPI (e.g. for CustomResourceDefinitionSpec.versions)
for i := range tvals {
eltypes[i] = t.(tftypes.Tuple).ElementTypes[0]
}
} else {
for i := range tvals {
eltypes[i] = t.(tftypes.Tuple).ElementTypes[i]
}
}
for i, v := range tvals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, eltypes[i], elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Tuple element into Tuple element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
lvals[i] = nv
eltypes[i] = nv.Type()
}
return newValue(tftypes.Tuple{ElementTypes: eltypes}, lvals, p)
case t.Is(tftypes.List{}):
var lvals []tftypes.Value = make([]tftypes.Value, len(tvals))
for i, v := range tvals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, t.(tftypes.List).ElementType, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Tuple element into List element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
lvals[i] = nv
}
return newValue(t, lvals, p)
case t.Is(tftypes.Set{}):
var svals []tftypes.Value = make([]tftypes.Value, len(tvals))
for i, v := range tvals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, t.(tftypes.Set).ElementType, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Tuple element into Set element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
svals[i] = nv
}
return newValue(t, svals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Cannot transform Tuple value into unsupported type",
Detail: fmt.Sprintf("Required type %s, but got %s\n ...at attribute\n%s", typeNameNoPrefix(t), typeNameNoPrefix(v.Type()), attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
func morphSetToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
var svals []tftypes.Value
err := v.As(&svals)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to extract value of Set attribute",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
switch {
case t.Is(tftypes.Set{}):
var svals []tftypes.Value = make([]tftypes.Value, len(svals))
for i, v := range svals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, t.(tftypes.Set).ElementType, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Set element into Set element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
svals[i] = nv
}
return newValue(t, svals, p)
case t.Is(tftypes.List{}):
var lvals []tftypes.Value = make([]tftypes.Value, len(svals))
for i, v := range svals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, t.(tftypes.List).ElementType, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Set element into List element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
lvals[i] = nv
}
return newValue(t, lvals, p)
case t.Is(tftypes.Tuple{}):
if len(t.(tftypes.Tuple).ElementTypes) != len(svals) {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Set value into Tuple of different length",
Detail: fmt.Sprintf("Error at attribute:\n%s", attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
var tvals []tftypes.Value = make([]tftypes.Value, len(svals))
for i, v := range svals {
elp := p.WithElementKeyInt(i)
nv, d := ValueToType(v, t.(tftypes.Tuple).ElementTypes[i], elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Set element into Tuple element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
tvals[i] = nv
}
return newValue(t, tvals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Cannot transform Set value into unsupported type",
Detail: fmt.Sprintf("Required type %s, but got %s\n...at attribute:\n%s", typeNameNoPrefix(t), typeNameNoPrefix(v.Type()), attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
func morphMapToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
var mvals map[string]tftypes.Value
err := v.As(&mvals)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to extract value of Map attribute",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
switch {
case t.Is(tftypes.Object{}):
var ovals map[string]tftypes.Value = make(map[string]tftypes.Value, len(mvals))
for k, v := range mvals {
elp := p.WithElementKeyString(k)
et, ok := t.(tftypes.Object).AttributeTypes[k]
if !ok {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityWarning,
Summary: "Attribute not found in schema",
Detail: fmt.Sprintf("Unable to find schema type for attribute:\n%s", attributePathSummary(elp)),
})
continue
}
nv, d := ValueToType(v, et, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Map element into Object element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
ovals[k] = nv
}
return newValue(t, ovals, p)
case t.Is(tftypes.Map{}):
var nmvals map[string]tftypes.Value = make(map[string]tftypes.Value, len(mvals))
for k, v := range mvals {
elp := p.WithElementKeyString(k)
nv, d := ValueToType(v, t.(tftypes.Map).ElementType, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: elp,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Map element into Map element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
nmvals[k] = nv
}
return newValue(t, nmvals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Cannot transform Map value into unsupported type",
Detail: fmt.Sprintf("Required type %s, but got %s\n...at attribute:\n%s", typeNameNoPrefix(t), typeNameNoPrefix(v.Type()), attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
func morphObjectToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
var diags []*tfprotov5.Diagnostic
var vals map[string]tftypes.Value
err := v.As(&vals)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to extract value of Object attribute",
Detail: fmt.Sprintf("Error: %s\n...at attribute:\n%s", err, p),
})
return tftypes.Value{}, diags
}
switch {
case t.Is(tftypes.Object{}):
var ovals map[string]tftypes.Value = make(map[string]tftypes.Value, len(vals))
for k, v := range vals {
elp := p.WithAttributeName(k)
nt, ok := t.(tftypes.Object).AttributeTypes[k]
if !ok {
if !v.IsNull() {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityWarning,
Summary: "Attribute not found in schema",
Detail: fmt.Sprintf("Unable to find schema type for attribute:\n%s", attributePathSummary(elp)),
})
}
continue
}
nv, d := ValueToType(v, nt, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Object element into Object element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
ovals[k] = nv
}
// for attributes not specified by user add a nil value of their respective type
// tftypes.NewValue() fails if any of the attributes in the object don't have a corresponding value
for k := range t.(tftypes.Object).AttributeTypes {
if _, ok := ovals[k]; !ok {
nv, d := newValue(t.(tftypes.Object).AttributeTypes[k], nil, p)
if d != nil {
diags = append(diags, d...)
return tftypes.Value{}, diags
}
ovals[k] = nv
}
}
otypes := make(map[string]tftypes.Type, len(ovals))
for k, v := range ovals {
otypes[k] = v.Type()
}
return tftypes.NewValue(tftypes.Object{AttributeTypes: otypes}, ovals), diags
case t.Is(tftypes.Map{}):
var mvals map[string]tftypes.Value = make(map[string]tftypes.Value, len(vals))
for k, v := range vals {
elp := p.WithElementKeyString(k)
nv, d := ValueToType(v, t.(tftypes.Map).ElementType, elp)
if len(d) > 0 {
diags = append(diags, d...)
for i := range d {
if d[i].Severity == tfprotov5.DiagnosticSeverityError {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Object element into Map element type",
Detail: fmt.Sprintf("Error (see above) at attribute:\n%s", attributePathSummary(elp)),
})
return tftypes.Value{}, diags
}
}
}
mvals[k] = nv
}
return newValue(t, mvals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Failed to transform Object into unsupported type",
Detail: fmt.Sprintf("Required type %s, but got %s\n...at attribute:\n%s", typeNameNoPrefix(t), typeNameNoPrefix(v.Type()), attributePathSummary(p)),
})
return tftypes.Value{}, diags
}
// ValueToTypePath "normalizes" AttributePaths of values into a form that only describes the type hyerarchy.
// this is used when comparing value paths to type hints generated during the translation from OpenAPI into tftypes.
func ValueToTypePath(a *tftypes.AttributePath) *tftypes.AttributePath {
if a == nil {
return nil
}
ns := make([]tftypes.AttributePathStep, len(a.Steps()))
os := a.Steps()
for i := range os {
switch os[i].(type) {
case tftypes.AttributeName:
ns[i] = tftypes.AttributeName(os[i].(tftypes.AttributeName))
case tftypes.ElementKeyString:
ns[i] = tftypes.ElementKeyString("#")
case tftypes.ElementKeyInt:
ns[i] = tftypes.ElementKeyInt(-1)
}
}
return tftypes.NewAttributePathWithSteps(ns)
}
func typeNameNoPrefix(t tftypes.Type) string {
return strings.ReplaceAll(t.String(), "tftypes.", "")
}
func attributePathSummary(p *tftypes.AttributePath) string {
var b strings.Builder
for pos, step := range p.Steps() {
switch v := step.(type) {
case tftypes.AttributeName:
if pos != 0 {
b.WriteString(".")
}
b.WriteString(string(v))
case tftypes.ElementKeyString:
b.WriteString("[" + string(v) + "]")
case tftypes.ElementKeyInt:
b.WriteString("[" + strconv.FormatInt(int64(v), 10) + "]")
case tftypes.ElementKeyValue:
b.WriteString("[" + tftypes.Value(v).String() + "]")
}
}
return b.String()
}
func validateValue(t tftypes.Type, val interface{}, p *tftypes.AttributePath) []*tfprotov5.Diagnostic {
var diags []*tfprotov5.Diagnostic
if err := tftypes.ValidateValue(t, val); err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Provider encountered an error when trying to determine the Terraform type information for the configured manifest",
Detail: err.(error).Error(),
})
return diags
}
return nil
}
func newValue(t tftypes.Type, val interface{}, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
if diags := validateValue(t, val, p); diags != nil {
return tftypes.Value{}, diags
}
return tftypes.NewValue(t, val), nil
}