-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
352 lines (283 loc) · 6.71 KB
/
generator.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
package main
import (
"fmt"
"os"
"strings"
)
type Generator struct {
Trainer *V1TrainerYaml
environment string
}
type Url struct {
Url string
}
func NewUrl(url interface{}) Url {
switch url.(type) {
case string:
return Url{
Url: url.(string),
}
case Environment:
return Url{
Url: fmt.Sprintf("%s://%s", url.(Environment).Scheme, url.(Environment).Value),
}
case *Environment:
return Url{
Url: fmt.Sprintf("%s://%s", url.(*Environment).Scheme, url.(*Environment).Value),
}
default:
panic("unsupported url type")
}
}
func (u Url) String() string {
return u.Url
}
func (u Url) WithoutScheme() string {
if u.Url == "" {
return ""
}
httpContains := strings.Contains(u.Url, "http://")
if httpContains {
return strings.Replace(u.Url, "http://", "", 1)
}
httpsContains := strings.Contains(u.Url, "https://")
if httpsContains {
return strings.Replace(u.Url, "https://", "", 1)
}
return u.Url
}
func (u Url) Scheme() string {
if u.Url == "" {
return ""
}
httpContains := strings.Contains(u.Url, "http://")
if httpContains {
return "http"
}
httpsContains := strings.Contains(u.Url, "https://")
if httpsContains {
return "https"
}
// TODO: think about this, maybe we return empty string
return "http"
}
func (u Url) Hostname() string {
if u.Url == "" {
return ""
}
withoutScheme := u.WithoutScheme()
withoutScheme = strings.Split(withoutScheme, "/")[0]
return withoutScheme
}
func (u Url) Path() string {
if u.Url == "" {
return ""
}
withoutScheme := u.WithoutScheme()
index := strings.Index(withoutScheme, "/")
if index == -1 {
return ""
}
return withoutScheme[index:]
}
func NewGenerator(trainer *V1TrainerYaml, environment string) *Generator {
checkEnvironment(trainer, environment)
return &Generator{
Trainer: trainer,
environment: environment,
}
}
func (g *Generator) Generate(m map[string]interface{}) map[string]interface{} {
m = g.GenerateForFields("", m)
m = g.GenerateForAbsoluteConfig("", m)
return m
}
func (g *Generator) GenerateForAbsoluteConfig(key string, m map[string]interface{}) map[string]interface{} {
for k, v := range m {
kk := getKey(key, k)
switch v.(type) {
case map[string]interface{}:
m[k] = g.GenerateForAbsoluteConfig(kk, v.(map[string]interface{}))
case []interface{}:
m[k] = g.GenerateForAbsoluteConfigForArray(kk, v.([]interface{}))
case interface{}:
m[k] = g.decideConfigValue(kk, v)
default:
m[k] = v
}
}
return m
}
func (g *Generator) GenerateForAbsoluteConfigValue(k string, v interface{}) {
}
func (g *Generator) GenerateForAbsoluteConfigForArray(k string, arr []interface{}) []interface{} {
for index, v := range arr {
key := fmt.Sprintf("%s[%d]", k, index)
switch v.(type) {
case map[string]interface{}:
arr[index] = g.GenerateForAbsoluteConfig(key, (v).(map[string]interface{}))
case []interface{}:
arr[index] = g.GenerateForAbsoluteConfigForArray(key, v.([]interface{}))
case interface{}:
arr[index] = g.decideConfigValue(key, v)
}
}
return arr
}
func getKey(key string, k string) string {
if key == "" {
return k
}
return key + "." + k
}
func (g *Generator) decideConfigValue(k string, v interface{}) interface{} {
config := g.getConfigValue(k)
if config == nil {
return v
}
return *config
}
func (g *Generator) getConfigValue(k interface{}) *interface{} {
if k == "" {
return nil
}
for _, config := range g.Trainer.Information.AbsoluteConfig {
if config.Key == k {
s := config.Environment[g.environment]
return &s
} else if isMatchesForArray(config.Key, k.(string)) {
s := config.Environment[g.environment]
return &s
} else if isWildCardMatches(config.Key, k.(string)) {
s := config.Environment[g.environment]
return &s
}
}
return nil
}
func (g *Generator) GenerateForFields(key string, m map[string]interface{}) map[string]interface{} {
for k, v := range m {
kk := getKey(key, k)
switch v.(type) {
case string:
m[k] = g.generateString(kk, v.(string))
case map[string]interface{}:
m[k] = g.GenerateForFields(kk, v.(map[string]interface{}))
case []interface{}:
m[k] = v
case int:
m[k] = v
case bool:
m[k] = v
default:
m[k] = v
}
}
return m
}
func (g *Generator) generateString(k, v string) string {
if v == "" {
return v
}
if !strings.Contains(v, "http://") && !strings.Contains(v, "https://") {
text, ok := g.getText(k, v)
if !ok {
return v
}
return text
}
environmentUrl := g.getEnvironmentUrl(k, v)
currentUrl := NewUrl(v)
return fmt.Sprintf("%s://%s%s", environmentUrl.Scheme(), environmentUrl.Hostname(), currentUrl.Path())
}
func (g *Generator) getText(key, text string) (string, bool) {
if text == "" {
return "", false
}
field := g.findField(text)
if field == nil {
//f1, ok := g.getTextByKey(key)
//if !ok {
// return "", false
//}
//return f1, true
return "", false
}
if field.Type != "text" {
return "", false
}
environment := field.Environment[g.environment]
return environment.Value, true
}
func (g *Generator) getTextByKey(k string) (string, bool) {
fieldByValue := g.findFieldByKey(k)
if fieldByValue == nil {
return "", false
}
if fieldByValue.Type != "text" {
return "", false
}
env := fieldByValue.Environment[g.environment]
return env.Value, true
}
func (g *Generator) getEnvironmentUrl(key, currentUrl string) Url {
field := g.findField(currentUrl)
if field != nil {
environment := field.GetEnvironment(g.environment)
if environment == nil {
return NewUrl(currentUrl)
}
return NewUrl(environment)
}
fieldByKey := g.findFieldByKey(key)
if fieldByKey == nil {
return NewUrl(currentUrl)
}
environment := fieldByKey.GetEnvironment(g.environment)
if environment == nil {
return NewUrl(currentUrl)
}
return NewUrl(environment)
}
func (g *Generator) findField(val string) *Field {
for _, field := range g.Trainer.Information.Fields {
for _, environment := range field.Environment {
if environment.Value != "" && NewUrl(val).Hostname() == environment.Value {
return &field
}
}
}
return nil
}
func (g *Generator) findFieldByKey(key string) *Field {
for _, field := range g.Trainer.Information.Fields {
for _, k := range field.Keys {
kLength := len(k)
keyLength := len(key)
if kLength > keyLength {
if strings.Contains(k, key) {
return &field
}
} else {
if strings.Contains(key, k) {
return &field
}
}
}
}
return nil
}
func checkEnvironment(trainer *V1TrainerYaml, environment string) {
for _, field := range trainer.Information.Fields {
isDefined := false
for k, _ := range field.Environment {
if k == environment {
isDefined = true
}
}
if !isDefined {
fmt.Printf("environment %s is not defined in %v\n", environment, field.Keys)
os.Exit(1)
}
}
}