-
Notifications
You must be signed in to change notification settings - Fork 105
/
helper.go
698 lines (635 loc) · 17.2 KB
/
helper.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
Copyright 2018 Caicloud Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package golang
import (
"fmt"
"path"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"github.com/caicloud/nirvana/definition"
"github.com/caicloud/nirvana/service"
"github.com/caicloud/nirvana/utils/api"
)
// Type abstracts common ability from type declarations.
type Type interface {
// Name returns type name.
Name() string
// Generate generates type codes.
Generate() []byte
}
type basicType struct {
name string
comments string
target string
}
func (t *basicType) Name() string {
return t.name
}
func (t *basicType) Generate() []byte {
buf := api.NewBuffer()
buf.Write(t.comments)
buf.Writef("type %s %s\n", t.name, t.target)
return buf.Bytes()
}
type arrayType struct {
name string
comments string
elem string
}
func (t *arrayType) Name() string {
return t.name
}
func (t *arrayType) Generate() []byte {
buf := api.NewBuffer()
buf.Write(t.comments)
buf.Writef("type %s []%s\n", t.name, t.elem)
return buf.Bytes()
}
type pointerType struct {
name string
comments string
elem string
}
func (t *pointerType) Name() string {
return t.name
}
func (t *pointerType) Generate() []byte {
buf := api.NewBuffer()
buf.Write(t.comments)
buf.Writef("type %s *%s\n", t.name, t.elem)
return buf.Bytes()
}
type mapType struct {
name string
comments string
key string
elem string
}
func (t *mapType) Name() string {
return t.name
}
func (t *mapType) Generate() []byte {
buf := api.NewBuffer()
buf.Write(t.comments)
buf.Writef("type %s map[%s]%s\n", t.name, t.key, t.elem)
return buf.Bytes()
}
type structField struct {
name string
comments string
typ string
tag string
}
type structType struct {
name string
comments string
fields []structField
}
func (t *structType) Name() string {
return t.name
}
func (t *structType) Generate() []byte {
buf := api.NewBuffer()
buf.Write(t.comments)
buf.Writef("type %s struct {\n", t.name)
for _, field := range t.fields {
buf.Write(field.comments)
buf.Writef("%s %s", field.name, field.typ)
if field.tag != "" {
buf.Write(" `" + field.tag + "`")
}
buf.Writeln()
}
buf.Write("}\n")
return buf.Bytes()
}
type parameterExtension struct {
Source string
Name string
Key string
}
type functionParameter struct {
Source string
Name string
ProposedName string
Typ string
Extensions []parameterExtension
}
type functionResult struct {
Destination string
ProposedName string
Typ string
Creator string
}
type function struct {
Path string
Method string
Code int
Name string
Comments string
Parameters []functionParameter
Results []functionResult
}
// helper provides methods to help to generate codes.
type helper struct {
definitions *api.Definitions
namer *typeNamer
rootPkg string
}
// newHelper creates a generator helper.
func newHelper(rootPkg string, definitions *api.Definitions) (*helper, error) {
namer, err := newTypeNamer(rootPkg, definitions.Types)
if err != nil {
return nil, err
}
return &helper{definitions, namer, rootPkg}, nil
}
// Types returns types which is required to generate.
func (h *helper) Types() ([]Type, []string) {
types := []Type{}
generatedTypes := []*api.Type{}
for name, typ := range h.definitions.Types {
if typ.Kind == reflect.Func || typ.PkgPath == "" ||
strings.Contains(typ.PkgPath, "/vendor/") ||
!(typ.PkgPath == h.rootPkg || strings.HasPrefix(typ.PkgPath, h.rootPkg+"/")) {
// Ignore functions.
// Ignore unnamed types.
// Ignore vendored types.
// Ignore types which are from standard and third-party libraries.
continue
}
target := h.namer.Name(name)
switch typ.Kind {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.String:
types = append(types, &basicType{
name: target,
comments: h.namer.Comments(name),
target: typ.Kind.String(),
})
case reflect.Array, reflect.Slice:
generatedTypes = append(generatedTypes, typ)
types = append(types, &arrayType{
name: target,
comments: h.namer.Comments(name),
elem: h.namer.Name(typ.Elem),
})
case reflect.Ptr:
generatedTypes = append(generatedTypes, typ)
types = append(types, &pointerType{
name: target,
comments: h.namer.Comments(name),
elem: h.namer.Name(typ.Elem),
})
case reflect.Map:
generatedTypes = append(generatedTypes, typ)
types = append(types, &mapType{
name: target,
comments: h.namer.Comments(name),
key: h.namer.Name(typ.Key),
elem: h.namer.Name(typ.Elem),
})
case reflect.Struct:
generatedTypes = append(generatedTypes, typ)
s := &structType{
name: target,
comments: h.namer.Comments(name),
}
types = append(types, s)
for _, field := range typ.Fields {
c := api.ParseComments(field.Comments)
// Ignore field options.
c.CleanOptions()
sf := structField{
comments: c.LineComments(),
typ: h.namer.Name(field.Type),
tag: string(field.Tag),
}
if !field.Anonymous {
sf.name = field.Name
}
s.fields = append(s.fields, sf)
}
}
}
sort.Sort(typeSorter(types))
return types, h.packages(generatedTypes, true)
}
// packages generates a list of imported packages with aliases.
func (h *helper) packages(types []*api.Type, extended bool) []string {
pkgMap := map[string]bool{}
for _, typ := range types {
pkgs := h.pkgs(typ, extended)
for _, pkg := range pkgs {
pkgMap[pkg] = true
}
}
results := []string{}
for pkg := range pkgMap {
alias := h.namer.Alias(pkg)
results = append(results, fmt.Sprintf(`%s "%s"`, alias, pkg))
}
return results
}
// pkgs generates a list of imported packages without aliases.
func (h *helper) pkgs(typ *api.Type, extended bool) []string {
switch typ.Kind {
case reflect.Array, reflect.Slice, reflect.Ptr:
return h.pkgs(h.definitions.Types[typ.Elem], extended)
case reflect.Map:
pkgs := h.pkgs(h.definitions.Types[typ.Key], extended)
return append(pkgs, h.pkgs(h.definitions.Types[typ.Elem], extended)...)
}
if typ.PkgPath != "" {
index := strings.LastIndex(typ.PkgPath, "/vendor/")
if index >= 0 ||
!(typ.PkgPath == h.rootPkg || strings.HasPrefix(typ.PkgPath, h.rootPkg+"/")) {
pkg := typ.PkgPath
if index > 0 {
pkg = pkg[index+len("/vendor/"):]
}
return []string{pkg}
}
if extended && typ.Kind == reflect.Struct {
pkgs := []string{}
for _, field := range typ.Fields {
pkgs = append(pkgs, h.pkgs(h.definitions.Types[field.Type], extended)...)
}
return pkgs
}
}
return []string{}
}
// Functions returns functions which is required to generate.
func (h *helper) Functions() ([]function, []string) {
functionNames := map[string]int{}
functions := []function{}
types := []*api.Type{}
for path, defs := range h.definitions.Definitions {
for _, def := range defs {
fn := function{
Path: path,
Method: def.HTTPMethod,
Code: def.HTTPCode,
}
// The priority of summary is higher than original function name.
if def.Summary != "" {
// Remove invalid chars and regard as function name.
fn.Name = nameReplacer.ReplaceAllString(def.Summary, "")
}
if fn.Name == "" {
// If original function is public and there is no summary,
// original function name is selected.
name := strings.TrimSpace(h.namer.Name(def.Function))
if name != "" && name[0] >= 'A' && name[0] <= 'Z' {
fn.Name = name
}
}
if fn.Name == "" {
// Anonymous function.
fn.Name = "AnonymousAPI"
}
count := functionNames[fn.Name]
functionNames[fn.Name]++
if count > 0 {
fn.Name += strconv.Itoa(count)
}
comments := fmt.Sprintf("%s ", fn.Name)
if def.Description != "" {
comments += fmt.Sprintf("description:\n%s", def.Description)
} else {
comments += "does not have any description."
}
fn.Comments = api.ParseComments(comments).LineComments()
sigNames := h.namer.nameContainer()
// If there is no specified consumer, defaults to application/json.
firstNonEmptyConsume := definition.MIMEJSON
for _, consume := range def.Consumes {
if consume != "" {
firstNonEmptyConsume = consume
break
}
}
for _, param := range def.Parameters {
if param.Source == definition.Prefab {
// Ignore prefabs.
continue
}
p := functionParameter{
Source: string(param.Source),
Name: param.Name,
ProposedName: sigNames.proposeName(param.Name, param.Type),
Typ: h.namer.Name(param.Type),
}
types = append(types, h.definitions.Types[param.Type])
if param.Source == definition.Body {
// Use first consumer as the name of body parameter.
p.Name = firstNonEmptyConsume
}
if param.Source == definition.Auto {
// Generate field extensions for auto struct.
h.enumFields(param.Type, "",
func(key string, tag string, field api.StructField) {
source, name, _, err := service.ParseAutoParameterTag(tag)
if err != nil {
// Ignore invalid source tag.
return
}
extension := parameterExtension{
Source: string(source),
Name: name,
Key: key,
}
if source == definition.Body {
// Use first consumer as the name of body parameter.
extension.Name = firstNonEmptyConsume
}
p.Extensions = append(p.Extensions, extension)
})
}
fn.Parameters = append(fn.Parameters, p)
}
for _, result := range def.Results {
if result.Destination == definition.Error {
// Ignore errors
continue
}
r := functionResult{
Destination: string(result.Destination),
ProposedName: sigNames.proposeName("", result.Type),
Typ: h.namer.Name(result.Type),
}
typ := h.definitions.Types[result.Type]
types = append(types, typ)
if typ.Kind == reflect.Ptr {
r.Creator = fmt.Sprintf("new(%s)", h.namer.Name(typ.Elem))
}
fn.Results = append(fn.Results, r)
}
functions = append(functions, fn)
}
}
sort.Sort(functionSorter(functions))
return functions, h.packages(types, false)
}
func (h *helper) enumFields(name api.TypeName, key string, fn func(key string, source string, field api.StructField)) {
typ := h.definitions.Types[name]
if typ.Kind == reflect.Struct {
if key != "" {
key += "."
}
for _, field := range typ.Fields {
source := field.Tag.Get("source")
fieldKey := key + field.Name
if source != "" {
fn(fieldKey, source, field)
} else {
fieldType := h.definitions.Types[field.Type]
if fieldType.Kind == reflect.Struct {
h.enumFields(field.Type, key, fn)
}
}
}
}
}
type nameContainer struct {
names map[string]int
namer *typeNamer
}
var nameReplacer = regexp.MustCompile(`[^a-zA-Z0-9]`)
func (n *nameContainer) proposeName(name string, typ api.TypeName) string {
if name == "" {
name = n.deconstruct(typ)
}
name = nameReplacer.ReplaceAllString(name, "")
if name == "" {
name = "temp"
}
if name[0] >= 'A' && name[0] <= 'Z' {
name = string(name[0]|0x20) + name[1:]
}
index := n.names[name]
if index > 0 {
name += strconv.Itoa(index)
}
n.names[name]++
return name
}
func (n *nameContainer) deconstruct(name api.TypeName) string {
typ := n.namer.types[name]
switch typ.Kind {
case reflect.Ptr:
return n.deconstruct(typ.Elem)
case reflect.Array, reflect.Slice, reflect.Map:
result := n.deconstruct(typ.Elem)
// Unsafe to convert result to plural form.
switch {
case strings.HasSuffix(result, "es"):
case strings.HasSuffix(result, "y"):
result = result[:len(result)-1] + "ies"
case strings.HasSuffix(result, "s"):
result = result[:len(result)-1] + "es"
default:
result += "s"
}
return result
default:
return n.namer.Name(name)
}
}
type typeNamer struct {
types map[api.TypeName]*api.Type
names map[api.TypeName]string
aliases map[string]int
packages map[string]string
comments map[api.TypeName]string
rootPkg string
}
func newTypeNamer(rootPkg string, types map[api.TypeName]*api.Type) (*typeNamer, error) {
n := &typeNamer{
types: types,
names: make(map[api.TypeName]string),
aliases: make(map[string]int),
packages: make(map[string]string),
comments: make(map[api.TypeName]string),
rootPkg: rootPkg,
}
for tn := range n.types {
if _, err := n.parse(tn); err != nil {
return nil, err
}
}
return n, nil
}
func (n *typeNamer) nameContainer() *nameContainer {
return &nameContainer{
names: map[string]int{},
namer: n,
}
}
func (n *typeNamer) parse(tn api.TypeName) (string, error) {
name, ok := n.names[tn]
if ok {
return name, nil
}
typ, ok := n.types[tn]
if !ok {
return "", fmt.Errorf("no type with name %s", tn)
}
comments := ""
if typ.Kind == reflect.Interface ||
typ.PkgPath != "" && (strings.Contains(typ.PkgPath, "/vendor/") ||
!(typ.PkgPath == n.rootPkg || strings.HasPrefix(typ.PkgPath, n.rootPkg+"/"))) {
// Interfaces, Builtin types, Third-party types, Standard types are imported rather than copied.
alias := n.packages[typ.PkgPath]
if alias == "" && typ.PkgPath != "" {
alias = path.Base(typ.PkgPath)
index := n.aliases[alias]
n.aliases[alias]++
if index > 0 {
alias += strconv.Itoa(index)
}
n.packages[typ.PkgPath] = alias
}
name = typ.Name
if alias != "" {
name = fmt.Sprintf("%s.%s", alias, name)
}
} else {
switch typ.Kind {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.String:
if typ.PkgPath != "" {
name, comments = n.reconcileNameAndComments(typ.Name, typ.Comments)
} else {
name = typ.Kind.String()
}
case reflect.Array, reflect.Slice:
elemName, err := n.parse(typ.Elem)
if err != nil {
return "", err
}
if typ.PkgPath != "" {
name, comments = n.reconcileNameAndComments(typ.Name, typ.Comments)
} else {
name = fmt.Sprintf("[]%s", elemName)
}
case reflect.Ptr:
elemName, err := n.parse(typ.Elem)
if err != nil {
return "", err
}
if typ.PkgPath != "" {
name, comments = n.reconcileNameAndComments(typ.Name, typ.Comments)
} else {
name = fmt.Sprintf("*%s", elemName)
}
case reflect.Map:
keyName, err := n.parse(typ.Key)
if err != nil {
return "", err
}
elemName, err := n.parse(typ.Elem)
if err != nil {
return "", err
}
if typ.PkgPath != "" {
name, comments = n.reconcileNameAndComments(typ.Name, typ.Comments)
} else {
name = fmt.Sprintf("map[%s]%s", keyName, elemName)
}
case reflect.Struct, reflect.Func:
name, comments = n.reconcileNameAndComments(typ.Name, typ.Comments)
default:
return "", fmt.Errorf("can't generate a name for type %s", tn)
}
}
n.names[tn] = name
n.comments[tn] = comments
return name, nil
}
func (n *typeNamer) reconcileNameAndComments(origin, comments string) (string, string) {
c := api.ParseComments(comments)
aliases := c.Option(api.CommentsOptionAlias)
if len(aliases) > 0 {
alias := aliases[0]
if alias != "" && alias != origin {
c.Rename(origin, alias)
c.CleanOptions()
c.AddOption(api.CommentsOptionOrigin, origin)
origin = alias
}
}
return origin, c.LineComments()
}
func (n *typeNamer) Name(tn api.TypeName) string {
name, ok := n.names[tn]
if !ok {
panic(fmt.Errorf("can't find type %s", tn))
}
return name
}
func (n *typeNamer) Package(tn api.TypeName) (alias string, pkg string) {
typ, ok := n.types[tn]
if !ok {
panic(fmt.Errorf("can't find type %s", tn))
}
alias, ok = n.packages[typ.PkgPath]
if !ok {
return "", ""
}
return alias, typ.PkgPath
}
func (n *typeNamer) Alias(pkg string) string {
return n.packages[pkg]
}
func (n *typeNamer) Comments(tn api.TypeName) string {
comments, ok := n.comments[tn]
if !ok {
panic(fmt.Errorf("can't find type %s", tn))
}
return comments
}
type typeSorter []Type
// Len is the number of elements in the collection.
func (s typeSorter) Len() int {
return len(s)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (s typeSorter) Less(i, j int) bool {
return s[i].Name() < s[j].Name()
}
// Swap swaps the elements with indexes i and j.
func (s typeSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type functionSorter []function
// Len is the number of elements in the collection.
func (s functionSorter) Len() int {
return len(s)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (s functionSorter) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
// Swap swaps the elements with indexes i and j.
func (s functionSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}