-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflowgen.go
206 lines (174 loc) · 4.87 KB
/
flowgen.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
package strategies
import (
"fmt"
"go/ast"
"go/token"
goTypes "go/types"
"reflect"
"strconv"
"strings"
"github.com/GettEngineering/effe/fields"
"github.com/GettEngineering/effe/plugin"
"github.com/GettEngineering/effe/types"
)
const (
errorExpr = "error"
fmtLibrary = "fmt"
)
type FlowGen interface {
AddImport(string)
BuildComponentStmt(ctx *BlockContext, cCall, failureCall ComponentCall) ComponentStmt
ApplyPlugins(ctx *BlockContext, componentStmt ComponentStmt) *ast.BlockStmt
ServiceName() string
VarBuilder() VarBuilder
GenComponentCall(types.Component) (ComponentCall, error)
TypesInfo() *goTypes.Info
}
func (f flowGen) ServiceName() string {
return f.serviceObjectName
}
type flowGen struct {
globalVarNamesCounter map[string]int
plugins []plugin.Plugin
importSet map[string]struct{}
serviceObjectName string
chain *chain
typesInfo *goTypes.Info
}
func (f flowGen) TypesInfo() *goTypes.Info {
return f.typesInfo
}
func (f *flowGen) GenComponentCall(component types.Component) (ComponentCall, error) {
cType := reflect.TypeOf(component).String()
dotIndex := strings.Index(cType, ".")
if dotIndex != -1 {
cType = string([]byte(cType)[dotIndex+1:])
}
return f.chain.generators[cType](f, component)
}
func (f *flowGen) AddImport(impr string) {
f.importSet[impr] = struct{}{}
}
func (f *flowGen) VarBuilder() VarBuilder {
return f.buildVar
}
func (f *flowGen) buildVar(t ast.Expr) *ast.Ident {
v := fields.NewIdentWithType(t)
index := f.incrementGlovalVarNameCounter(v.Name)
if index > 1 {
v = ast.NewIdent(v.Name + strconv.Itoa(index))
}
return v
}
func (f *flowGen) incrementGlovalVarNameCounter(name string) int {
f.globalVarNamesCounter[name]++
return f.globalVarNamesCounter[name]
}
func (f *flowGen) buildFailureBlock(ctx *BlockContext, failureCall ComponentCall, output *ast.FieldList, cName *ast.Ident) *ast.BlockStmt {
errVars := make([]string, 0)
block := &ast.BlockStmt{}
if output == nil {
return nil
}
for _, output := range output.List {
if fields.GetTypeStrName(output.Type) == errorExpr {
v, ok := ctx.Vars[errorExpr]
if ok {
errVars = append(errVars, v.Name)
}
}
}
if len(errVars) == 0 {
return nil
}
for _, errVarName := range errVars {
ifBody := &ast.BlockStmt{}
if failureCall != nil {
failureStmt := f.BuildComponentStmt(ctx, failureCall, nil)
ifBody.List = append(ifBody.List, failureStmt.Stmt())
}
failMsg := fmt.Sprintf("failure call %s", cName)
returnStmt, usedFmtLibrary := BuildFailureReturnStmt(ctx.Output, ctx.Vars, failMsg, f.typesInfo)
if usedFmtLibrary {
f.AddImport(fmtLibrary)
}
ifBody.List = append(ifBody.List, returnStmt)
ifStmt := &ast.IfStmt{
Cond: &ast.BinaryExpr{
X: ast.NewIdent(errVarName),
Op: token.NEQ,
Y: ast.NewIdent("nil"),
},
Body: ifBody,
}
block.List = append(block.List, ifStmt)
}
return block
}
func (f *flowGen) BuildComponentStmt(ctx *BlockContext, cCall, failureCall ComponentCall) ComponentStmt {
var name string
if cCall.Name() != nil {
name = cCall.Name().Name
} else {
name = "call func"
}
componentStmt := &componentStmt{
componentName: name,
}
componentStmt.inputFields = ctx.BuildInputVars(cCall.Input())
call := &ast.CallExpr{
Fun: cCall.Fn(),
Args: getNamesFromFieldList(componentStmt.inputFields),
}
if cCall.Output() != nil && len(cCall.Output().List) > 0 {
outputFields, allVarsExist := ctx.BuildOutputVars(cCall.Output())
assignStmt := &ast.AssignStmt{
Rhs: []ast.Expr{
call,
},
Lhs: getNamesFromFieldList(outputFields),
}
if allVarsExist {
assignStmt.Tok = token.ASSIGN
} else {
assignStmt.Tok = token.DEFINE
}
componentStmt.outputFields = outputFields
componentStmt.stmt = assignStmt
} else {
componentStmt.stmt = &ast.ExprStmt{
X: call,
}
}
componentStmt.failureBlock = f.buildFailureBlock(ctx, failureCall, cCall.Output(), cCall.Name())
return componentStmt
}
func (f *flowGen) ApplyPlugins(ctx *BlockContext, componentStmt ComponentStmt) *ast.BlockStmt {
usedPlugins := make(map[plugin.Plugin]struct{})
block := &ast.BlockStmt{}
for _, p := range f.plugins {
beforeStmts := p.Before(ctx, componentStmt.Name(), componentStmt.InputFields())
block.List = append(block.List, beforeStmts...)
pluginUsed := p.Change(ctx, componentStmt)
if len(beforeStmts) > 0 || pluginUsed {
usedPlugins[p] = struct{}{}
}
}
block.List = append(block.List, componentStmt.Stmt())
if componentStmt.ErrStmt() != nil {
block.List = append(block.List, componentStmt.ErrStmt().List...)
}
for _, p := range f.plugins {
afterStmts := p.Success(ctx, componentStmt.Name(), componentStmt.OutputFields())
if len(afterStmts) > 0 {
usedPlugins[p] = struct{}{}
}
block.List = append(block.List, afterStmts...)
}
for p := range usedPlugins {
for _, impr := range p.Imports() {
f.AddImport(impr)
}
}
return block
}