forked from alecthomas/hcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
567 lines (520 loc) · 15 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
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 hcl
import (
"encoding"
"encoding/json"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/alecthomas/participle"
"github.com/alecthomas/participle/lexer"
)
var (
textUnmarshalerInterface = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
textMarshalerInterface = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
jsonUnmarshalerInterface = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
jsonMarshalerInterface = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
remainType = reflect.TypeOf([]*Entry{})
durationType = reflect.TypeOf(time.Duration(0))
timeType = reflect.TypeOf(time.Time{})
)
// Unmarshal HCL into a Go struct.
func Unmarshal(data []byte, v interface{}, options ...MarshalOption) error {
ast, err := ParseBytes(data)
if err != nil {
return err
}
return UnmarshalAST(ast, v, options...)
}
// UnmarshalAST unmarshalls an already parsed or constructed AST into a Go struct.
func UnmarshalAST(ast *AST, v interface{}, options ...MarshalOption) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
return fmt.Errorf("%T must be a pointer", v)
}
opt := &marshalState{}
for _, option := range options {
option(opt)
}
return unmarshalEntries(rv.Elem(), ast.Entries, opt)
}
// UnmarshalBlock into a struct.
func UnmarshalBlock(block *Block, v interface{}, options ...MarshalOption) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Struct {
return fmt.Errorf("%T must be a pointer to a struct", v)
}
rv = rv.Elem()
opt := &marshalState{}
for _, option := range options {
option(opt)
}
return unmarshalBlock(rv, block, opt)
}
func unmarshalEntries(v reflect.Value, entries []*Entry, opt *marshalState) error {
if v.Kind() != reflect.Struct {
return fmt.Errorf("%T must be a struct", v.Interface())
}
// Collect entries from the source into a map.
seen := map[string]*Entry{}
mentries := make(map[string][]*Entry, len(entries))
for _, entry := range entries {
key := entry.Key()
existing, ok := mentries[key]
if ok {
// Mismatch in type.
if !((existing[0].Block == nil) == (entry.Block == nil)) {
return participle.Errorf(existing[0].Pos, "%s: %s cannot be both block and attribute", entry.Pos, key)
}
}
mentries[key] = append(mentries[key], entry)
seen[key] = entry
}
// Collect the fields of the target struct.
fields, err := flattenFields(v, opt)
if err != nil {
return err
}
// Apply HCL entries to our fields.
for _, field := range fields {
tag := field.tag // nolint: govet
switch {
case tag.name == "":
continue
case tag.label:
delete(seen, tag.name)
continue
case tag.remain:
if field.t.Type != remainType {
panic(fmt.Sprintf(`"remain" field %q must be of type []*hcl.Entry but is %T`, field.t.Name, field.t.Type))
}
remaining := []*Entry{}
for _, entries := range mentries {
remaining = append(remaining, entries...)
}
sort.Slice(remaining, func(i, j int) bool {
return remaining[i].Key() < remaining[j].Key()
})
field.v.Set(reflect.ValueOf(remaining))
return nil
}
haventSeen := seen[tag.name] == nil
entries := mentries[tag.name]
if len(entries) == 0 {
if !tag.optional && haventSeen {
return fmt.Errorf("missing required attribute %q", tag.name)
}
// apply defaults here as there's no value for this field
v, err := defaultValueFromTag(field, tag.defaultValue)
if err != nil {
return err
}
if v != nil {
// check enum before assigning default value
err := checkEnum(v, field, tag.enum)
if err != nil {
return fmt.Errorf("default value conflicts with enum: %v", err)
}
err = unmarshalValue(field.v, v, opt)
if err != nil {
return fmt.Errorf("error applying default value to field %q, %v", field.t.Name, err)
}
}
continue
}
delete(seen, tag.name)
entry := entries[0]
entries = entries[1:]
mentries[tag.name] = entries
// Field is a pointer, create value if necessary, then move field down.
if field.v.Kind() == reflect.Ptr {
if field.v.IsNil() {
field.v.Set(reflect.New(field.v.Type().Elem()))
}
field.v = field.v.Elem()
field.t.Type = field.t.Type.Elem()
}
// Check for unmarshaler interfaces and other special cases.
if entry.Attribute != nil {
val := entry.Attribute.Value
if uv, ok := implements(field.v, jsonUnmarshalerInterface); ok {
err := uv.Interface().(json.Unmarshaler).UnmarshalJSON([]byte(val.String()))
if err != nil {
return participle.Wrapf(val.Pos, err, "invalid value")
}
continue
} else if uv, ok := implements(field.v, textUnmarshalerInterface); ok {
err := uv.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(*val.Str))
if err != nil {
return participle.Wrapf(val.Pos, err, "invalid value")
}
continue
} else if val != nil && val.Str != nil {
switch field.v.Interface().(type) {
case time.Duration:
d, err := time.ParseDuration(*val.Str)
if err != nil {
return participle.Wrapf(val.Pos, err, "invalid duration")
}
field.v.Set(reflect.ValueOf(d))
continue
case time.Time:
t, err := time.Parse(time.RFC3339, *val.Str)
if err != nil {
return participle.Wrapf(val.Pos, err, "invalid time")
}
field.v.Set(reflect.ValueOf(t))
continue
}
}
}
switch field.v.Kind() {
case reflect.Struct:
if len(entries) > 0 {
return participle.Errorf(entry.Pos, "duplicate field %q at %s", entry.Key(), entry.Pos)
}
if entry.Attribute != nil {
return participle.Errorf(entry.Pos, "expected a block for %q but got an attribute", tag.name)
}
err := unmarshalBlock(field.v, entry.Block, opt)
if err != nil {
return participle.AnnotateError(entry.Pos, err)
}
case reflect.Slice:
// Slice of blocks.
ptr := false
elt := field.v.Type().Elem()
if elt.Kind() == reflect.Ptr {
elt = elt.Elem()
ptr = true
}
if elt.Kind() == reflect.Struct {
mentries[field.t.Name] = nil
entries = append([]*Entry{entry}, entries...)
for _, entry := range entries {
if entry.Attribute != nil {
return participle.Errorf(entry.Pos, "expected a block for %q but got an attribute", tag.name)
}
el := reflect.New(elt).Elem()
err := unmarshalBlock(el, entry.Block, opt)
if err != nil {
return participle.AnnotateError(entry.Pos, err)
}
if ptr {
el = el.Addr()
}
field.v.Set(reflect.Append(field.v, el))
}
continue
}
fallthrough
default:
// Anything else must be a scalar value.
if len(entries) > 0 {
return participle.Errorf(entry.Pos, "duplicate field %q at %s", entry.Key(), entries[0].Pos)
}
if entry.Block != nil {
return participle.Errorf(entry.Pos, "expected an attribute for %q but got a block", tag.name)
}
value := entry.Attribute.Value
// check enum before unmarshalling actual value
err := checkEnum(value, field, tag.enum)
if err != nil {
return err
}
err = unmarshalValue(field.v, value, opt)
if err != nil {
pos := entry.Attribute.Pos
if value != nil {
pos = value.Pos
}
return participle.AnnotateError(pos, err)
}
}
}
if len(seen) > 0 {
need := []string{}
var pos *lexer.Position
for key, entry := range seen {
if pos == nil {
pos = &entry.Pos
}
need = append(need, strconv.Quote(key))
}
return participle.Errorf(*pos, "found extra fields %s", strings.Join(need, ", "))
}
return nil
}
func checkEnum(v *Value, f field, enum string) error { // nolint: interfacer
if enum == "" || v == nil {
return nil
}
k := f.v.Kind()
if k == reflect.Ptr {
k = f.v.Elem().Kind()
}
switch k {
case reflect.Map, reflect.Struct, reflect.Array, reflect.Slice:
return fmt.Errorf("enum on map, struct, array and slice are not supported on field %q", f.t.Name)
default:
enums, err := enumValuesFromTag(f, enum)
if err != nil {
return err
}
enumStr := []string{}
for _, e := range enums {
if e.String() == v.String() {
return nil
}
enumStr = append(enumStr, e.String())
}
return fmt.Errorf("value %s does not match anything within enum %s", v.String(), strings.Join(enumStr, ", "))
}
}
func unmarshalBlock(v reflect.Value, block *Block, opt *marshalState) error {
if pos := v.FieldByName("Pos"); pos.IsValid() {
pos.Set(reflect.ValueOf(block.Pos))
}
fields, err := flattenFields(v, opt)
if err != nil {
return participle.AnnotateError(block.Pos, err)
}
labels := block.Labels
for _, field := range fields {
tag := field.tag // nolint: govet
if tag.name == "" || !tag.label {
continue
}
if len(labels) == 0 {
return participle.Errorf(block.Pos, "missing label %q", tag.name)
}
if uv, ok := implements(field.v, textUnmarshalerInterface); ok {
label := labels[0]
labels = labels[1:]
err := uv.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(label))
if err != nil {
return participle.Wrapf(block.Pos, err, "invalid label %q", tag.name)
}
} else if field.v.Kind() == reflect.String {
label := labels[0]
labels = labels[1:]
field.v.SetString(label)
} else if field.v.Kind() == reflect.Slice && field.v.Type().Elem().Kind() == reflect.String {
field.v.Set(reflect.ValueOf(labels))
labels = nil
} else {
panic("label field " + fieldID(v.Type(), field.t) + " must be string or []string")
}
}
if len(labels) > 0 {
return participle.Errorf(block.Pos, "too many labels for block %q", block.Name)
}
return unmarshalEntries(v, block.Body, opt)
}
func unmarshalValue(rv reflect.Value, v *Value, opt *marshalState) error {
switch rv.Kind() {
case reflect.String:
switch {
case v.Str != nil:
rv.SetString(*v.Str)
case v.Type != nil:
rv.SetString(*v.Type)
case v.HeredocDelimiter != "":
rv.SetString(v.GetHeredoc())
default:
return participle.Errorf(v.Pos, "expected a type or string but got %s", v)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Number == nil {
return participle.Errorf(v.Pos, "expected a number but got %s", v)
}
n, _ := v.Number.Int64()
rv.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if v.Number == nil {
return participle.Errorf(v.Pos, "expected a number but got %s", v)
}
n, _ := v.Number.Uint64()
rv.SetUint(n)
case reflect.Float32, reflect.Float64:
if v.Number == nil {
return participle.Errorf(v.Pos, "expected a number but got %s", v)
}
n, _ := v.Number.Float64()
rv.SetFloat(n)
case reflect.Map:
if !v.HaveMap {
return participle.Errorf(v.Pos, "expected a map but got %s", v)
}
t := rv.Type()
if t.Key().Kind() != reflect.String {
panic(fmt.Sprintf("map keys must be strings but we have %s", t.Key()))
}
rv.Set(reflect.MakeMap(t))
for _, entry := range v.Map {
key := reflect.New(t.Key()).Elem()
value := reflect.New(t.Elem()).Elem()
switch {
case entry.Key.Str != nil:
key.SetString(*entry.Key.Str)
case entry.Key.Type != nil:
key.SetString(*entry.Key.Type)
default:
panic(fmt.Errorf("map key must be a string or type but is %s", entry.Key))
}
err := unmarshalValue(value, entry.Value, opt)
if err != nil {
return participle.Wrapf(entry.Value.Pos, err, "invalid map value")
}
rv.SetMapIndex(key, value)
}
case reflect.Slice:
if !v.HaveList {
return fmt.Errorf("expected a list but got %s", v)
}
t := rv.Type().Elem()
lv := reflect.MakeSlice(rv.Type(), 0, 4)
for _, entry := range v.List {
value := reflect.New(t).Elem()
err := unmarshalValue(value, entry, opt)
if err != nil {
return participle.Wrapf(entry.Pos, err, "invalid list element")
}
lv = reflect.Append(lv, value)
}
rv.Set(lv)
case reflect.Ptr:
if rv.IsNil() {
pv := reflect.New(rv.Type().Elem())
rv.Set(pv)
}
return unmarshalValue(rv.Elem(), v, opt)
case reflect.Bool:
var ok bool
switch {
case v == nil:
if !opt.bareAttr {
return fmt.Errorf("expected = after attribute")
}
ok = true
case v.Bool == nil:
return participle.Errorf(v.Pos, "expected a bool but got %s", v)
default:
ok = bool(*v.Bool)
}
rv.SetBool(ok)
default:
panic(rv.Kind().String())
}
return nil
}
type field struct {
t reflect.StructField
v reflect.Value
tag tag
}
func flattenFields(v reflect.Value, opt *marshalState) ([]field, error) {
out := make([]field, 0, v.NumField())
t := v.Type()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
ft := t.Field(i)
if ft.Anonymous {
if f.Kind() != reflect.Struct {
return nil, fmt.Errorf("%s: anonymous field must be a struct", ft.Name)
}
sub, err := flattenFields(f, opt)
if err != nil {
return nil, fmt.Errorf("%s: %s", ft.Name, err)
}
out = append(out, sub...)
} else {
tag := parseTag(v.Type(), ft, opt) // nolint: govet
out = append(out, field{ft, f, tag})
}
}
return out, nil
}
func fieldID(parent reflect.Type, t reflect.StructField) string {
return fmt.Sprintf("%s.%s.%s", parent.PkgPath(), parent.Name(), t.Name)
}
type tag struct {
name string
optional bool
label bool
block bool
remain bool
help string
defaultValue string
enum string
}
func (t tag) comments(opts *marshalState) []string {
if opts.schema && t.help != "" {
return strings.Split(t.help, "\n")
}
return nil
}
func parseTag(parent reflect.Type, t reflect.StructField, opt *marshalState) tag {
help := t.Tag.Get("help")
defaultValue := t.Tag.Get("default")
enum := t.Tag.Get("enum")
s, ok := t.Tag.Lookup("hcl")
isBlock := false
if !ok && opt.inferHCLTags {
// if the struct field is a struct or pointer to struct set the tag as block
tt := t.Type
for tt.Kind() == reflect.Ptr || tt.Kind() == reflect.Slice {
tt = tt.Elem()
}
isBlock = tt.Kind() == reflect.Struct
}
if !ok {
s, ok = t.Tag.Lookup("json")
if !ok {
return tag{name: t.Name, block: isBlock, optional: true, help: help, defaultValue: defaultValue, enum: enum}
}
}
parts := strings.Split(s, ",")
name := parts[0]
if name == "-" {
return tag{}
}
id := fieldID(parent, t)
if name == "" {
name = t.Name
}
if len(parts) == 1 {
return tag{name: name, block: isBlock, help: help, defaultValue: defaultValue, optional: defaultValue != "", enum: enum}
}
option := parts[1]
switch option {
case "optional", "omitempty":
return tag{name: name, block: isBlock, optional: true, help: help, defaultValue: defaultValue, enum: enum}
case "label":
return tag{name: name, label: true, help: help}
case "block":
return tag{name: name, block: true, optional: true, help: help}
case "remain":
return tag{name: name, remain: true, help: help}
default:
panic("invalid HCL tag option " + option + " on " + id)
}
}
func implements(v reflect.Value, iface reflect.Type) (reflect.Value, bool) {
if v.Type().Implements(iface) {
return v, true
} else if v.CanAddr() && v.Addr().Type().Implements(iface) {
return v.Addr(), true
}
return reflect.Value{}, false
}
func typeImplements(t reflect.Type, iface reflect.Type) bool {
if t.Implements(iface) {
return true
} else if reflect.PtrTo(t).Implements(iface) {
return true
}
return false
}