This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
nodes.go
428 lines (364 loc) · 10.8 KB
/
nodes.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
package hclencoder
import (
"errors"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/hcl/hcl/token"
)
const (
// HCLTagName is the struct field tag used by the HCL decoder. The
// values from this tag are used in the same way as the decoder.
HCLTagName = "hcl"
// KeyTag indicates that the value of the field should be part of
// the parent object block's key, not a property of that block
KeyTag string = "key"
// SquashTag is attached to anonymous fields of a struct and indicates
// to the encoder to lift the fields of that value into the parent
// block's scope transparently. Otherwise, the field's type is used as
// the key for the value.
SquashTag string = "squash"
// UnusedKeysTag is a flag that indicates any unused keys found by the
// decoder are stored in this field of type []string. This has the same
// behavior as the OmitTag and is not encoded.
UnusedKeysTag string = "unusedKeys"
// DecodedFieldsTag is a flag that indicates all fields decoded are
// stored in this field of type []string. This has the same behavior as
// the OmitTag and is not encoded.
DecodedFieldsTag string = "decodedFields"
// HCLETagName is the struct field tag used by this package. The
// values from this tag are used in conjunction with HCLTag values.
HCLETagName = "hcle"
// OmitTag will omit this field from encoding. This is the similar
// behavior to `json:"-"`.
OmitTag string = "omit"
// OmitEmptyTag will omit this field if it is a zero value. This
// is similar behavior to `json:",omitempty"`
OmitEmptyTag string = "omitempty"
)
type fieldMeta struct {
anonymous bool
name string
key bool
squash bool
unusedKeys bool
decodedFields bool
omit bool
omitEmpty bool
}
// encode converts a reflected valued into an HCL ast.Node in a depth-first manner.
func encode(in reflect.Value) (node ast.Node, key []*ast.ObjectKey, err error) {
in, isNil := deref(in)
if isNil {
return nil, nil, nil
}
switch in.Kind() {
case reflect.Bool, reflect.Float64, reflect.String,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return encodePrimitive(in)
case reflect.Slice:
return encodeList(in)
case reflect.Map:
return encodeMap(in)
case reflect.Struct:
return encodeStruct(in)
default:
return nil, nil, fmt.Errorf("cannot encode kind %s to HCL", in.Kind())
}
}
// encodePrimitive converts a primitive value into an ast.LiteralType. An
// ast.ObjectKey is never returned.
func encodePrimitive(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
tkn, err := tokenize(in, false)
if err != nil {
return nil, nil, err
}
return &ast.LiteralType{Token: tkn}, nil, nil
}
// encodeList converts a slice to an appropriate ast.Node type depending on its
// element value type. An ast.ObjectKey is never returned.
func encodeList(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
childType := in.Type().Elem()
childLoop:
for {
switch childType.Kind() {
case reflect.Ptr:
childType = childType.Elem()
default:
break childLoop
}
}
switch childType.Kind() {
case reflect.Map, reflect.Struct, reflect.Interface:
return encodeBlockList(in)
default:
return encodePrimitiveList(in)
}
}
// encodePrimitiveList converts a slice of primitive values to an ast.ListType. An
// ast.ObjectKey is never returned.
func encodePrimitiveList(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
l := in.Len()
n := &ast.ListType{List: make([]ast.Node, 0, l)}
for i := 0; i < l; i++ {
child, _, err := encode(in.Index(i))
if err != nil {
return nil, nil, err
}
if child != nil {
n.Add(child)
}
}
return n, nil, nil
}
// encodeBlockList converts a slice of non-primitive types to an ast.ObjectList. An
// ast.ObjectKey is never returned.
func encodeBlockList(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
l := in.Len()
n := &ast.ObjectList{Items: make([]*ast.ObjectItem, 0, l)}
for i := 0; i < l; i++ {
child, childKey, err := encode(in.Index(i))
if err != nil {
return nil, nil, err
}
if child == nil {
continue
}
if childKey == nil {
return encodePrimitiveList(in)
}
item := &ast.ObjectItem{Val: child}
item.Keys = childKey
n.Add(item)
}
return n, nil, nil
}
// encodeMap converts a map type into an ast.ObjectType. Maps must have string
// key values to be encoded. An ast.ObjectKey is never returned.
func encodeMap(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
if keyType := in.Type().Key().Kind(); keyType != reflect.String {
return nil, nil, fmt.Errorf("map keys must be strings, %s given", keyType)
}
l := make(objectItems, 0, in.Len())
for _, key := range in.MapKeys() {
tkn, _ := tokenize(key, true) // error impossible since we've already checked key kind
val, childKey, err := encode(in.MapIndex(key))
if err != nil {
return nil, nil, err
}
if val == nil {
continue
}
switch typ := val.(type) {
case *ast.ObjectList:
// If the item is an object list, we need to flatten out the items.
// Child keys are assumed to be added to the above call to encode
itemKey := &ast.ObjectKey{Token: tkn}
for _, obj := range typ.Items {
keys := append([]*ast.ObjectKey{itemKey}, obj.Keys...)
l = append(l, &ast.ObjectItem{
Keys: keys,
Val: obj.Val,
})
}
default:
item := &ast.ObjectItem{
Keys: []*ast.ObjectKey{{Token: tkn}},
Val: val,
}
if childKey != nil {
item.Keys = append(item.Keys, childKey...)
}
l = append(l, item)
}
}
sort.Sort(l)
return &ast.ObjectType{List: &ast.ObjectList{Items: []*ast.ObjectItem(l)}}, nil, nil
}
// encodeStruct converts a struct type into an ast.ObjectType. An ast.ObjectKey
// may be returned if a KeyTag is present that should be used by a parent
// ast.ObjectItem if this node is nested.
func encodeStruct(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
l := in.NumField()
list := &ast.ObjectList{Items: make([]*ast.ObjectItem, 0, l)}
keys := make([]*ast.ObjectKey, 0)
for i := 0; i < l; i++ {
field := in.Type().Field(i)
meta := extractFieldMeta(field)
// these tags are used for debugging the decoder
// they should not be output
if meta.unusedKeys || meta.decodedFields || meta.omit {
continue
}
tkn, _ := tokenize(reflect.ValueOf(meta.name), true) // impossible to not be string
// if the OmitEmptyTag is provided, check if the value is its zero value.
rawVal := in.Field(i)
if meta.omitEmpty {
zeroVal := reflect.Zero(rawVal.Type()).Interface()
if reflect.DeepEqual(rawVal.Interface(), zeroVal) {
continue
}
}
val, childKeys, err := encode(rawVal)
if err != nil {
return nil, nil, err
}
if val == nil {
continue
}
// this field is a key and should be bubbled up to the parent node
if meta.key {
if lit, ok := val.(*ast.LiteralType); ok && lit.Token.Type == token.STRING {
keys = append(keys, &ast.ObjectKey{Token: lit.Token})
continue
}
return nil, nil, errors.New("struct key fields must be string literals")
}
// this field is anonymous and should be squashed into the parent struct's fields
if meta.anonymous && meta.squash {
switch val := val.(type) {
case *ast.ObjectType:
list.Items = append(list.Items, val.List.Items...)
if childKeys != nil {
keys = childKeys
}
continue
}
}
itemKey := &ast.ObjectKey{Token: tkn}
// if the item is an object list, we need to flatten out the items
if objectList, ok := val.(*ast.ObjectList); ok {
for _, obj := range objectList.Items {
objectKeys := append([]*ast.ObjectKey{itemKey}, obj.Keys...)
list.Add(&ast.ObjectItem{
Keys: objectKeys,
Val: obj.Val,
})
}
continue
}
item := &ast.ObjectItem{
Keys: []*ast.ObjectKey{itemKey},
Val: val,
}
if childKeys != nil {
item.Keys = append(item.Keys, childKeys...)
}
list.Add(item)
}
if len(keys) == 0 {
return &ast.ObjectType{List: list}, nil, nil
}
return &ast.ObjectType{List: list}, keys, nil
}
// tokenize converts a primitive type into an token.Token. IDENT tokens (unquoted strings)
// can be optionally triggered for any string types.
func tokenize(in reflect.Value, ident bool) (t token.Token, err error) {
switch in.Kind() {
case reflect.Bool:
return token.Token{
Type: token.BOOL,
Text: strconv.FormatBool(in.Bool()),
}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return token.Token{
Type: token.NUMBER,
Text: fmt.Sprintf("%d", in.Uint()),
}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return token.Token{
Type: token.NUMBER,
Text: fmt.Sprintf("%d", in.Int()),
}, nil
case reflect.Float64:
return token.Token{
Type: token.FLOAT,
Text: strconv.FormatFloat(in.Float(), 'g', -1, 64),
}, nil
case reflect.String:
if ident {
return token.Token{
Type: token.IDENT,
Text: in.String(),
}, nil
}
return token.Token{
Type: token.STRING,
Text: fmt.Sprintf(`"%s"`, in.String()),
}, nil
}
return t, fmt.Errorf("cannot encode primitive kind %s to token", in.Kind())
}
// extractFieldMeta pulls information about struct fields and the optional HCL tags
func extractFieldMeta(f reflect.StructField) (meta fieldMeta) {
if f.Anonymous {
meta.anonymous = true
meta.name = f.Type.Name()
} else {
meta.name = f.Name
}
tags := strings.Split(f.Tag.Get(HCLTagName), ",")
if len(tags) > 0 {
if tags[0] != "" {
meta.name = tags[0]
}
for _, tag := range tags[1:] {
switch tag {
case KeyTag:
meta.key = true
case SquashTag:
meta.squash = true
case DecodedFieldsTag:
meta.decodedFields = true
case UnusedKeysTag:
meta.unusedKeys = true
}
}
}
tags = strings.Split(f.Tag.Get(HCLETagName), ",")
for _, tag := range tags {
switch tag {
case OmitTag:
meta.omit = true
case OmitEmptyTag:
meta.omitEmpty = true
}
}
return
}
// deref safely dereferences interface and pointer values to their underlying value types.
// It also detects if that value is invalid or nil.
func deref(in reflect.Value) (val reflect.Value, isNil bool) {
switch in.Kind() {
case reflect.Invalid:
return in, true
case reflect.Interface, reflect.Ptr:
if in.IsNil() {
return in, true
}
// recurse for the elusive double pointer
return deref(in.Elem())
case reflect.Slice, reflect.Map:
return in, in.IsNil()
default:
return in, false
}
}
type objectItems []*ast.ObjectItem
func (ol objectItems) Len() int { return len(ol) }
func (ol objectItems) Swap(i, j int) { ol[i], ol[j] = ol[j], ol[i] }
func (ol objectItems) Less(i, j int) bool {
iKeys := ol[i].Keys
jKeys := ol[j].Keys
for k := 0; k < len(iKeys) && k < len(jKeys); k++ {
if iKeys[k].Token.Text == jKeys[k].Token.Text {
continue
}
return iKeys[k].Token.Text < jKeys[k].Token.Text
}
return len(iKeys) <= len(jKeys)
}