/
map.go
567 lines (491 loc) · 16.4 KB
/
map.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
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
package types
import (
"context"
"fmt"
"sort"
"strings"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/reflect"
"github.com/hashicorp/terraform-plugin-framework/path"
)
var (
_ attr.Type = MapType{}
_ attr.Value = &Map{}
)
// MapType is an AttributeType representing a map of values. All values must
// be of the same type, which the provider must specify as the ElemType
// property. Keys will always be strings.
type MapType struct {
ElemType attr.Type
}
// WithElementType returns a new copy of the type with its element type set.
func (m MapType) WithElementType(typ attr.Type) attr.TypeWithElementType {
return MapType{
ElemType: typ,
}
}
// ElementType returns the type's element type.
func (m MapType) ElementType() attr.Type {
return m.ElemType
}
// TerraformType returns the tftypes.Type that should be used to represent this
// type. This constrains what user input will be accepted and what kind of data
// can be set in state. The framework will use this to translate the
// AttributeType to something Terraform can understand.
func (m MapType) TerraformType(ctx context.Context) tftypes.Type {
return tftypes.Map{
ElementType: m.ElemType.TerraformType(ctx),
}
}
// ValueFromTerraform returns an attr.Value given a tftypes.Value. This is
// meant to convert the tftypes.Value into a more convenient Go type for the
// provider to consume the data with.
func (m MapType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
ma := Map{
ElemType: m.ElemType,
state: valueStateDeprecated,
}
if in.Type() == nil {
ma.Null = true
return ma, nil
}
if !in.Type().Is(tftypes.Map{}) {
return nil, fmt.Errorf("can't use %s as value of Map, can only use tftypes.Map values", in.String())
}
if !in.Type().Equal(tftypes.Map{ElementType: m.ElemType.TerraformType(ctx)}) {
return nil, fmt.Errorf("can't use %s as value of Map with ElementType %T, can only use %s values", in.String(), m.ElemType, m.ElemType.TerraformType(ctx).String())
}
if !in.IsKnown() {
ma.Unknown = true
return ma, nil
}
if in.IsNull() {
ma.Null = true
return ma, nil
}
val := map[string]tftypes.Value{}
err := in.As(&val)
if err != nil {
return nil, err
}
elems := make(map[string]attr.Value, len(val))
for key, elem := range val {
av, err := m.ElemType.ValueFromTerraform(ctx, elem)
if err != nil {
return nil, err
}
elems[key] = av
}
ma.Elems = elems
return ma, nil
}
// Equal returns true if `o` is also a MapType and has the same ElemType.
func (m MapType) Equal(o attr.Type) bool {
if m.ElemType == nil {
return false
}
other, ok := o.(MapType)
if !ok {
return false
}
return m.ElemType.Equal(other.ElemType)
}
// ApplyTerraform5AttributePathStep applies the given AttributePathStep to the
// map.
func (m MapType) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
if _, ok := step.(tftypes.ElementKeyString); !ok {
return nil, fmt.Errorf("cannot apply step %T to MapType", step)
}
return m.ElemType, nil
}
// String returns a human-friendly description of the MapType.
func (m MapType) String() string {
return "types.MapType[" + m.ElemType.String() + "]"
}
// Validate validates all elements of the map that are of type
// xattr.TypeWithValidate.
func (m MapType) Validate(ctx context.Context, in tftypes.Value, path path.Path) diag.Diagnostics {
var diags diag.Diagnostics
if in.Type() == nil {
return diags
}
if !in.Type().Is(tftypes.Map{}) {
err := fmt.Errorf("expected Map value, received %T with value: %v", in, in)
diags.AddAttributeError(
path,
"Map Type Validation Error",
"An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return diags
}
if !in.IsKnown() || in.IsNull() {
return diags
}
var elems map[string]tftypes.Value
if err := in.As(&elems); err != nil {
diags.AddAttributeError(
path,
"Map Type Validation Error",
"An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return diags
}
validatableType, isValidatable := m.ElemType.(xattr.TypeWithValidate)
if !isValidatable {
return diags
}
for index, elem := range elems {
if !elem.IsFullyKnown() {
continue
}
diags = append(diags, validatableType.Validate(ctx, elem, path.AtMapKey(index))...)
}
return diags
}
// ValueType returns the Value type.
func (t MapType) ValueType(_ context.Context) attr.Value {
return Map{
ElemType: t.ElemType,
}
}
// MapNull creates a Map with a null value. Determine whether the value is
// null via the Map type IsNull method.
//
// Setting the deprecated Map type ElemType, Elems, Null, or Unknown fields
// after creating a Map with this function has no effect.
func MapNull(elementType attr.Type) Map {
return Map{
elementType: elementType,
state: valueStateNull,
}
}
// MapUnknown creates a Map with an unknown value. Determine whether the
// value is unknown via the Map type IsUnknown method.
//
// Setting the deprecated Map type ElemType, Elems, Null, or Unknown fields
// after creating a Map with this function has no effect.
func MapUnknown(elementType attr.Type) Map {
return Map{
elementType: elementType,
state: valueStateUnknown,
}
}
// MapValue creates a Map with a known value. Access the value via the Map
// type Elements or ElementsAs methods.
//
// Setting the deprecated Map type ElemType, Elems, Null, or Unknown fields
// after creating a Map with this function has no effect.
func MapValue(elementType attr.Type, elements map[string]attr.Value) (Map, diag.Diagnostics) {
var diags diag.Diagnostics
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/521
ctx := context.Background()
for key, element := range elements {
if !elementType.Equal(element.Type(ctx)) {
diags.AddError(
"Invalid Map Element Type",
"While creating a Map value, an invalid element was detected. "+
"A Map must use the single, given element type. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("Map Element Type: %s\n", elementType.String())+
fmt.Sprintf("Map Key (%s) Element Type: %s", key, element.Type(ctx)),
)
}
}
if diags.HasError() {
return MapUnknown(elementType), diags
}
return Map{
elementType: elementType,
elements: elements,
state: valueStateKnown,
}, nil
}
// MapValueFrom creates a Map with a known value, using reflection rules.
// The elements must be a map of string keys to values which can convert into
// the given element type. Access the value via the Map type Elements or
// ElementsAs methods.
func MapValueFrom(ctx context.Context, elementType attr.Type, elements any) (Map, diag.Diagnostics) {
attrValue, diags := reflect.FromValue(
ctx,
MapType{ElemType: elementType},
elements,
path.Empty(),
)
if diags.HasError() {
return MapUnknown(elementType), diags
}
m, ok := attrValue.(Map)
// This should not happen, but ensure there is an error if it does.
if !ok {
diags.AddError(
"Unable to Convert Map Value",
"An unexpected result occurred when creating a Map using MapValueFrom. "+
"This is an issue with terraform-plugin-framework and should be reported to the provider developers.",
)
}
return m, diags
}
// MapValueMust creates a Map with a known value, converting any diagnostics
// into a panic at runtime. Access the value via the Map
// type Elements or ElementsAs methods.
//
// This creation function is only recommended to create Map values which will
// not potentially effect practitioners, such as testing, or exhaustively
// tested provider logic.
//
// Setting the deprecated Map type ElemType, Elems, Null, or Unknown fields
// after creating a Map with this function has no effect.
func MapValueMust(elementType attr.Type, elements map[string]attr.Value) Map {
m, diags := MapValue(elementType, elements)
if diags.HasError() {
// This could potentially be added to the diag package.
diagsStrings := make([]string, 0, len(diags))
for _, diagnostic := range diags {
diagsStrings = append(diagsStrings, fmt.Sprintf(
"%s | %s | %s",
diagnostic.Severity(),
diagnostic.Summary(),
diagnostic.Detail()))
}
panic("MapValueMust received error(s): " + strings.Join(diagsStrings, "\n"))
}
return m
}
// Map represents a mapping of string keys to attr.Value values of a single
// type.
type Map struct {
// Unknown will be set to true if the entire map is an unknown value.
// If only some of the elements in the map are unknown, their known or
// unknown status will be represented however that attr.Value
// surfaces that information. The Map's Unknown property only tracks if
// the number of elements in a Map is known, not whether the elements
// that are in the map are known.
//
// If the Map was created with the MapValue, MapNull, or MapUnknown
// functions, changing this field has no effect.
//
// Deprecated: Use the MapUnknown function to create an unknown Map
// value or use the IsUnknown method to determine whether the Map value
// is unknown instead.
Unknown bool
// Null will be set to true if the map is null, either because it was
// omitted from the configuration, state, or plan, or because it was
// explicitly set to null.
//
// If the Map was created with the MapValue, MapNull, or MapUnknown
// functions, changing this field has no effect.
//
// Deprecated: Use the MapNull function to create a null Map value or
// use the IsNull method to determine whether the Map value is null
// instead.
Null bool
// Elems are the elements in the map.
//
// If the Map was created with the MapValue, MapNull, or MapUnknown
// functions, changing this field has no effect.
//
// Deprecated: Use the MapValue function to create a known Map value or
// use the Elements or ElementsAs methods to retrieve the Map elements
// instead.
Elems map[string]attr.Value
// ElemType is the AttributeType of the elements in the map. All
// elements in the map must be of this type.
//
// Deprecated: Use the MapValue, MapNull, or MapUnknown functions
// to create a Map or use the ElementType method to retrieve the
// Map element type instead.
ElemType attr.Type
// elements is the mapping of known values in the Map.
elements map[string]attr.Value
// elementType is the type of the elements in the Map.
elementType attr.Type
// state represents whether the Map is null, unknown, or known. During the
// exported field deprecation period, this state can also be "deprecated",
// which remains the zero-value for compatibility to ensure exported field
// updates take effect. The zero-value will be changed to null in a future
// version.
state valueState
}
// Elements returns the mapping of elements for the Map. Returns nil if the
// Map is null or unknown.
func (m Map) Elements() map[string]attr.Value {
if m.state == valueStateDeprecated {
return m.Elems
}
return m.elements
}
// ElementsAs populates `target` with the elements of the Map, throwing an
// error if the elements cannot be stored in `target`.
func (m Map) ElementsAs(ctx context.Context, target interface{}, allowUnhandled bool) diag.Diagnostics {
// we need a tftypes.Value for this Map to be able to use it with our
// reflection code
val, err := m.ToTerraformValue(ctx)
if err != nil {
err := fmt.Errorf("error getting Terraform value for map: %w", err)
return diag.Diagnostics{
diag.NewErrorDiagnostic(
"Map Conversion Error",
"An unexpected error was encountered trying to convert the map into an equivalent Terraform value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
),
}
}
return reflect.Into(ctx, MapType{ElemType: m.ElemType}, val, target, reflect.Options{
UnhandledNullAsEmpty: allowUnhandled,
UnhandledUnknownAsEmpty: allowUnhandled,
}, path.Empty())
}
// ElementType returns the element type for the Map.
func (m Map) ElementType(_ context.Context) attr.Type {
if m.state == valueStateDeprecated {
return m.ElemType
}
return m.elementType
}
// Type returns a MapType with the same element type as `m`.
func (m Map) Type(ctx context.Context) attr.Type {
return MapType{ElemType: m.ElementType(ctx)}
}
// ToTerraformValue returns the data contained in the List as a tftypes.Value.
func (m Map) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
if m.state == valueStateDeprecated && m.ElemType == nil {
return tftypes.Value{}, fmt.Errorf("cannot convert Map to tftypes.Value if ElemType field is not set")
}
mapType := tftypes.Map{ElementType: m.ElementType(ctx).TerraformType(ctx)}
switch m.state {
case valueStateDeprecated:
if m.Unknown {
return tftypes.NewValue(mapType, tftypes.UnknownValue), nil
}
if m.Null {
return tftypes.NewValue(mapType, nil), nil
}
vals := make(map[string]tftypes.Value, len(m.Elems))
for key, elem := range m.Elems {
val, err := elem.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(mapType, tftypes.UnknownValue), err
}
vals[key] = val
}
if err := tftypes.ValidateValue(mapType, vals); err != nil {
return tftypes.NewValue(mapType, tftypes.UnknownValue), err
}
return tftypes.NewValue(mapType, vals), nil
case valueStateKnown:
vals := make(map[string]tftypes.Value, len(m.elements))
for key, elem := range m.elements {
val, err := elem.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(mapType, tftypes.UnknownValue), err
}
vals[key] = val
}
if err := tftypes.ValidateValue(mapType, vals); err != nil {
return tftypes.NewValue(mapType, tftypes.UnknownValue), err
}
return tftypes.NewValue(mapType, vals), nil
case valueStateNull:
return tftypes.NewValue(mapType, nil), nil
case valueStateUnknown:
return tftypes.NewValue(mapType, tftypes.UnknownValue), nil
default:
panic(fmt.Sprintf("unhandled Map state in ToTerraformValue: %s", m.state))
}
}
// Equal returns true if the Map is considered semantically equal
// (same type and same value) to the attr.Value passed as an argument.
func (m Map) Equal(o attr.Value) bool {
other, ok := o.(Map)
if !ok {
return false
}
if m.state != other.state {
return false
}
if m.state == valueStateKnown {
if !m.elementType.Equal(other.elementType) {
return false
}
if len(m.elements) != len(other.elements) {
return false
}
for key, mElem := range m.elements {
otherElem := other.elements[key]
if !mElem.Equal(otherElem) {
return false
}
}
return true
}
if m.Unknown != other.Unknown {
return false
}
if m.Null != other.Null {
return false
}
if m.ElemType == nil && other.ElemType != nil {
return false
}
if m.ElemType != nil && !m.ElemType.Equal(other.ElemType) {
return false
}
if len(m.Elems) != len(other.Elems) {
return false
}
for key, mElem := range m.Elems {
oElem, ok := other.Elems[key]
if !ok {
return false
}
if !mElem.Equal(oElem) {
return false
}
}
return true
}
// IsNull returns true if the Map represents a null value.
func (m Map) IsNull() bool {
if m.state == valueStateNull {
return true
}
return m.state == valueStateDeprecated && m.Null
}
// IsUnknown returns true if the Map represents a currently unknown value.
// Returns false if the Map has a known number of elements, even if all are
// unknown values.
func (m Map) IsUnknown() bool {
if m.state == valueStateUnknown {
return true
}
return m.state == valueStateDeprecated && m.Unknown
}
// String returns a human-readable representation of the Map value.
// The string returned here is not protected by any compatibility guarantees,
// and is intended for logging and error reporting.
func (m Map) String() string {
if m.IsUnknown() {
return attr.UnknownValueString
}
if m.IsNull() {
return attr.NullValueString
}
// We want the output to be consistent, so we sort the output by key
keys := make([]string, 0, len(m.Elements()))
for k := range m.Elements() {
keys = append(keys, k)
}
sort.Strings(keys)
var res strings.Builder
res.WriteString("{")
for i, k := range keys {
if i != 0 {
res.WriteString(",")
}
res.WriteString(fmt.Sprintf("%q:%s", k, m.Elements()[k].String()))
}
res.WriteString("}")
return res.String()
}