forked from zclconf/go-cty
/
unmarshal.go
333 lines (288 loc) · 8.27 KB
/
unmarshal.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
package msgpack
import (
"bytes"
"github.com/vmihailenco/msgpack/v5"
msgpackCodes "github.com/vmihailenco/msgpack/v5/msgpcode"
"github.com/zclconf/go-cty/cty"
)
// Unmarshal interprets the given bytes as a msgpack-encoded cty Value of
// the given type, returning the result.
//
// If an error is returned, the error is written with a hypothetical
// end-user that wrote the msgpack file as its audience, using cty type
// system concepts rather than Go type system concepts.
func Unmarshal(b []byte, ty cty.Type) (cty.Value, error) {
r := bytes.NewReader(b)
dec := msgpack.NewDecoder(r)
var path cty.Path
return unmarshal(dec, ty, path)
}
func unmarshal(dec *msgpack.Decoder, ty cty.Type, path cty.Path) (cty.Value, error) {
peek, err := dec.PeekCode()
if err != nil {
return cty.DynamicVal, path.NewError(err)
}
if msgpackCodes.IsExt(peek) {
// We just assume _all_ extensions are unknown values,
// since we don't have any other extensions.
return unmarshalUnknownValue(dec, ty, path)
}
if ty == cty.DynamicPseudoType {
return unmarshalDynamic(dec, path)
}
if peek == msgpackCodes.Nil {
dec.Skip() // skip what we've peeked
return cty.NullVal(ty), nil
}
switch {
case ty.IsPrimitiveType():
val, err := unmarshalPrimitive(dec, ty, path)
if err != nil {
return cty.NilVal, err
}
return val, nil
case ty.IsListType():
return unmarshalList(dec, ty.ElementType(), path)
case ty.IsSetType():
return unmarshalSet(dec, ty.ElementType(), path)
case ty.IsMapType():
return unmarshalMap(dec, ty.ElementType(), path)
case ty.IsTupleType():
return unmarshalTuple(dec, ty.TupleElementTypes(), path)
case ty.IsObjectType():
return unmarshalObject(dec, ty.AttributeTypes(), path)
default:
return cty.NilVal, path.NewErrorf("unsupported type %s", ty.FriendlyName())
}
}
func unmarshalPrimitive(dec *msgpack.Decoder, ty cty.Type, path cty.Path) (cty.Value, error) {
switch ty {
case cty.Bool:
rv, err := dec.DecodeBool()
if err != nil {
return cty.DynamicVal, path.NewErrorf("bool is required")
}
return cty.BoolVal(rv), nil
case cty.Number:
// Marshal will try int and float first, if the value can be
// losslessly represented in these encodings, and then fall
// back on a string if the number is too large or too precise.
peek, err := dec.PeekCode()
if err != nil {
return cty.DynamicVal, path.NewErrorf("number is required")
}
if msgpackCodes.IsFixedNum(peek) {
rv, err := dec.DecodeInt64()
if err != nil {
return cty.DynamicVal, path.NewErrorf("number is required")
}
return cty.NumberIntVal(rv), nil
}
switch peek {
case msgpackCodes.Int8, msgpackCodes.Int16, msgpackCodes.Int32, msgpackCodes.Int64:
rv, err := dec.DecodeInt64()
if err != nil {
return cty.DynamicVal, path.NewErrorf("number is required")
}
return cty.NumberIntVal(rv), nil
case msgpackCodes.Uint8, msgpackCodes.Uint16, msgpackCodes.Uint32, msgpackCodes.Uint64:
rv, err := dec.DecodeUint64()
if err != nil {
return cty.DynamicVal, path.NewErrorf("number is required")
}
return cty.NumberUIntVal(rv), nil
case msgpackCodes.Float, msgpackCodes.Double:
rv, err := dec.DecodeFloat64()
if err != nil {
return cty.DynamicVal, path.NewErrorf("number is required")
}
return cty.NumberFloatVal(rv), nil
default:
rv, err := dec.DecodeString()
if err != nil {
return cty.DynamicVal, path.NewErrorf("number is required")
}
v, err := cty.ParseNumberVal(rv)
if err != nil {
return cty.DynamicVal, path.NewErrorf("number is required")
}
return v, nil
}
case cty.String:
rv, err := dec.DecodeString()
if err != nil {
return cty.DynamicVal, path.NewErrorf("string is required")
}
return cty.StringVal(rv), nil
default:
// should never happen
panic("unsupported primitive type")
}
}
func unmarshalList(dec *msgpack.Decoder, ety cty.Type, path cty.Path) (cty.Value, error) {
length, err := dec.DecodeArrayLen()
if err != nil {
return cty.DynamicVal, path.NewErrorf("a list is required")
}
switch {
case length < 0:
return cty.NullVal(cty.List(ety)), nil
case length == 0:
return cty.ListValEmpty(ety), nil
}
vals := make([]cty.Value, 0, length)
path = append(path, nil)
for i := 0; i < length; i++ {
path[len(path)-1] = cty.IndexStep{
Key: cty.NumberIntVal(int64(i)),
}
val, err := unmarshal(dec, ety, path)
if err != nil {
return cty.DynamicVal, err
}
vals = append(vals, val)
}
return cty.ListVal(vals), nil
}
func unmarshalSet(dec *msgpack.Decoder, ety cty.Type, path cty.Path) (cty.Value, error) {
length, err := dec.DecodeArrayLen()
if err != nil {
return cty.DynamicVal, path.NewErrorf("a set is required")
}
switch {
case length < 0:
return cty.NullVal(cty.Set(ety)), nil
case length == 0:
return cty.SetValEmpty(ety), nil
}
vals := make([]cty.Value, 0, length)
path = append(path, nil)
for i := 0; i < length; i++ {
path[len(path)-1] = cty.IndexStep{
Key: cty.NumberIntVal(int64(i)),
}
val, err := unmarshal(dec, ety, path)
if err != nil {
return cty.DynamicVal, err
}
vals = append(vals, val)
}
return cty.SetVal(vals), nil
}
func unmarshalMap(dec *msgpack.Decoder, ety cty.Type, path cty.Path) (cty.Value, error) {
length, err := dec.DecodeMapLen()
if err != nil {
return cty.DynamicVal, path.NewErrorf("a map is required")
}
switch {
case length < 0:
return cty.NullVal(cty.Map(ety)), nil
case length == 0:
return cty.MapValEmpty(ety), nil
}
vals := make(map[string]cty.Value, length)
path = append(path, nil)
for i := 0; i < length; i++ {
key, err := dec.DecodeString()
if err != nil {
path[:len(path)-1].NewErrorf("non-string key in map")
}
path[len(path)-1] = cty.IndexStep{
Key: cty.StringVal(key),
}
val, err := unmarshal(dec, ety, path)
if err != nil {
return cty.DynamicVal, err
}
vals[key] = val
}
return cty.MapVal(vals), nil
}
func unmarshalTuple(dec *msgpack.Decoder, etys []cty.Type, path cty.Path) (cty.Value, error) {
length, err := dec.DecodeArrayLen()
if err != nil {
return cty.DynamicVal, path.NewErrorf("a tuple is required")
}
switch {
case length < 0:
return cty.NullVal(cty.Tuple(etys)), nil
case length == 0:
return cty.TupleVal(nil), nil
case length != len(etys):
return cty.DynamicVal, path.NewErrorf("a tuple of length %d is required", len(etys))
}
vals := make([]cty.Value, 0, length)
path = append(path, nil)
for i := 0; i < length; i++ {
path[len(path)-1] = cty.IndexStep{
Key: cty.NumberIntVal(int64(i)),
}
ety := etys[i]
val, err := unmarshal(dec, ety, path)
if err != nil {
return cty.DynamicVal, err
}
vals = append(vals, val)
}
return cty.TupleVal(vals), nil
}
func unmarshalObject(dec *msgpack.Decoder, atys map[string]cty.Type, path cty.Path) (cty.Value, error) {
length, err := dec.DecodeMapLen()
if err != nil {
return cty.DynamicVal, path.NewErrorf("an object is required")
}
switch {
case length < 0:
return cty.NullVal(cty.Object(atys)), nil
case length == 0:
return cty.ObjectVal(nil), nil
case length != len(atys):
return cty.DynamicVal, path.NewErrorf("an object with %d attributes is required (%d given)",
len(atys), length)
}
vals := make(map[string]cty.Value, length)
path = append(path, nil)
for i := 0; i < length; i++ {
key, err := dec.DecodeString()
if err != nil {
return cty.DynamicVal, path[:len(path)-1].NewErrorf("all keys must be strings")
}
path[len(path)-1] = cty.IndexStep{
Key: cty.StringVal(key),
}
aty, exists := atys[key]
if !exists {
return cty.DynamicVal, path.NewErrorf("unsupported attribute")
}
val, err := unmarshal(dec, aty, path)
if err != nil {
return cty.DynamicVal, err
}
vals[key] = val
}
return cty.ObjectVal(vals), nil
}
func unmarshalDynamic(dec *msgpack.Decoder, path cty.Path) (cty.Value, error) {
length, err := dec.DecodeArrayLen()
if err != nil {
return cty.DynamicVal, path.NewError(err)
}
switch {
case length == -1:
return cty.NullVal(cty.DynamicPseudoType), nil
case length != 2:
return cty.DynamicVal, path.NewErrorf(
"dynamic value array must have exactly two elements",
)
}
typeJSON, err := dec.DecodeBytes()
if err != nil {
return cty.DynamicVal, path.NewError(err)
}
var ty cty.Type
err = (&ty).UnmarshalJSON(typeJSON)
if err != nil {
return cty.DynamicVal, path.NewError(err)
}
return unmarshal(dec, ty, path)
}