/
value.go
582 lines (557 loc) · 15.2 KB
/
value.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package tftypes
import (
"bytes"
"fmt"
"math/big"
"sort"
"strconv"
"strings"
msgpack "github.com/vmihailenco/msgpack/v4"
)
// ValueConverter is an interface that provider-defined types can implement to
// control how Value.As will convert a Value into that type. The passed Value
// is the Value that Value.As is being called on. The intended usage is to call
// Value.As on the passed Value, converting it into a builtin type, and then
// converting or casting that builtin type to the provider-defined type.
type ValueConverter interface {
FromTerraform5Value(Value) error
}
// ValueCreator is an interface that provider-defined types can implement to
// control how NewValue will convert that type into a Value. The returned
// interface should return one of the builtin Value representations that should
// be used for that Value.
type ValueCreator interface {
ToTerraform5Value() (interface{}, error)
}
// Value is a piece of data from Terraform or being returned to Terraform. It
// has a Type associated with it, defining its shape and characteristics, and a
// Go representation of that Type containing the data itself. Values are a
// special type and are not represented as pure Go values beause they can
// contain UnknownValues, which cannot be losslessly represented in Go's type
// system.
//
// The recommended usage of a Value is to check that it is known, using
// Value.IsKnown, then to convert it to a Go type, using Value.As. The Go type
// can then be manipulated.
type Value struct {
typ Type
value interface{}
}
func (val Value) String() string {
typ := val.Type()
if typ == nil {
return "invalid typeless tftypes.Value<>"
}
// null and unknown values we use static strings for
if val.IsNull() {
return typ.String() + "<null>"
}
if !val.IsKnown() {
return typ.String() + "<unknown>"
}
// everything else is built up
var res strings.Builder
switch {
case typ.Is(String):
var s string
err := val.As(&s)
if err != nil {
panic(err)
}
res.WriteString(typ.String() + `<"` + s + `">`)
case typ.Is(Number):
n := big.NewFloat(0)
err := val.As(&n)
if err != nil {
panic(err)
}
res.WriteString(typ.String() + `<"` + n.String() + `">`)
case typ.Is(Bool):
var b bool
err := val.As(&b)
if err != nil {
panic(err)
}
res.WriteString(typ.String() + `<"` + strconv.FormatBool(b) + `">`)
case typ.Is(List{}), typ.Is(Set{}), typ.Is(Tuple{}):
var l []Value
err := val.As(&l)
if err != nil {
panic(err)
}
res.WriteString(typ.String() + `<`)
for pos, el := range l {
if pos != 0 {
res.WriteString(", ")
}
res.WriteString(el.String())
}
res.WriteString(">")
case typ.Is(Map{}), typ.Is(Object{}):
m := map[string]Value{}
err := val.As(&m)
if err != nil {
panic(err)
}
res.WriteString(typ.String() + `<`)
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for pos, key := range keys {
if pos != 0 {
res.WriteString(", ")
}
res.WriteString(`"` + key + `":`)
res.WriteString(m[key].String())
}
res.WriteString(">")
}
return res.String()
}
// ApplyTerraform5AttributePathStep applies an AttributePathStep to a Value,
// returning the Value found at that AttributePath within the Value. It
// fulfills that AttributePathStepper interface, allowing Values to be passed
// to WalkAttributePath. This allows retrieving a subset of a Value using an
// AttributePath. If the AttributePathStep can't be applied to the Value,
// either because it is the wrong type or because no Value exists at that
// AttributePathStep, an ErrInvalidStep error will be returned.
func (val Value) ApplyTerraform5AttributePathStep(step AttributePathStep) (interface{}, error) {
if !val.IsKnown() || val.IsNull() {
return nil, ErrInvalidStep
}
switch s := step.(type) {
case AttributeName:
if !val.Type().Is(Object{}) {
return nil, ErrInvalidStep
}
o := map[string]Value{}
err := val.As(&o)
if err != nil {
return nil, err
}
res, ok := o[string(s)]
if !ok {
return nil, ErrInvalidStep
}
return res, nil
case ElementKeyString:
if !val.Type().Is(Map{}) {
return nil, ErrInvalidStep
}
m := map[string]Value{}
err := val.As(&m)
if err != nil {
return nil, err
}
res, ok := m[string(s)]
if !ok {
return nil, ErrInvalidStep
}
return res, nil
case ElementKeyInt:
if !val.Type().Is(List{}) && !val.Type().Is(Tuple{}) {
return nil, ErrInvalidStep
}
if int64(s) < 0 {
return nil, ErrInvalidStep
}
sl := []Value{}
err := val.As(&sl)
if err != nil {
return nil, err
}
if int64(len(sl)) <= int64(s) {
return nil, ErrInvalidStep
}
return sl[int64(s)], nil
case ElementKeyValue:
if !val.Type().Is(Set{}) {
return nil, ErrInvalidStep
}
sl := []Value{}
err := val.As(&sl)
if err != nil {
return nil, err
}
for _, el := range sl {
diffs, err := el.Diff(Value(s))
if err != nil {
return nil, err
}
if len(diffs) == 0 {
return el, nil
}
}
return nil, ErrInvalidStep
default:
return nil, fmt.Errorf("unexpected AttributePathStep type %T", step)
}
}
// Equal returns true if two Values should be considered equal. Values are
// considered equal if their types are considered equal and if they represent
// data that is considered equal.
func (val Value) Equal(o Value) bool {
if val.Type() == nil && o.Type() == nil && val.value == nil && o.value == nil {
return true
}
if val.Type() == nil {
return false
}
if o.Type() == nil {
return false
}
if !val.Type().Equal(o.Type()) {
return false
}
diff, err := val.Diff(o)
if err != nil {
panic(err)
}
return len(diff) < 1
}
// Copy returns a defensively-copied clone of Value that shares no underlying
// data structures with the original Value and can be mutated without
// accidentally mutating the original.
func (val Value) Copy() Value {
newVal := val.value
switch v := val.value.(type) {
case []Value:
newVals := make([]Value, 0, len(v))
for _, value := range v {
newVals = append(newVals, value.Copy())
}
newVal = newVals
case map[string]Value:
newVals := make(map[string]Value, len(v))
for k, value := range v {
newVals[k] = value.Copy()
}
newVal = newVals
}
return NewValue(val.Type(), newVal)
}
// NewValue returns a Value constructed using the specified Type and stores the
// passed value in it.
//
// The passed value should be in one of the builtin Value representations or
// implement the ValueCreator interface.
//
// If the passed value is not a valid value for the passed type, NewValue will
// panic. Any value and type combination that does not return an error from
// ValidateValue is guaranteed to not panic. When calling NewValue with user
// input with a type not known at compile time, it is recommended to call
// ValidateValue before calling NewValue, to allow graceful handling of the
// error.
//
// The builtin Value representations are:
//
// - String: string, *string
// - Number: *big.Float, int64, *int64, int32, *int32, int16, *int16, int8,
// *int8, int, *int, uint64, *uint64, uint32, *uint32, uint16,
// *uint16, uint8, *uint8, uint, *uint, float64, *float64
// - Bool: bool, *bool
// - Map and Object: map[string]Value
// - Tuple, List, and Set: []Value
func NewValue(t Type, val interface{}) Value {
v, err := newValue(t, val)
if err != nil {
panic(err)
}
return v
}
// ValidateValue checks that the Go type passed as `val` can be used as a value
// for the Type passed as `t`. A nil error response indicates that the value is
// valid for the type.
func ValidateValue(t Type, val interface{}) error {
_, err := newValue(t, val)
return err
}
func newValue(t Type, val interface{}) (Value, error) {
if val == nil || val == UnknownValue {
return Value{
typ: t,
value: val,
}, nil
}
if creator, ok := val.(ValueCreator); ok {
var err error
val, err = creator.ToTerraform5Value()
if err != nil {
return Value{}, fmt.Errorf("error creating tftypes.Value: %w", err)
}
}
switch {
case t.Is(String):
v, err := valueFromString(val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(Number):
v, err := valueFromNumber(val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(Bool):
v, err := valueFromBool(val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(Map{}):
v, err := valueFromMap(t.(Map).ElementType, val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(Object{}):
v, err := valueFromObject(t.(Object).AttributeTypes, t.(Object).OptionalAttributes, val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(List{}):
v, err := valueFromList(t.(List).ElementType, val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(Set{}):
v, err := valueFromSet(t.(Set).ElementType, val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(Tuple{}):
v, err := valueFromTuple(t.(Tuple).ElementTypes, val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(DynamicPseudoType):
v, err := valueFromDynamicPseudoType(val)
if err != nil {
return Value{}, err
}
return v, nil
default:
return Value{}, fmt.Errorf("unknown type %s passed to tftypes.NewValue", t)
}
}
// As converts a Value into a Go value. `dst` must be set to a pointer to a
// value of a supported type for the Value's type or an implementation of the
// ValueConverter interface.
//
// For Strings, `dst` must be a pointer to a string or a pointer to a pointer
// to a string. If it's a pointer to a pointer to a string, if the Value is
// null, the pointer to the string will be set to nil. If it's a pointer to a
// string, if the Value is null, the string will be set to the empty value.
//
// For Numbers, `dst` must be a poitner to a big.Float or a pointer to a
// pointer to a big.Float. If it's a pointer to a pointer to a big.Float, if
// the Value is null, the pointer to the big.Float will be set to nil. If it's
// a pointer to a big.Float, if the Value is null, the big.Float will be set to
// 0.
//
// For Bools, `dst` must be a pointer to a bool or a pointer to a pointer to a
// bool. If it's a pointer to a pointer to a bool, if the Value is null, the
// pointer to the bool will be set to nil. If it's a pointer to a bool, if the
// Value is null, the bool will be set to false.
//
// For Maps and Objects, `dst` must be a pointer to a map[string]Value or a
// pointer to a pointer to a map[string]Value. If it's a pointer to a pointer
// to a map[string]Value, if the Value is null, the pointer to the
// map[string]Value will be set to nil. If it's a pointer to a
// map[string]Value, if the Value is null, the map[string]Value will be set to
// an empty map.
//
// For Lists, Sets, and Tuples, `dst` must be a pointer to a []Value or a
// pointer to a pointer to a []Value. If it's a pointer to a pointer to a
// []Value, if the Value is null, the poitner to []Value will be set to nil. If
// it's a pointer to a []Value, if the Value is null, the []Value will be set
// to an empty slice.
//
// Future builtin conversions may be added over time.
//
// If `val` is unknown, an error will be returned, as unknown values can't be
// represented in Go's type system. Providers should check Value.IsKnown before
// calling Value.As.
func (val Value) As(dst interface{}) error {
unmarshaler, ok := dst.(ValueConverter)
if ok {
return unmarshaler.FromTerraform5Value(val)
}
if !val.IsKnown() {
return fmt.Errorf("unmarshaling unknown values is not supported")
}
switch target := dst.(type) {
case *string:
if val.IsNull() {
*target = ""
return nil
}
v, ok := val.value.(string)
if !ok {
return fmt.Errorf("can't unmarshal %s into %T, expected string", val.Type(), dst)
}
*target = v
return nil
case **string:
if val.IsNull() {
*target = nil
return nil
}
if *target == nil {
var s string
*target = &s
}
return val.As(*target)
case *big.Float:
if val.IsNull() {
target.Set(big.NewFloat(0))
return nil
}
v, ok := val.value.(*big.Float)
if !ok {
return fmt.Errorf("can't unmarshal %s into %T, expected *big.Float", val.Type(), dst)
}
target.Copy(v)
return nil
case **big.Float:
if val.IsNull() {
*target = nil
return nil
}
if *target == nil {
*target = big.NewFloat(0)
}
return val.As(*target)
case *bool:
if val.IsNull() {
*target = false
return nil
}
v, ok := val.value.(bool)
if !ok {
return fmt.Errorf("can't unmarshal %s into %T, expected boolean", val.Type(), dst)
}
*target = v
return nil
case **bool:
if val.IsNull() {
*target = nil
return nil
}
if *target == nil {
var b bool
*target = &b
}
return val.As(*target)
case *map[string]Value:
if val.IsNull() {
*target = map[string]Value{}
return nil
}
v, ok := val.value.(map[string]Value)
if !ok {
return fmt.Errorf("can't unmarshal %s into %T, expected map[string]tftypes.Value", val.Type(), dst)
}
*target = v
return nil
case **map[string]Value:
if val.IsNull() {
*target = nil
return nil
}
if *target == nil {
m := map[string]Value{}
*target = &m
}
return val.As(*target)
case *[]Value:
if val.IsNull() {
*target = []Value{}
return nil
}
v, ok := val.value.([]Value)
if !ok {
return fmt.Errorf("can't unmarshal %s into %T expected []tftypes.Value", val.Type(), dst)
}
*target = v
return nil
case **[]Value:
if val.IsNull() {
*target = nil
return nil
}
if *target == nil {
l := []Value{}
*target = &l
}
return val.As(*target)
}
return fmt.Errorf("can't unmarshal into %T, needs FromTerraform5Value method", dst)
}
// Type returns the Type of the Value.
func (val Value) Type() Type {
return val.typ
}
// IsKnown returns true if `val` is known. If `val` is an aggregate type, only
// the top level of the aggregate type is checked; elements and attributes are
// not checked.
func (val Value) IsKnown() bool {
return val.value != UnknownValue
}
// IsFullyKnown returns true if `val` is known. If `val` is an aggregate type,
// IsFullyKnown only returns true if all elements and attributes are known, as
// well.
func (val Value) IsFullyKnown() bool {
if !val.IsKnown() {
return false
}
if val.value == nil {
return true
}
switch val.Type().(type) {
case primitive:
return true
case List, Set, Tuple:
for _, v := range val.value.([]Value) {
if !v.IsFullyKnown() {
return false
}
}
return true
case Map, Object:
for _, v := range val.value.(map[string]Value) {
if !v.IsFullyKnown() {
return false
}
}
return true
}
panic(fmt.Sprintf("unknown type %T", val.Type()))
}
// IsNull returns true if the Value is null.
func (val Value) IsNull() bool {
return val.value == nil
}
// MarshalMsgPack returns a msgpack representation of the Value. This is used
// for constructing tfprotov5.DynamicValues.
//
// Deprecated: this is not meant to be called by third parties. Don't use it.
func (val Value) MarshalMsgPack(t Type) ([]byte, error) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
err := marshalMsgPack(val, t, NewAttributePath(), enc)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func unexpectedValueTypeError(p *AttributePath, expected, got interface{}, typ Type) error {
return p.NewErrorf("unexpected value type %T, %s values must be of type %T", got, typ, expected)
}