/
formatter.go
280 lines (268 loc) · 7.06 KB
/
formatter.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
package importer
import (
"fmt"
"regexp"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/polytomic/polytomic-go"
"github.com/zclconf/go-cty/cty"
"golang.org/x/exp/slices"
)
// ReplaceRefs takes in an array of bytes and a map of references
// It will parse the array of bytes and replace any reference byte sequences
// with the value from the map.
func ReplaceRefs(b []byte, refs map[string]string) []byte {
s := string(b)
for k, v := range refs {
// Replace all instances of the reference and the enclosing quotes
re := regexp.MustCompile(`"` + k + `"`)
s = re.ReplaceAllString(s, v)
}
return []byte(s)
}
func unquoteVariableRef(b []byte) []byte {
s := string(b)
re := regexp.MustCompile(`\"(var\..*)\"`)
s = re.ReplaceAllString(s, "$1")
return []byte(s)
}
// convert arbitrary values to cty.Value
func typeConverter(value any) cty.Value {
switch value := value.(type) {
case map[string]any:
config := make(map[string]cty.Value)
for k, v := range value {
if v == nil {
continue
}
switch v := v.(type) {
case *string:
if v == nil {
continue
} else if *v == "" {
continue
}
config[k] = cty.StringVal(*v)
case string:
if v == "" {
continue
}
config[k] = cty.StringVal(v)
case int:
config[k] = cty.NumberIntVal(int64(v))
case *int:
if v == nil {
continue
}
config[k] = cty.NumberIntVal(int64(*v))
case float64:
config[k] = cty.NumberFloatVal(v)
case *bool:
if v == nil {
continue
}
config[k] = cty.BoolVal(*v)
case bool:
config[k] = cty.BoolVal(v)
case map[string]any:
config[k] = typeConverter(v)
case map[string]string:
config[k] = typeConverter(v)
case []string:
if len(v) == 0 {
continue
}
vals := make([]cty.Value, 0)
for _, v := range v {
vals = append(vals, cty.StringVal(v))
}
if len(vals) == 0 {
continue
}
config[k] = cty.ListVal(vals)
case []any:
if len(v) == 0 {
continue
}
vals := make([]cty.Value, 0)
for _, v := range v {
switch v := v.(type) {
case map[string]any:
vals = append(vals, typeConverter(v))
case string:
vals = append(vals, cty.StringVal(v))
case int:
vals = append(vals, cty.NumberIntVal(int64(v)))
case float64:
vals = append(vals, cty.NumberFloatVal(v))
case bool:
vals = append(vals, cty.BoolVal(v))
default:
fmt.Printf("Unknown type for %s: %T in array\n", k, v)
continue
}
}
if len(vals) == 0 {
continue
}
config[k] = cty.ListVal(vals)
case *polytomic.RunAfter:
if v == nil {
continue
}
config[k] = typeConverter(v)
case *polytomic.Source:
if v == nil {
continue
}
config[k] = typeConverter(v)
case polytomic.ScheduleFrequency:
config[k] = cty.StringVal(string(v))
default:
fmt.Printf("Unknown type for %s: %T in map\n", k, v)
continue
}
}
return cty.ObjectVal(config)
case []map[string]any:
vals := make([]cty.Value, 0)
for _, v := range value {
vals = append(vals, typeConverter(v))
}
if len(vals) == 0 {
return cty.EmptyTupleVal
}
return cty.TupleVal(vals)
case []string:
vals := make([]cty.Value, 0)
for _, v := range value {
vals = append(vals, cty.StringVal(v))
}
if len(vals) == 0 {
return cty.ListValEmpty(cty.String)
}
return cty.ListVal(vals)
case []any:
if len(value) == 0 {
return cty.NilVal
}
vals := make([]cty.Value, 0)
for _, v := range value {
switch v := v.(type) {
case map[string]any:
vals = append(vals, typeConverter(v))
case string:
vals = append(vals, cty.StringVal(v))
case int:
vals = append(vals, cty.NumberIntVal(int64(v)))
case float64:
vals = append(vals, cty.NumberFloatVal(v))
case bool:
vals = append(vals, cty.BoolVal(v))
default:
fmt.Printf("Unknown type for %s: %T in array\n", value, v)
continue
}
}
return cty.ListVal(vals)
case map[string]*string:
config := make(map[string]cty.Value)
for k, v := range value {
if v == nil {
continue
}
if *v != "" {
config[k] = cty.StringVal(*v)
}
}
return cty.ObjectVal(config)
case map[string]string:
config := make(map[string]cty.Value)
for k, v := range value {
if v != "" {
config[k] = cty.StringVal(v)
}
}
return cty.ObjectVal(config)
case *string:
if value == nil {
return cty.NilVal
}
return cty.StringVal(*value)
case string:
if value == "" {
return cty.NilVal
}
return cty.StringVal(value)
case nil:
return cty.NilVal
case bool:
return cty.BoolVal(value)
case float64:
return cty.NumberFloatVal(value)
case *polytomic.Source:
if value == nil {
return cty.NilVal
}
config := map[string]cty.Value{
"field": cty.StringVal(value.Field),
"model_id": cty.StringVal(value.ModelId),
}
return cty.ObjectVal(config)
default:
fmt.Printf("Unknown type: %T\n", value)
return cty.NilVal
}
}
// wrapJSONEncode wraps the given attribute names within the given map[string]any
// or []map[string]any with a jsonencode function.
func wrapJSONEncode(v interface{}, wrapped ...string) hclwrite.Tokens {
var tokens hclwrite.Tokens
if len(wrapped) == 0 {
// Assume the whole thing is wrapped
tokens = append(tokens, hclwrite.Tokens{{Bytes: []byte("jsonencode(")}}...)
tokens = append(tokens, hclwrite.TokensForValue(typeConverter(v))...)
tokens = append(tokens, hclwrite.Tokens{{Bytes: []byte(")")}}...)
return tokens
}
switch v := v.(type) {
case map[string]any:
tokens = append(tokens, jsonEncodeMap(v, wrapped...)...)
case []map[string]any:
tokens = append(tokens, hclwrite.Tokens{{Bytes: []byte("[")}}...)
for _, v := range v {
tokens = append(tokens, jsonEncodeMap(v, wrapped...)...)
tokens = append(tokens, hclwrite.Tokens{{Bytes: []byte(",")}}...)
}
tokens = append(tokens, hclwrite.Tokens{{Bytes: []byte("]")}}...)
default:
fmt.Printf("Unknown type: %T in jsonencode wrapper\n", v)
}
return tokens
}
// jsonEncodeMap wraps the given attribute names with a jsonencode function.
func jsonEncodeMap(m map[string]any, wrapped ...string) hclwrite.Tokens {
var tokens hclwrite.Tokens
tokens = append(tokens, &hclwrite.Token{Bytes: []byte("{\n")})
for _, k := range sortedKeys(m) {
v := m[k]
value := typeConverter(v)
if value == cty.NilVal {
continue
}
if slices.Contains(wrapped, k) {
tokens = append(tokens, &hclwrite.Token{Bytes: []byte(k)})
tokens = append(tokens, &hclwrite.Token{Bytes: []byte("=")})
tokens = append(tokens, &hclwrite.Token{Bytes: []byte("jsonencode(")})
tokens = append(tokens, hclwrite.TokensForValue(typeConverter(v))...)
tokens = append(tokens, &hclwrite.Token{Bytes: []byte(")")})
tokens = append(tokens, &hclwrite.Token{Bytes: []byte("\n")})
} else {
tokens = append(tokens, &hclwrite.Token{Bytes: []byte(k)})
tokens = append(tokens, &hclwrite.Token{Bytes: []byte("=")})
tokens = append(tokens, hclwrite.TokensForValue(typeConverter(v))...)
tokens = append(tokens, &hclwrite.Token{Bytes: []byte("\n")})
}
}
tokens = append(tokens, &hclwrite.Token{Bytes: []byte("}")})
return tokens
}