forked from rodaine/hclencoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.go
174 lines (162 loc) · 4.67 KB
/
tokens.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
package hclencoder
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
"reflect"
"sort"
)
// tokenize converts a primitive type into tokens. structs and maps are converted into objects and slices are converted
// into tuples.
func tokenize(in reflect.Value, meta fieldMeta) (tkns hclwrite.Tokens, err error) {
tokenEqual := hclwrite.Token{
Type: hclsyntax.TokenEqual,
Bytes: []byte("="),
SpacesBefore: 0,
}
tokenComma := hclwrite.Token{
Type: hclsyntax.TokenComma,
Bytes: []byte(","),
SpacesBefore: 0,
}
tokenOCurlyBrace := hclwrite.Token{
Type: hclsyntax.TokenOBrace,
Bytes: []byte("{"),
SpacesBefore: 0,
}
tokenCCurlyBrace := hclwrite.Token{
Type: hclsyntax.TokenCBrace,
Bytes: []byte("}"),
SpacesBefore: 0,
}
switch in.Kind() {
case reflect.Bool:
return hclwrite.TokensForValue(cty.BoolVal(in.Bool())), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return hclwrite.TokensForValue(cty.NumberUIntVal(in.Uint())), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return hclwrite.TokensForValue(cty.NumberIntVal(in.Int())), nil
case reflect.Float64:
return hclwrite.TokensForValue(cty.NumberFloatVal(in.Float())), nil
case reflect.String:
val := in.String()
if !meta.expression {
return hclwrite.TokensForValue(cty.StringVal(val)), nil
}
// Unfortunately hcl escapes template expressions (${...}) when using hclwrite.TokensForValue. So we escape
// everything but template expressions and then parse the expression into tokens.
tokens, diags := hclsyntax.LexExpression([]byte(val), meta.name, hcl.Pos{
Line: 0,
Column: 0,
Byte: 0,
})
if diags != nil {
return nil, fmt.Errorf("error when parsing string %s: %v", val, diags.Error())
}
return convertTokens(tokens), nil
case reflect.Pointer, reflect.Interface:
val, isNil := deref(in)
if isNil {
return nil, nil
}
return tokenize(val, meta)
case reflect.Struct:
var tokens []*hclwrite.Token
tokens = append(tokens, &tokenOCurlyBrace)
for i := 0; i < in.NumField(); i++ {
field := in.Type().Field(i)
meta := extractFieldMeta(field)
rawVal := in.Field(i)
if meta.omitEmpty {
zeroVal := reflect.Zero(rawVal.Type()).Interface()
if reflect.DeepEqual(rawVal.Interface(), zeroVal) {
continue
}
}
val, err := tokenize(rawVal, meta)
if err != nil {
return nil, err
}
for _, tkn := range hclwrite.TokensForValue(cty.StringVal(meta.name)) {
tokens = append(tokens, tkn)
}
tokens = append(tokens, &tokenEqual)
for _, tkn := range val {
tokens = append(tokens, tkn)
}
if i < in.NumField()-1 {
tokens = append(tokens, &tokenComma)
}
}
tokens = append(tokens, &tokenCCurlyBrace)
return tokens, nil
case reflect.Slice:
var tokens []*hclwrite.Token
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenOBrace,
Bytes: []byte("["),
SpacesBefore: 0,
})
for i := 0; i < in.Len(); i++ {
value, err := tokenize(in.Index(i), meta)
if err != nil {
return nil, err
}
for _, tkn := range value {
tokens = append(tokens, tkn)
}
if i < in.Len()-1 {
tokens = append(tokens, &tokenComma)
}
}
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenCBrace,
Bytes: []byte("]"),
SpacesBefore: 0,
})
return tokens, nil
case reflect.Map:
if keyType := in.Type().Key().Kind(); keyType != reflect.String {
return nil, fmt.Errorf("map keys must be strings, %s given", keyType)
}
var tokens []*hclwrite.Token
tokens = append(tokens, &tokenOCurlyBrace)
var keys []string
for _, k := range in.MapKeys() {
keys = append(keys, k.String())
}
sort.Strings(keys)
for i, k := range keys {
val, err := tokenize(in.MapIndex(reflect.ValueOf(k)), meta)
if err != nil {
return nil, err
}
for _, tkn := range hclwrite.TokensForValue(cty.StringVal(k)) {
tokens = append(tokens, tkn)
}
tokens = append(tokens, &tokenEqual)
for _, tkn := range val {
tokens = append(tokens, tkn)
}
if i < len(keys)-1 {
tokens = append(tokens, &tokenComma)
}
}
tokens = append(tokens, &tokenCCurlyBrace)
return tokens, nil
}
return nil, fmt.Errorf("cannot encode primitive kind %s to token", in.Kind())
}
func convertTokens(tokens hclsyntax.Tokens) hclwrite.Tokens {
var result []*hclwrite.Token
for _, token := range tokens {
result = append(result, &hclwrite.Token{
Type: token.Type,
Bytes: token.Bytes,
SpacesBefore: 0,
})
}
return result
}