-
Notifications
You must be signed in to change notification settings - Fork 11
/
generator.go
executable file
·562 lines (457 loc) · 13.3 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"bytes"
"github.com/aymerick/raymond"
"regexp"
"sort"
)
type Protocol struct {
Version Version `json:"version,omitempty"`
Domains []Domain `json:"domains,omitempty"`
}
type EventMapping struct {
EventName string
Class string
}
func (p Protocol) EventMappings() []EventMapping {
mappings := make(map[string]string)
for _, domain := range p.Domains {
for _, event := range domain.Events {
if event.HasReturnValue() {
mappings[fmt.Sprintf("%s.%s", domain.Name, event.Name)] = fmt.Sprintf("%s.api.%s.%s", basePackage, domain.LowerName(), event.ClassName())
} else {
mappings[fmt.Sprintf("%s.%s", domain.Name, event.Name)] = fmt.Sprintf("%s.%s", basePackage, "protocol.RawEvent")
}
}
}
retval := make([]EventMapping, 0)
for event, class := range mappings {
retval = append(retval, EventMapping{
EventName: event,
Class: class,
})
}
sort.Slice(retval, func(i, j int) bool {
return retval[i].EventName < retval[j].EventName
})
return retval
}
type Version struct {
Major string `json:"major,omitempty"`
Minor string `json:"minor,omitempty"`
}
type Domain struct {
Name string `json:"domain,omitempty"`
Experimental bool `json:"experimental,omitempty"`
Types []Type `json:"types"`
Commands []Command `json:"commands,omitempty"`
Events []Event `json:"events,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Description string `json:"description,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
}
func (d Domain) Interfaces() string {
interfaces := make([]string, 0)
interfaces = append(interfaces, fmt.Sprintf("%s.protocol.Domain(\"%s\", \"\"\"%s\"\"\", connection)", basePackage, d.Name, d.Description))
return fmt.Sprintf(": %s", strings.Join(interfaces, ", "))
}
func (d Domain) Annotations() string {
annotations := make([]string, 0)
if d.Deprecated {
if d.Description == "" {
annotations = append(annotations, fmt.Sprintf("@Deprecated(level = DeprecationLevel.WARNING, message = \"%s is deprecated.\")", d.Name))
}
if d.Description != "" {
annotations = append(annotations, fmt.Sprintf("@Deprecated(level = DeprecationLevel.WARNING, message = \"%s\")", d.Description))
}
}
if d.Experimental {
annotations = append(annotations, fmt.Sprintf("@%s.protocol.Experimental", basePackage))
}
return strings.Join(annotations, "\n")
}
func (d Domain) LowerName() string {
return strings.ToLower(d.Name)
}
func (d Domain) UpperName() string {
return strings.ToUpper(d.Name)
}
type Type struct {
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Enum []string `json:"enum,omitempty"`
Description string `json:"description,omitempty"`
Properties []Parameter `json:"properties,omitempty"`
Experimental bool `json:"experimental,omitempty"`
Items *RefItem `json:"items,omitempty"`
MaxItems uint `json:"maxItems,omitempty"`
MinItems uint `json:"minItems,omitempty"`
}
func (t Type) HasProperties() bool {
return len(t.Properties) > 0
}
func (t Type) IsEnum() bool {
return len(t.Enum) > 0
}
type Command struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Returns []Parameter `json:"returns,omitempty"`
Parameters []Parameter `json:"parameters,omitempty"`
Experimental bool `json:"experimental,omitempty"`
Redirect string `json:"redirect,omitempty"`
Handlers []string `json:"handlers,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
}
func (c Command) Annotations() string {
annotations := make([]string, 0)
if c.Deprecated {
annotations = append(annotations, fmt.Sprintf("@Deprecated(level = DeprecationLevel.WARNING, message = \"%s is deprecated.\")", c.Name))
}
if c.Experimental {
annotations = append(annotations, fmt.Sprintf("@%s.protocol.Experimental", basePackage))
}
return strings.Join(annotations, "\n")
}
func (c Command) MethodModifiers() string {
return ""
}
func (c Command) SimpleName() string {
return strings.Title(c.Name)
}
func (c Command) ClassName() string {
return c.SimpleName() + "Response"
}
func (c Command) InputDataClass() string {
if !c.HasInputParams() {
return ""
}
template, err := readTemplate("input_data_class")
if err != nil {
log.Panicf("Could not render template: %s", err)
}
return raymond.MustRender(template, c)
}
func (c Command) OutputDataClass() string {
if !c.HasReturnValue() {
return ""
}
template, err := readTemplate("output_data_class")
if err != nil {
log.Panicf("Could not render template: %s", err)
}
return raymond.MustRender(template, struct {
Type Command
IsOutput bool
Description string
Domain string
}{Type: c, IsOutput: true, Domain: currentDomain})
}
func (c Command) HasReturnValue() bool {
return len(c.Returns) > 0
}
func (c Command) HasInputParams() bool {
return len(c.Parameters) > 0
}
type RefItem struct {
Reference string `json:"$ref,omitempty"`
Type string `json:"type,omitempty"`
Enum []string `json:"enum,omitempty"`
Description string `json:"description,omitempty"`
}
type Parameter struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Reference string `json:"$ref,omitempty"`
Optional bool `json:"optional,omitempty"`
Enum []string `json:"enum,omitempty"`
Experimental bool `json:"experimental,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Items *RefItem `json:"items,omitempty"`
MinItems uint `json:"minItems,omitempty"`
MaxItems uint `json:"maxItems,omitempty"`
}
func sanitizeReference(ref string) string {
parts := strings.Split(ref, ".")
if len(parts) == 1 {
return ref
}
return fmt.Sprintf("%s.api.%s.%s", basePackage, strings.ToLower(parts[0]), parts[1])
}
func (p Parameter) ReferenceImport() string {
var ref string = ""
if p.Reference != "" {
ref = p.Reference
}
if p.Items != nil && p.Items.Reference != "" {
ref = p.Items.Reference
}
parts := strings.Split(ref, ".")
if len(parts) == 2 {
return fmt.Sprintf("%s.%s", strings.ToLower(parts[0]), parts[1])
}
return ""
}
func (p Parameter) GetFormattedType() string {
if p.Reference != "" {
return sanitizeReference(p.Reference)
}
if p.Type != "" {
switch p.Type {
case "string":
return "String"
case "boolean":
return "Boolean"
case "integer":
return "Int"
case "number":
return "Double"
case "array":
return fmt.Sprintf("List<%s>", p.GetParameterArrayType())
case "any", "object":
return "kotlinx.serialization.json.JsonElement"
case "binary":
return "String"
default:
panic("Unknown type " + p.Type)
}
}
return fmt.Sprintf("UnknownType<%v>", p)
}
func (p Parameter) GetParameterArrayType() string {
if p.Items != nil && p.Items.Reference != "" {
return sanitizeReference(p.Items.Reference)
}
if p.Items.Type != "" {
switch p.Items.Type {
case "string":
return "String"
case "boolean":
return "Boolean"
case "integer":
return "Int"
case "any", "object":
return "kotlinx.serialization.json.JsonElement"
case "number":
return "Double"
}
}
return fmt.Sprintf("UnknownType<%v>", p)
}
func (t Type) GetFormattedType() string {
if t.Type != "" {
switch t.Type {
case "string":
return "String"
case "boolean":
return "Bool"
case "integer":
return "Int"
case "number":
return "Double"
case "array":
return fmt.Sprintf("List<%s>", t.GetParameterArrayType())
case "object":
return "String"
case "any":
return "kotlinx.serialization.json.JsonElement"
default:
panic("Unknown type " + t.Type)
}
}
return fmt.Sprintf("UnknownType<%v>", t)
}
func (t Type) GetParameterArrayType() string {
if t.Items != nil && t.Items.Reference != "" {
return sanitizeReference(t.Items.Reference)
}
if t.Items.Type != "" {
switch t.Items.Type {
case "string":
return "String"
case "boolean":
return "Bool"
case "integer":
return "Int"
case "number":
return "Double"
case "any":
return "kotlinx.serialization.json.JsonElement"
case "object":
return "Object"
}
}
return fmt.Sprintf("UnknownType<%v>", t)
}
type Event struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Returns []Parameter `json:"parameters,omitempty"`
Experimental bool `json:"experimental,omitempty"`
}
func (e Event) OutputDataClass() string {
if !e.HasReturnValue() {
return ""
}
template, err := readTemplate("output_data_class")
if err != nil {
log.Panicf("Could not render template: %s", err)
}
return raymond.MustRender(template, struct {
Type Event
IsEvent bool
Domain string
}{Domain: currentDomain, Type: e, IsEvent: true})
}
func (e Event) SimpleName() string {
return strings.Title(e.Name)
}
func (e Event) HasReturnValue() bool {
return len(e.Returns) > 0
}
func (e Event) ClassName() string {
return e.SimpleName() + "Event"
}
func (e Event) ImplementingInterfaces() string {
return fmt.Sprintf("%s.protocol.Event {\noverride fun domain() = \"%s\" \n override fun eventName() = \"%s\"\n }", basePackage, currentDomain, e.Name)
}
var protocolFile string
var basePackage string
var kotlinBase string
var currentDomain string
func init() {
flag.StringVar(&protocolFile, "protocol-file", "protocol.json", "")
flag.StringVar(&basePackage, "base-package", "pl.wendigo.chrome", "")
flag.StringVar(&kotlinBase, "kotlin-base", "src/main/kotlin", "")
}
func kotlinFilename(pkg string) string {
fullpath := fmt.Sprintf("%s/%s/%s.kt", kotlinBase, strings.Replace(basePackage, ".", "/", -1), pkg)
dir := filepath.Dir(fullpath)
log.Printf("Creating dir: %s for %s", dir, pkg)
err := os.MkdirAll(dir, 0700)
if err != nil {
log.Fatalf("Could not create dir: %s: %s", dir, err)
}
return fullpath
}
var camelRegex = regexp.MustCompile("[0-9A-Za-z]+")
func EnumName(src string) string {
byteSrc := []byte(src)
chunks := camelRegex.FindAll(byteSrc, -1)
for idx, val := range chunks {
chunks[idx] = bytes.ToUpper(val)
}
return string(bytes.Join(chunks, []byte("_")))
}
func readTemplate(name string) (string, error) {
bytes, err := ioutil.ReadFile("generator_templates/" + name + ".hbs")
return string(bytes), err
}
func generateAndWrite(filename, tpl string, ctx interface{}) error {
template, err := readTemplate(tpl)
if err != nil {
return err
}
content := raymond.MustRender(template, ctx)
if err := ioutil.WriteFile(filename, []byte(content), 0700); err != nil {
return err
}
return nil
}
func main() {
flag.Parse()
file, err := os.Open(protocolFile)
if err != nil {
log.Panicf("Could not open file: %v", file)
}
protocol := &Protocol{}
if err := json.NewDecoder(file).Decode(protocol); err != nil {
log.Panicf("Could not read json: %s", err)
}
raymond.RegisterHelper("value", func(name string, options *raymond.Options) string {
switch name {
case "this":
return "@kotlinx.serialization.SerialName(\"this\") val _this"
case "object":
return "@kotlinx.serialization.SerialName(\"object\") val _object"
}
return fmt.Sprintf("val %s", name)
})
raymond.RegisterHelper("Package", func(options *raymond.Options) string {
return basePackage
})
raymond.RegisterHelper("Domain", func(options *raymond.Options) string {
return currentDomain
})
raymond.RegisterHelper("trim", func(value string, options *raymond.Options) string {
clean := strings.TrimSpace(value)
if len(clean) > 0 {
return " " + clean
}
return ""
})
raymond.RegisterHelper("lower", func(value string, options *raymond.Options) string {
return strings.ToLower(value)
})
raymond.RegisterHelper("EnumName", func(value string, options *raymond.Options) string {
switch value {
case "Infinity":
return "PLUS_INFINITY"
case "-Infinity":
return "MINUS_INFINITY"
case "-0":
return "ZERO"
}
return EnumName(value)
})
log.Println("Generating protocol class")
if err := generateAndWrite(kotlinFilename("api/ProtocolDomains"), "protocol_domains", struct {
Protocol Protocol
}{
Protocol: *protocol,
}); err != nil {
log.Panicf("Could not generate file: %s", err)
}
log.Println("Generating event serializers")
if err := generateAndWrite(kotlinFilename("api/EventSerializers"), "event_serializers", struct {
Events []EventMapping
}{
Events: protocol.EventMappings(),
}); err != nil {
log.Panicf("Could not generate file: %s", err)
}
if err := generateAndWrite("domains.md", "markdown", struct {
Domains []Domain
}{
Domains: protocol.Domains,
}); err != nil {
log.Panicf("Could not generate file: %s", err)
}
for _, domain := range protocol.Domains {
log.Printf("Generating classes for domain: %s in %s", domain.Name, basePackage)
currentDomain = domain.Name
if err := generateAndWrite(kotlinFilename("api/"+domain.LowerName()+"/Domain"), "protocol_domain", struct {
Domain Domain
}{
Domain: domain,
}); err != nil {
log.Panicf("Could not write file: %s", err)
}
if err := generateAndWrite(kotlinFilename("api/"+domain.LowerName()+"/Types"), "types", struct {
Domain Domain
}{
Domain: domain,
}); err != nil {
log.Panicf("Could not write file: %s", err)
}
}
}