-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
123 lines (99 loc) · 2.51 KB
/
parser.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
package autonats
import (
"go/ast"
"go/parser"
"go/token"
"path/filepath"
)
// Parser config
type ParserConfig struct {
BaseDir string // Directory containing interfaces to scan
DefaultTimeout int // Timeout for NATS requests
OutputFileName string // Output file name
DefaultConcurrency int // Default handler concurrency
Tracing bool // Generate tracing code
}
// Parser object
type Parser struct {
config *ParserConfig
services []*Service
rawPackages map[string]*ast.Package
packages map[string]*Package
}
func ParseDir(path string) (map[string]*ast.Package, error) {
fileSet := token.NewFileSet()
if !filepath.IsAbs(path) {
if baseDir, err := filepath.Abs(path); err != nil {
return nil, err
} else {
path = baseDir
}
}
return parser.ParseDir(fileSet, path, nil, 4)
}
// Creates a new parser with the provided config
func NewParser(config *ParserConfig) *Parser {
return &Parser{
config: config,
services: make([]*Service, 0),
rawPackages: make(map[string]*ast.Package),
packages: make(map[string]*Package),
}
}
func (par *Parser) ParseDir(path string) error {
packages, err := ParseDir(path)
if err != nil {
return err
}
par.AddRawPackages(packages)
return nil
}
func (par *Parser) AddRawPackages(packages map[string]*ast.Package) {
for k, v := range packages {
par.rawPackages[k] = v
}
}
// Runs the parser and outputs generated code to file
func (par *Parser) Run() {
packages := make(map[string]*Package)
services := make([]*Service, 0)
for _, v := range par.rawPackages {
services = append(services, ServicesFromPkg(v)...)
}
for _, service := range services {
pkg, ok := packages[service.FileName]
if !ok {
pkg = PackageFromService(service)
packages[service.FileName] = pkg
}
for _, m := range service.Methods {
if m.Timeout <= 0 {
m.Timeout = par.config.DefaultTimeout
}
if m.HandlerConcurrency <= 0 {
m.HandlerConcurrency = par.config.DefaultConcurrency
}
}
pkg.AddService(service)
}
par.services = services
par.packages = packages
}
func (par *Parser) Render() error {
imports := make([]string, 0)
for pk := range par.packages {
for k := range par.packages[pk].Imports {
imports = append(imports, k)
}
}
data := RenderData{
FileName: par.config.OutputFileName,
Path: par.config.BaseDir,
Services: par.services,
Imports: imports,
Timeout: par.config.DefaultTimeout,
JsonLib: "jsoniter",
Tracing: par.config.Tracing,
}
return Render(&data)
}