-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerator.go
354 lines (311 loc) · 8.87 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
package generator
import (
"context"
"fmt"
"go/ast"
goTypes "go/types"
"github.com/GettEngineering/effe/types"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
)
const (
// The function from Effe, which declares flow
BuildFLowExprType = "BuildFlow"
)
// Generator loads dsl, generates code or diagrams.
type Generator struct {
settings Settings
strategy Strategy
loader Loader
drawer Drawer
}
// Loader executes parsers for components by type. Generator gets arguments from
// expression BuildFlow and passes to Loader.
type Loader interface {
// Convert expressions to Components. Returns array of Components and a component with type Failure.
LoadFlow([]ast.Expr, map[string]*ast.FuncDecl) ([]types.Component, types.Component, error)
}
// Strategy generates the flow function.
type Strategy interface {
// BuildFlow takes a list of components and a failure component and returns
// the flow function and an array of imports.sss
BuildFlow([]types.Component, types.Component, *goTypes.Info) (ast.Expr, []string, error)
}
// Drawe draws graphs for business flows.
type Drawer interface {
// DrawBuild takes a list of compoments and a failure components, returns
// string in plantuml dsl
DrawFlow([]types.Component, types.Component) (string, error)
}
type Option func(g *Generator)
// WithSetttings is used for overriding settings
// Default is settings from a method DefaultSettings()
func WithSetttings(s Settings) Option {
return func(g *Generator) {
g.settings = s
}
}
// WithDrawer is used for overriding a drawer
func WithDrawer(d Drawer) Option {
return func(g *Generator) {
g.drawer = d
}
}
// WithDrawer is used for overriding a loader
func WithLoader(l Loader) Option {
return func(g *Generator) {
g.loader = l
}
}
// WithDrawer is used for overriding a strategy
func WithStrategy(s Strategy) Option {
return func(g *Generator) {
g.strategy = s
}
}
// Initialize a new generator with options
func NewGenerator(opts ...Option) *Generator {
g := &Generator{}
for _, opt := range opts {
opt(g)
}
return g
}
// This method generates diagrams for a package
func (g *Generator) GenerateDiagram(ctx context.Context, wd string, env []string, patterns []string, outputDir string) ([]types.GenerateResult, []error) {
pkgs, errs := load(ctx, wd, env, patterns)
if len(errs) > 0 {
return nil, errs
}
generated := make([]types.GenerateResult, 0)
for _, pkg := range pkgs {
genRes := types.GenerateResult{
PkgPath: pkg.PkgPath,
}
res, errs := g.generateDiagramForPkg(pkg)
if errs != nil {
genRes.Errs = append(genRes.Errs, errs...)
generated = append(generated, genRes)
continue
}
outputFlows, err := writeDiagrams(pkg, outputDir, res)
if err != nil {
genRes.Errs = append(genRes.Errs, errs...)
generated = append(generated, genRes)
continue
}
for _, output := range outputFlows {
generated = append(generated, types.GenerateResult{
PkgPath: pkg.PkgPath,
OutputPath: output,
})
}
}
return generated, nil
}
type drawFlowRes struct {
name string
graph string
}
func (g *Generator) generateDiagramForPkg(pkg *packages.Package) ([]drawFlowRes, []error) {
pkgFuncDecls, flowDecls := g.loadFuncsAndFlows(pkg)
analyzer := newAnayzer(flowDecls)
sortedFlowDecls, errs := analyzer.sortFlowDeclsByDependecies()
if len(errs) > 0 {
return nil, errs
}
flows := make([]drawFlowRes, 0)
for _, flowDecl := range sortedFlowDecls {
flowComponents, failureComponent, err := g.loader.LoadFlow(flowDecl.buildFlowFuncCall.Args, pkgFuncDecls)
if err != nil {
errs = append(errs, errors.Errorf("can't load flow: %s, error: %s", flowDecl.flowFunc.Name, err))
continue
}
var flowGraph string
flowGraph, err = g.drawer.DrawFlow(flowComponents, failureComponent)
if err != nil {
errs = append(errs, errors.Errorf("can't load flow: %s, error: %s", flowDecl.flowFunc.Name, err))
continue
}
flows = append(flows, drawFlowRes{
name: flowDecl.flowFunc.Name.Name,
graph: flowGraph,
})
pkgFuncDecls[flowDecl.flowFunc.Name.Name] = generateEmptyFlowFuncAsStepDeclaration(flowDecl.flowFunc)
}
return flows, errs
}
func generateEmptyFlowFuncAsStepDeclaration(flowFuncDecl *ast.FuncDecl) *ast.FuncDecl {
return &ast.FuncDecl{
Name: flowFuncDecl.Name,
Type: &ast.FuncType{
Params: flowFuncDecl.Type.Params,
Results: &ast.FieldList{
List: []*ast.Field{
{
Type: &ast.FuncType{
Params: flowFuncDecl.Type.Params,
Results: flowFuncDecl.Type.Results,
},
},
},
},
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ReturnStmt{
Results: []ast.Expr{
&ast.FuncLit{
Type: &ast.FuncType{
Params: flowFuncDecl.Type.Params,
Results: flowFuncDecl.Type.Results,
},
Body: &ast.BlockStmt{},
},
},
},
},
},
}
}
// This method generates code for a directory and environments.
func (g *Generator) Generate(ctx context.Context, wd string, env []string, patterns []string) ([]types.GenerateResult, []error) {
pkgs, errs := load(ctx, wd, env, patterns)
if len(errs) > 0 {
return nil, errs
}
generated := make([]types.GenerateResult, len(pkgs))
for i, pkg := range pkgs {
generated[i].PkgPath = pkg.PkgPath
p, errs := g.generateForPackage(pkg)
if errs != nil {
generated[i].Errs = append(generated[i].Errs, errs...)
continue
}
outputFileName, err := writeGeneratedCode(pkg, p)
if err != nil {
generated[i].Errs = append(generated[i].Errs, err)
continue
}
generated[i].OutputPath = outputFileName
}
return generated, nil
}
type flowDecl struct {
flowFunc *ast.FuncDecl
buildFlowFuncCall *ast.CallExpr
}
func (f flowDecl) FlowName() string {
return f.flowFunc.Name.Name
}
type pkgGen struct {
flowFuncDecls []*ast.FuncDecl
depInitializerFuncDecls []*ast.FuncDecl
implFuncDecls []*ast.FuncDecl
typeSpecs []*ast.TypeSpec
imports []string
}
func (g *Generator) loadFuncsAndFlows(pkg *packages.Package) (map[string]*ast.FuncDecl, []flowDecl) {
pkgFuncDecls := make(map[string]*ast.FuncDecl)
flowDecls := []flowDecl{}
for _, f := range pkg.Syntax {
for _, decl := range f.Decls {
fn, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
if fn.Body == nil {
continue
}
pkgFuncDecls[fn.Name.String()] = fn
buildFlowFuncCall := findExprInBody(fn, pkg, BuildFLowExprType)
if buildFlowFuncCall == nil {
continue
}
flowDecls = append(flowDecls, flowDecl{
flowFunc: fn,
buildFlowFuncCall: buildFlowFuncCall,
})
}
}
return pkgFuncDecls, flowDecls
}
func (g *Generator) generateForPackage(pkg *packages.Package) (*pkgGen, []error) {
pkgFuncDecls, flowDecls := g.loadFuncsAndFlows(pkg)
analyzer := newAnayzer(flowDecls)
sortedFlowDecls, errs := analyzer.sortFlowDeclsByDependecies()
if len(errs) > 0 {
return nil, errs
}
importSet := make(map[string]struct{})
p := &pkgGen{}
for _, flowDecl := range sortedFlowDecls {
f := &flowGen{
pkgFuncDecls: pkgFuncDecls,
implFields: make(map[string]implFieldInfo),
}
res, err := g.genFlow(flowDecl.flowFunc, flowDecl.buildFlowFuncCall, f, pkg.TypesInfo)
if err != nil {
loadErr, ok := err.(*types.LoadError)
if ok {
position := pkg.Fset.Position(loadErr.Pos)
err = fmt.Errorf("%s %s", loadErr.Err, position.String())
}
errs = append(errs, err)
continue
}
//Import types, which are used in flow
for _, fieldInfo := range f.implFields {
if fieldInfo.input != nil {
mergeImportSets(importSet, getExportedType(pkg, fieldInfo.input))
}
if fieldInfo.output != nil {
mergeImportSets(importSet, getExportedType(pkg, fieldInfo.output))
}
}
for _, impr := range res.imports {
importSet[impr] = struct{}{}
}
p.depInitializerFuncDecls = append(p.depInitializerFuncDecls, res.depInitializerFuncDecl)
p.flowFuncDecls = append(p.flowFuncDecls, res.flowFuncDecl)
p.implFuncDecls = append(p.implFuncDecls, res.implFuncDecls...)
p.typeSpecs = append(p.typeSpecs, res.typeSpecs...)
pkgFuncDecls[flowDecl.FlowName()] = res.flowFuncDecl
continue
}
if len(errs) > 0 {
return nil, errs
}
for k := range importSet {
p.imports = append(p.imports, k)
}
return p, nil
}
func load(ctx context.Context, wd string, env []string, patterns []string) ([]*packages.Package, []error) {
cfg := &packages.Config{
Context: ctx,
Mode: packages.LoadAllSyntax, //nolint:staticcheck
Dir: wd,
Env: env,
BuildFlags: []string{"-tags=effeinject"},
// TODO(light): Use ParseFile to skip function bodies and comments in indirect packages.
}
escaped := make([]string, len(patterns))
for i := range patterns {
escaped[i] = "pattern=" + patterns[i]
}
pkgs, err := packages.Load(cfg, escaped...)
if err != nil {
return nil, []error{err}
}
var errs []error
for _, p := range pkgs {
for _, e := range p.Errors {
errs = append(errs, e)
}
}
if len(errs) > 0 {
return nil, errs
}
return pkgs, nil
}