/
value_msgpack.go
580 lines (537 loc) · 15.7 KB
/
value_msgpack.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
package tftypes
import (
"bytes"
"fmt"
"math"
"math/big"
"sort"
"github.com/vmihailenco/msgpack"
msgpackCodes "github.com/vmihailenco/msgpack/codes"
)
type msgPackUnknownType struct{}
var msgPackUnknownVal = msgPackUnknownType{}
func (u msgPackUnknownType) MarshalMsgpack() ([]byte, error) {
return []byte{0xd4, 0, 0}, nil
}
// ValueFromMsgPack returns a Value from the MsgPack-encoded bytes, using the
// provided Type to determine what shape the Value should be.
// DynamicPseudoTypes will be transparently parsed into the types they
// represent.
//
// Deprecated: this function is exported for internal use in
// terraform-plugin-go. Third parties should not use it, and its behavior is
// not covered under the API compatibility guarantees. Don't use this.
func ValueFromMsgPack(data []byte, typ Type) (Value, error) {
r := bytes.NewReader(data)
dec := msgpack.NewDecoder(r)
return msgpackUnmarshal(dec, typ, NewAttributePath())
}
func msgpackUnmarshal(dec *msgpack.Decoder, typ Type, path *AttributePath) (Value, error) {
peek, err := dec.PeekCode()
if err != nil {
return Value{}, path.NewErrorf("error peeking next byte: %w", err)
}
if msgpackCodes.IsExt(peek) {
// as with go-cty, assume all extensions are unknown values
err := dec.Skip()
if err != nil {
return Value{}, path.NewErrorf("error skipping extension byte: %w", err)
}
return NewValue(typ, UnknownValue), nil
}
if typ.Is(DynamicPseudoType) {
return msgpackUnmarshalDynamic(dec, path)
}
if peek == msgpackCodes.Nil {
err := dec.Skip()
if err != nil {
return Value{}, path.NewErrorf("error skipping nil byte: %w", err)
}
return NewValue(typ, nil), nil
}
switch {
case typ.Is(String):
rv, err := dec.DecodeString()
if err != nil {
return Value{}, path.NewErrorf("error decoding string: %w", err)
}
return NewValue(String, rv), nil
case typ.Is(Number):
peek, err := dec.PeekCode()
if err != nil {
return Value{}, path.NewErrorf("couldn't peek number: %w", err)
}
if msgpackCodes.IsFixedNum(peek) {
rv, err := dec.DecodeInt64()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as int64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
}
switch peek {
case msgpackCodes.Int8, msgpackCodes.Int16, msgpackCodes.Int32, msgpackCodes.Int64:
rv, err := dec.DecodeInt64()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as int64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
case msgpackCodes.Uint8, msgpackCodes.Uint16, msgpackCodes.Uint32, msgpackCodes.Uint64:
rv, err := dec.DecodeUint64()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as uint64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
case msgpackCodes.Float, msgpackCodes.Double:
rv, err := dec.DecodeFloat64()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as float64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
default:
rv, err := dec.DecodeString()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as string: %w", err)
}
// according to
// https://github.com/hashicorp/go-cty/blob/85980079f637862fa8e43ddc82dd74315e2f4c85/cty/value_init.go#L49
// Base 10, precision 512, and rounding to nearest even
// is the standard way to handle numbers arriving as
// strings.
fv, _, err := big.ParseFloat(rv, 10, 512, big.ToNearestEven)
if err != nil {
return Value{}, path.NewErrorf("error parsing %q as number: %w", rv, err)
}
return NewValue(Number, fv), nil
}
case typ.Is(Bool):
rv, err := dec.DecodeBool()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode bool: %w", err)
}
return NewValue(Bool, rv), nil
case typ.Is(List{}):
return msgpackUnmarshalList(dec, typ.(List).ElementType, path)
case typ.Is(Set{}):
return msgpackUnmarshalSet(dec, typ.(Set).ElementType, path)
case typ.Is(Map{}):
return msgpackUnmarshalMap(dec, typ.(Map).AttributeType, path)
case typ.Is(Tuple{}):
return msgpackUnmarshalTuple(dec, typ.(Tuple).ElementTypes, path)
case typ.Is(Object{}):
return msgpackUnmarshalObject(dec, typ.(Object).AttributeTypes, path)
}
return Value{}, path.NewErrorf("unsupported type %s", typ.String())
}
func msgpackUnmarshalList(dec *msgpack.Decoder, typ Type, path *AttributePath) (Value, error) {
length, err := dec.DecodeArrayLen()
if err != nil {
return Value{}, path.NewErrorf("error decoding list length: %w", err)
}
switch {
case length < 0:
return NewValue(List{
ElementType: typ,
}, nil), nil
case length == 0:
return NewValue(List{
ElementType: typ,
}, []Value{}), nil
}
vals := make([]Value, 0, length)
for i := 0; i < length; i++ {
innerPath := path.WithElementKeyInt(int64(i))
val, err := msgpackUnmarshal(dec, typ, innerPath)
if err != nil {
return Value{}, err
}
vals = append(vals, val)
}
elTyp := typ
if elTyp.Is(DynamicPseudoType) {
elTyp, err = TypeFromElements(vals)
if err != nil {
return Value{}, err
}
}
return NewValue(List{
ElementType: elTyp,
}, vals), nil
}
func msgpackUnmarshalSet(dec *msgpack.Decoder, typ Type, path *AttributePath) (Value, error) {
length, err := dec.DecodeArrayLen()
if err != nil {
return Value{}, path.NewErrorf("error decoding set length: %w", err)
}
switch {
case length < 0:
return NewValue(Set{
ElementType: typ,
}, nil), nil
case length == 0:
return NewValue(Set{
ElementType: typ,
}, []Value{}), nil
}
vals := make([]Value, 0, length)
for i := 0; i < length; i++ {
innerPath := path.WithElementKeyInt(int64(i))
val, err := msgpackUnmarshal(dec, typ, innerPath)
if err != nil {
return Value{}, err
}
vals = append(vals, val)
}
elTyp, err := TypeFromElements(vals)
if err != nil {
return Value{}, err
}
return NewValue(Set{
ElementType: elTyp,
}, vals), nil
}
func msgpackUnmarshalMap(dec *msgpack.Decoder, typ Type, path *AttributePath) (Value, error) {
length, err := dec.DecodeMapLen()
if err != nil {
return Value{}, path.NewErrorf("error decoding map length: %w", err)
}
switch {
case length < 0:
return NewValue(Map{
AttributeType: typ,
}, nil), nil
case length == 0:
return NewValue(Map{
AttributeType: typ,
}, map[string]Value{}), nil
}
vals := make(map[string]Value, length)
for i := 0; i < length; i++ {
key, err := dec.DecodeString()
if err != nil {
return Value{}, path.NewErrorf("error decoding map key: %w", err)
}
innerPath := path.WithElementKeyString(key)
val, err := msgpackUnmarshal(dec, typ, innerPath)
if err != nil {
return Value{}, err
}
vals[key] = val
}
return NewValue(Map{
AttributeType: typ,
}, vals), nil
}
func msgpackUnmarshalTuple(dec *msgpack.Decoder, types []Type, path *AttributePath) (Value, error) {
length, err := dec.DecodeArrayLen()
if err != nil {
return Value{}, path.NewErrorf("error decoding tuple length: %w", err)
}
switch {
case length < 0:
return NewValue(Tuple{
ElementTypes: types,
}, nil), nil
case length == 0:
return NewValue(Tuple{
// no elements means no types
ElementTypes: nil,
}, []Value{}), nil
case length != len(types):
return Value{}, path.NewErrorf("error decoding tuple; expected %d items, got %d", len(types), length)
}
vals := make([]Value, 0, length)
for i := 0; i < length; i++ {
innerPath := path.WithElementKeyInt(int64(i))
typ := types[i]
val, err := msgpackUnmarshal(dec, typ, innerPath)
if err != nil {
return Value{}, err
}
vals = append(vals, val)
}
return NewValue(Tuple{
ElementTypes: types,
}, vals), nil
}
func msgpackUnmarshalObject(dec *msgpack.Decoder, types map[string]Type, path *AttributePath) (Value, error) {
length, err := dec.DecodeMapLen()
if err != nil {
return Value{}, path.NewErrorf("error decoding object length: %w", err)
}
switch {
case length < 0:
return NewValue(Object{
AttributeTypes: types,
}, nil), nil
case length == 0:
return NewValue(Object{
// no attributes means no types
AttributeTypes: map[string]Type{},
}, map[string]Value{}), nil
case length != len(types):
return Value{}, path.NewErrorf("error decoding object; expected %d attributes, got %d", len(types), length)
}
vals := make(map[string]Value, length)
for i := 0; i < length; i++ {
key, err := dec.DecodeString()
if err != nil {
return Value{}, path.NewErrorf("error decoding object key: %w", err)
}
typ, exists := types[key]
if !exists {
return Value{}, path.NewErrorf("unknown attribute %q", key)
}
innerPath := path.WithAttributeName(key)
val, err := msgpackUnmarshal(dec, typ, innerPath)
if err != nil {
return Value{}, err
}
vals[key] = val
}
return NewValue(Object{
AttributeTypes: types,
}, vals), nil
}
func msgpackUnmarshalDynamic(dec *msgpack.Decoder, path *AttributePath) (Value, error) {
length, err := dec.DecodeArrayLen()
if err != nil {
return Value{}, path.NewErrorf("error checking length of DynamicPseudoType value: %w", err)
}
switch {
case length == -1:
return NewValue(DynamicPseudoType, nil), nil
case length != 2:
return Value{}, path.NewErrorf("expected %d elements in DynamicPseudoType array, got %d", 2, length)
}
typeJSON, err := dec.DecodeBytes()
if err != nil {
return Value{}, path.NewErrorf("error decoding bytes: %w", err)
}
typ, err := ParseJSONType(typeJSON) //nolint:staticcheck
if err != nil {
return Value{}, path.NewErrorf("error parsing type information: %w", err)
}
return msgpackUnmarshal(dec, typ, path)
}
func marshalMsgPack(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
if typ.Is(DynamicPseudoType) && !val.Type().Is(DynamicPseudoType) {
return marshalMsgPackDynamicPseudoType(val, typ, p, enc)
}
if !val.IsKnown() {
err := enc.Encode(msgPackUnknownVal)
if err != nil {
return p.NewErrorf("error encoding UnknownValue: %w", err)
}
return nil
}
if val.IsNull() {
err := enc.EncodeNil()
if err != nil {
return p.NewErrorf("error encoding null value: %w", err)
}
return nil
}
switch {
case typ.Is(String):
return marshalMsgPackString(val, typ, p, enc)
case typ.Is(Number):
return marshalMsgPackNumber(val, typ, p, enc)
case typ.Is(Bool):
return marshalMsgPackBool(val, typ, p, enc)
case typ.Is(List{}):
return marshalMsgPackList(val, typ, p, enc)
case typ.Is(Set{}):
return marshalMsgPackSet(val, typ, p, enc)
case typ.Is(Map{}):
return marshalMsgPackMap(val, typ, p, enc)
case typ.Is(Tuple{}):
return marshalMsgPackTuple(val, typ, p, enc)
case typ.Is(Object{}):
return marshalMsgPackObject(val, typ, p, enc)
}
return fmt.Errorf("unknown type %s", typ)
}
func marshalMsgPackDynamicPseudoType(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
typeJSON, err := val.Type().MarshalJSON()
if err != nil {
return p.NewErrorf("error generating JSON for type %s: %w", val.Type(), err)
}
err = enc.EncodeArrayLen(2)
if err != nil {
return p.NewErrorf("error encoding array length: %w", err)
}
err = enc.EncodeBytes(typeJSON)
if err != nil {
return p.NewErrorf("error encoding JSON type info: %w", err)
}
err = marshalMsgPack(val, val.Type(), p, enc)
if err != nil {
return p.NewErrorf("error marshaling DynamicPseudoType value: %w", err)
}
return nil
}
func marshalMsgPackString(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
s, ok := val.value.(string)
if !ok {
return unexpectedValueTypeError(p, s, val.value, typ)
}
err := enc.EncodeString(s)
if err != nil {
return p.NewErrorf("error encoding string value: %w", err)
}
return nil
}
func marshalMsgPackNumber(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
n, ok := val.value.(*big.Float)
if !ok {
return unexpectedValueTypeError(p, n, val.value, typ)
}
if n.IsInf() {
if n.Sign() == -1 {
err := enc.EncodeFloat64(math.Inf(-1))
if err != nil {
return p.NewErrorf("error encoding negative infinity: %w", err)
}
} else if n.Sign() == 1 {
err := enc.EncodeFloat64(math.Inf(1))
if err != nil {
return p.NewErrorf("error encoding positive infinity: %w", err)
}
} else {
return p.NewErrorf("error encoding unknown infiniy: sign %d is unknown", n.Sign())
}
} else if iv, acc := n.Int64(); acc == big.Exact {
err := enc.EncodeInt(iv)
if err != nil {
return p.NewErrorf("error encoding int value: %w", err)
}
} else if fv, acc := n.Float64(); acc == big.Exact {
err := enc.EncodeFloat64(fv)
if err != nil {
return p.NewErrorf("error encoding float value: %w", err)
}
} else {
err := enc.EncodeString(n.Text('f', -1))
if err != nil {
return p.NewErrorf("error encoding number string value: %w", err)
}
}
return nil
}
func marshalMsgPackBool(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
b, ok := val.value.(bool)
if !ok {
return unexpectedValueTypeError(p, b, val.value, typ)
}
err := enc.EncodeBool(b)
if err != nil {
return p.NewErrorf("error encoding bool value: %w", err)
}
return nil
}
func marshalMsgPackList(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
l, ok := val.value.([]Value)
if !ok {
return unexpectedValueTypeError(p, l, val.value, typ)
}
err := enc.EncodeArrayLen(len(l))
if err != nil {
return p.NewErrorf("error encoding list length: %w", err)
}
for pos, i := range l {
err := marshalMsgPack(i, typ.(List).ElementType, p.WithElementKeyInt(int64(pos)), enc)
if err != nil {
return err
}
}
return nil
}
func marshalMsgPackSet(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
s, ok := val.value.([]Value)
if !ok {
return unexpectedValueTypeError(p, s, val.value, typ)
}
err := enc.EncodeArrayLen(len(s))
if err != nil {
return p.NewErrorf("error encoding set length: %w", err)
}
for _, i := range s {
err := marshalMsgPack(i, typ.(Set).ElementType, p.WithElementKeyValue(i), enc)
if err != nil {
return err
}
}
return nil
}
func marshalMsgPackMap(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
m, ok := val.value.(map[string]Value)
if !ok {
return unexpectedValueTypeError(p, m, val.value, typ)
}
err := enc.EncodeMapLen(len(m))
if err != nil {
return p.NewErrorf("error encoding map length: %w", err)
}
for k, v := range m {
err := marshalMsgPack(NewValue(String, k), String, p.WithElementKeyString(k), enc)
if err != nil {
return p.NewErrorf("error encoding map key: %w", err)
}
err = marshalMsgPack(v, typ.(Map).AttributeType, p, enc)
if err != nil {
return err
}
}
return nil
}
func marshalMsgPackTuple(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
t, ok := val.value.([]Value)
if !ok {
return unexpectedValueTypeError(p, t, val.value, typ)
}
types := typ.(Tuple).ElementTypes
err := enc.EncodeArrayLen(len(types))
if err != nil {
return p.NewErrorf("error encoding tuple length: %w", err)
}
for pos, v := range t {
ty := types[pos]
err := marshalMsgPack(v, ty, p.WithElementKeyInt(int64(pos)), enc)
if err != nil {
return err
}
}
return nil
}
func marshalMsgPackObject(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
o, ok := val.value.(map[string]Value)
if !ok {
return unexpectedValueTypeError(p, o, val.value, typ)
}
types := typ.(Object).AttributeTypes
keys := make([]string, 0, len(types))
for k := range types {
keys = append(keys, k)
}
sort.Strings(keys)
err := enc.EncodeMapLen(len(keys))
if err != nil {
return p.NewErrorf("error encoding object length: %w", err)
}
for _, k := range keys {
ty := types[k]
v, ok := o[k]
if !ok {
return p.WithAttributeName(k).NewErrorf("no value set")
}
err := marshalMsgPack(NewValue(String, k), String, p.WithAttributeName(k), enc)
if err != nil {
return err
}
err = marshalMsgPack(v, ty, p.WithAttributeName(k), enc)
if err != nil {
return err
}
}
return nil
}