-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml2jinja.go
335 lines (295 loc) · 7.23 KB
/
yaml2jinja.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
package main
import (
"fmt"
"reflect"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// Log variable
var Log = log.New()
// LogCtxt variable
var LogCtxt = Log.WithFields(log.Fields{
"tag": "yaml2jinja",
})
var globalForIndent = 0
// New creates Yaml2Jinja object
func New() Yaml2Jinja {
return Yaml2Jinja{}
}
// Yaml2Jinja to store converted result
type Yaml2Jinja struct {
FileName string
Output string
Depth int
ForIndent int
ForDepth []int
Index []string
NextIndex string
}
//Indent ...
func (yj *Yaml2Jinja) Indent() {
var ident int
forIndent := 0
if globalForIndent < yj.ForIndent { // entered for loop; add - sign/ident
forIndent = 1
} else if globalForIndent > yj.ForIndent { // escaped for loop; remove for sign
globalForIndent--
forIndent = -1
}
ident = yj.Depth + globalForIndent
if forIndent == 1 {
globalForIndent++ // increase globalForIndent after for the next processing cycle
}
switch {
case ident == 1:
yj.Output += " "
case ident == 2:
yj.Output += " "
case ident == 3:
yj.Output += " "
case ident == 4:
yj.Output += " "
case ident == 5:
yj.Output += " "
case ident == 6:
yj.Output += " "
case ident == 7:
yj.Output += " "
case ident == 8:
yj.Output += " "
case ident == 9:
yj.Output += " "
case ident == 10:
yj.Output += " "
default:
}
// append - indent at the end
if forIndent == 1 {
yj.Output += "- "
}
}
//ConcatenateKey sr=tring
func (yj *Yaml2Jinja) ConcatenateKey() string {
var cKey string
var start int
if len(yj.ForDepth) > 0 {
start = yj.ForDepth[len(yj.ForDepth)-1]
} else {
start = 1
}
for i := 0; i <= len(yj.Index)-1; i++ {
if i >= start {
if i == start {
cKey += yj.Index[i]
} else if i > 1 {
cKey += "." + yj.Index[i]
}
}
}
if yj.NextIndex != "" {
if cKey == "" {
cKey += yj.NextIndex
} else {
cKey += "." + yj.NextIndex
}
}
return cKey
}
// Append string to jinja output
func (yj *Yaml2Jinja) Append(action string) {
LogCtxt.WithFields(log.Fields{
"function": "Append",
"Depth": yj.Depth,
"Index": yj.Index,
"NextIndex": yj.NextIndex,
"ForDepth": yj.ForDepth,
}).Debug("Append Start")
switch {
case action == "ifStart":
LogCtxt.WithFields(log.Fields{
"function": "Append",
}).Debug("Action is ifStart")
yj.Output += "{% if " + goYamlFormat(yj.ConcatenateKey()) + " is defined %}" + "\n"
case action == "forStart":
LogCtxt.WithFields(log.Fields{
"function": "Append",
}).Debug("Action is forStart")
yj.Output += "{% for " + yj.NextIndex + " in " + goYamlFormat(yj.ConcatenateKey()) + " %}" + "\n"
case action == "ifEnd":
LogCtxt.WithFields(log.Fields{
"function": "Append",
}).Debug("Action is ifEnd")
yj.Output += "{% endif %}" + "\n"
case action == "forEnd":
LogCtxt.WithFields(log.Fields{
"function": "Append",
}).Debug("Action is forEnd")
yj.Output += "{% endfor %}" + "\n"
case action == "itemEmpty":
LogCtxt.WithFields(log.Fields{
"function": "Append",
}).Debug("Action is itemEmpty")
yj.Indent()
yj.Output += yj.NextIndex + ": " + "\n"
case action == "itemCtxt":
LogCtxt.WithFields(log.Fields{
"function": "Append",
}).Debug("Action is itemCtxt")
yj.Indent()
yj.Output += yj.NextIndex + ": " + "{{ " + goYamlFormat(yj.ConcatenateKey()) + " }}" + "\n"
default:
fmt.Println("Unknown")
}
LogCtxt.WithFields(log.Fields{
"function": "Append",
}).Debug("Append End")
}
// goYamlFormat replace - with _
func goYamlFormat(key string) string {
return strings.ReplaceAll(key, "-", "_")
}
// Convert transforms map[string]interface{} to go struct
func (yj *Yaml2Jinja) Convert(data []byte) (string, error) {
// Unmarshal to map[string]interface{}
var obj map[string]interface{}
err := yaml.Unmarshal(data, &obj)
if err != nil {
return "", err
}
for k, v := range obj {
yj.FileName = k + ".j2"
yj.Jinjify(k, v, false)
}
return yj.Output, nil
}
// Jinjify transforms map key values to jinja structs
func (yj *Yaml2Jinja) Jinjify(k string, v interface{}, arrayElem bool) {
//var key string
if reflect.TypeOf(v) == nil || len(k) == 0 {
// nil type
LogCtxt.WithFields(log.Fields{
"function": "Jinjify",
}).Debug("nil type")
return
}
switch reflect.TypeOf(v).Kind() {
// If yaml object
case reflect.Map:
switch val := v.(type) {
case map[interface{}]interface{}:
// YAML object MAP
LogCtxt.WithFields(log.Fields{
"function": "Jinjify",
}).Debug("YAML object MAP")
yj.Depth++
yj.Index = append(yj.Index, k)
if !arrayElem {
// Create new structure
LogCtxt.WithFields(log.Fields{
"function": "Jinjify",
"Depth": yj.Depth,
"Index": yj.Index,
"NextIndex": yj.NextIndex,
"ForDepth": yj.ForDepth,
}).Debug("Not an arrayElem")
}
// If array of yaml objects
for k1, v1 := range val {
// YAML object MAP loop
yj.NextIndex = k1.(string)
LogCtxt.WithFields(log.Fields{
"function": "Jinjify",
"Depth": yj.Depth,
"Index": yj.Index,
"NextIndex": yj.NextIndex,
"ForDepth": yj.ForDepth,
}).Debug("YAML object MAP loop")
if _, ok := k1.(string); ok {
yj.Append("ifStart")
if v1 == nil {
yj.Append("itemCtxt")
} else {
yj.Append("itemEmpty")
}
yj.Jinjify(k1.(string), v1, false)
yj.Append("ifEnd")
}
}
if !arrayElem {
// Not an arrayElem 2
yj.Depth--
yj.Index = yj.Index[:len(yj.Index)-1]
LogCtxt.WithFields(log.Fields{
"function": "Jinjify",
"Depth": yj.Depth,
"Index": yj.Index,
"NextIndex": yj.NextIndex,
"ForDepth": yj.ForDepth,
}).Debug("Not an arrayElem 2")
}
}
// If array
case reflect.Slice:
val := v.([]interface{})
if len(val) == 0 {
return
}
switch val[0].(type) {
case string, int, bool, float64:
LogCtxt.WithFields(log.Fields{
"function": "Jinjify",
"Depth": yj.Depth,
"Index": yj.Index,
"NextIndex": yj.NextIndex,
"ForDepth": yj.ForDepth,
}).Debug("string, int, bool, float64")
// if nested object
case map[interface{}]interface{}:
// Slice Object MAP
LogCtxt.WithFields(log.Fields{
"function": "Jinjify",
"Depth": yj.Depth,
"Index": yj.Index,
"NextIndex": yj.NextIndex,
"ForDepth": yj.ForDepth,
}).Debug("Slice object MAP")
key := k
// start for loop
yj.Append("forStart")
yj.ForDepth = append(yj.ForDepth, yj.Depth)
yj.ForIndent++
for _, v1 := range val {
// continue to loop through elements
yj.Jinjify(key, v1, true)
}
// stop for loop
yj.Append("forEnd")
if len(yj.ForDepth) > 0 {
yj.ForDepth = yj.ForDepth[:len(yj.ForDepth)-1]
}
yj.Depth--
yj.ForIndent--
yj.Index = yj.Index[:len(yj.Index)-1]
}
default:
// Default
LogCtxt.WithFields(log.Fields{
"function": "Jinjify",
"Depth": yj.Depth,
"Index": yj.Index,
"NextIndex": yj.NextIndex,
"ForDepth": yj.ForDepth,
}).Debug("Default")
}
}
func init() {
// Log as default ASCII formatter.
Log.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: false,
})
// Only log the info severity or above.
//Log.SetLevel(log.InfoLevel)
//Log.SetLevel(log.DebugLevel)
}