-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (150 loc) · 4.17 KB
/
main.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
package template
import (
"path"
"github.com/angrypie/tie/parser"
"github.com/angrypie/tie/template/modutils"
"github.com/angrypie/tie/types"
. "github.com/dave/jennifer/jen"
)
type Package = modutils.Package
type Module = modutils.Module
func GetMainPackage(packagePath string, modules []string) *Package {
f := NewFile("main")
f.Func().Id("main").Params().BlockFunc(func(g *Group) {
for _, module := range modules {
importPath := path.Join(packagePath, "tie_modules", module)
g.Qual(importPath, "Main").Call()
}
makeWaitGuard(g)
})
return modutils.NewPackage("main", "main.go", f.GoString())
}
func NewMainModule(p *parser.Parser, deps []Module) Module {
var modules []string
for _, dep := range deps {
modules = append(modules, dep.Name())
}
generator := func(p *parser.Parser) *Package {
return GetMainPackage(p.Package.Name, modules)
}
return modutils.NewStandartModule("tie_modules", generator, p, deps)
}
type PackageInfo struct {
Functions []parser.Function
Constructors map[string]Constructor
PackageName string
IsInitService bool
IsStopService bool
Service *types.Service
//ServicePath should refer to modified original package.
servicePath string
ModulePath string
}
func (info PackageInfo) GetServicePath() string {
if info.servicePath == "" {
return info.ModulePath
}
return info.servicePath
}
func (info *PackageInfo) SetServicePath(path string) {
info.servicePath = path
}
//TODO check receiver taht does not have constructors
func (info PackageInfo) IsReceiverType(field types.Field) bool {
_, ok := info.GetConstructor(field)
return ok
}
func (info PackageInfo) GetConstructor(field types.Field) (constructor Constructor, ok bool) {
_, path, local := field.TypeParts()
constructor, ok = info.Constructors[path+"."+local]
return
}
//check if receiver receiver have function with specified name
func (info PackageInfo) GetFunction(receiver types.Field, functionName string) (function parser.Function, ok bool) {
receiverType := receiver.TypeName()
for _, fn := range info.Functions {
//If receiver type have Stop method add it to special stoppableServices list
if HasReceiver(fn) && fn.Receiver.TypeName() == receiverType && fn.Name == "Stop" {
return fn, true
}
}
return
}
func NewPackageInfoFromParser(p *parser.Parser) *PackageInfo {
functions := p.GetFunctions()
var fns []parser.Function
for _, fn := range functions {
if name := fn.Name; name == "InitService" || name == "StopService" {
continue
}
fns = append(fns, fn)
}
info := PackageInfo{
Functions: fns,
Service: p.Service,
Constructors: make(map[string]Constructor),
PackageName: p.GetPackageName(),
ModulePath: p.Package.Name,
}
for _, fn := range functions {
if fn.Name == "InitService" {
info.IsInitService = true
}
if fn.Name == "StopService" {
info.IsStopService = true
}
receiver, ok := isConventionalConstructor(fn)
if ok {
_, path, local := receiver.TypeParts()
info.Constructors[path+"."+local] = *NewTypeConstructor(fn, receiver)
}
}
return &info
}
func createErrLog(msg string) *Statement {
return Qual("log", "Printf").Call(List(Lit("ERR %s: %s"), Lit(msg), Err()))
}
type Constructor struct {
Function parser.Function
Receiver parser.Field
}
func NewTypeConstructor(fn parser.Function, rec parser.Field) (constructor *Constructor) {
return &Constructor{
Function: fn,
Receiver: rec,
}
}
type OptionalConstructor = func(func(Constructor), ...func())
func NewOptionalConstructor(constructors ...Constructor) OptionalConstructor {
return func(constructor func(Constructor), empty ...func()) {
if len(constructors) > 0 {
constructor(constructors[0])
} else {
if len(empty) > 0 {
empty[0]()
}
}
}
}
type Field struct {
name string
typeName string
}
func NewField(name, typeName string) Field {
return Field{name, typeName}
}
func (field Field) Name() string {
return field.name
}
func (field Field) TypeName() string {
return field.typeName
}
func (field Field) TypeParts() (prefix, path, local string) {
return "", "", field.typeName
}
func fieldsFromParser(fields []parser.Field) (res []types.Field) {
for _, arg := range fields {
res = append(res, arg)
}
return
}