/
compile.go
94 lines (81 loc) · 1.91 KB
/
compile.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
package typgen
import (
"go/ast"
"go/parser"
"go/token"
"reflect"
"strings"
)
func Compile(paths ...string) ([]*Annotation, error) {
var directives []*Annotation
fset := token.NewFileSet() // positions are relative to fset
for _, path := range paths {
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, err
}
file := &File{
Path: path,
Name: f.Name.Name,
Imports: CreateImports(f),
}
for _, decl := range f.Decls {
switch decl.(type) {
case *ast.FuncDecl:
declType := CreateFuncDecl(decl.(*ast.FuncDecl), file)
directives = appendDecl(directives, file, declType)
case *ast.GenDecl:
declTypes := createGenDecl(decl.(*ast.GenDecl), file)
for _, declType := range declTypes {
directives = appendDecl(directives, file, declType)
}
}
}
}
return directives, nil
}
func appendDecl(d []*Annotation, file *File, declType Type) []*Annotation {
decl := &Decl{
File: file,
Type: declType,
}
return append(d, retrieveAnnots(decl)...)
}
func retrieveAnnots(decl *Decl) []*Annotation {
var annots []*Annotation
for _, raw := range decl.GetDocs() {
if strings.HasPrefix(raw, "//") {
raw = strings.TrimSpace(raw[2:])
}
if strings.HasPrefix(raw, "@") {
name, params := ParseRawAnnot(raw)
annots = append(annots, &Annotation{
Name: name,
Params: reflect.StructTag(params),
Decl: decl,
})
}
}
return annots
}
// ParseRawAnnot parse raw string to annotation
func ParseRawAnnot(raw string) (name, params string) {
iOpen := strings.IndexRune(raw, '(')
iSpace := strings.IndexRune(raw, ' ')
if iOpen < 0 {
if iSpace < 0 {
return strings.TrimSpace(raw), ""
}
name = raw[:iSpace]
} else {
if iSpace < 0 {
name = raw[:iOpen]
} else {
name = raw[:iSpace]
}
if iClose := strings.IndexRune(raw, ')'); iClose > 0 {
params = raw[iOpen+1 : iClose]
}
}
return name, params
}