-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflow_gen_res.go
201 lines (175 loc) · 5.41 KB
/
flow_gen_res.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
package generator
import (
"go/ast"
"go/types"
"strings"
"github.com/GettEngineering/effe/fields"
"github.com/pkg/errors"
)
type implFieldInfo struct {
serviceFuncName *ast.Ident
originalFuncName *ast.Ident
input *ast.FieldList
output *ast.FieldList
deps *ast.FieldList
}
type flowGenRes struct {
implFuncDecls []*ast.FuncDecl
typeSpecs []*ast.TypeSpec
flowFuncDecl *ast.FuncDecl
depInitializerFuncDecl *ast.FuncDecl
imports []string
}
func (g Generator) genFlowFunc(funcName, interfaceName *ast.Ident, flowFunc *ast.FuncLit) (*ast.TypeSpec, *ast.FuncDecl) {
typeFunc := &ast.TypeSpec{
Name: ast.NewIdent(funcName.Name + g.settings.FlowFuncPostfix()),
Type: flowFunc.Type,
}
return typeFunc, newFlowFunc(g.settings.LocalInterfaceVarname(), interfaceName, funcName, typeFunc.Name, flowFunc)
}
func (g Generator) genImplField(impleName *ast.Ident, field implFieldInfo, deps []*ast.Field) (*ast.Field, *ast.FuncDecl, *ast.KeyValueExpr) {
structFieldIdent := ast.NewIdent(field.originalFuncName.Name + g.settings.ImplFieldPostfix())
structField := &ast.Field{
Names: []*ast.Ident{structFieldIdent},
Type: &ast.FuncType{
Params: field.input,
Results: field.output,
},
}
depArgs := []ast.Expr{}
for _, dep := range field.deps.List {
flowDep := fields.FindFieldWithType(deps, dep.Type)
depArgs = append(depArgs, flowDep.Names[0])
}
assignExpr := &ast.KeyValueExpr{
Key: structFieldIdent,
Value: &ast.CallExpr{
Fun: field.originalFuncName,
Args: depArgs,
},
}
callArgs := []ast.Expr{}
for _, input := range field.input.List {
callArgs = append(callArgs, input.Names[0])
}
impleNameIdent := ast.NewIdent(strings.ToLower(string([]rune(impleName.Name)[0])))
implFunc := &ast.FuncDecl{
Name: field.serviceFuncName,
Recv: &ast.FieldList{
List: []*ast.Field{
{
Names: []*ast.Ident{impleNameIdent},
Type: &ast.StarExpr{
X: impleName,
},
},
},
},
Type: &ast.FuncType{
Params: field.input,
Results: field.output,
},
Body: &ast.BlockStmt{
List: []ast.Stmt{},
},
}
if field.output != nil && len(field.output.List) > 0 {
implFunc.Body.List = append(implFunc.Body.List, &ast.ReturnStmt{
Results: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: impleNameIdent,
Sel: structFieldIdent,
},
Args: callArgs,
},
},
})
} else {
implFunc.Body.List = append(implFunc.Body.List, &ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: impleNameIdent,
Sel: structFieldIdent,
},
Args: callArgs,
},
})
}
return structField, implFunc, assignExpr
}
func (g Generator) genImplementation(impleName, newImpleFuncName *ast.Ident, f *flowGen) (*ast.TypeSpec, *ast.FuncDecl, []*ast.FuncDecl) {
funcDecls := make([]*ast.FuncDecl, 0)
structType := &ast.StructType{
Fields: &ast.FieldList{},
}
allDeps := f.getSortedFlowDependecies()
assignExprs := []ast.Expr{}
for _, field := range f.sortedImplFields() {
strField, implFunc, assignExp := g.genImplField(impleName, field, allDeps)
assignExprs = append(assignExprs, assignExp)
structType.Fields.List = append(structType.Fields.List, strField)
funcDecls = append(funcDecls, implFunc)
}
interfaceServiceFunc := genInitializeImplementFunc(newImpleFuncName, impleName, allDeps, assignExprs)
typeSpec := &ast.TypeSpec{
Name: impleName,
Type: structType,
}
return typeSpec, interfaceServiceFunc, funcDecls
}
func genInterface(interfaceName *ast.Ident, f *flowGen) *ast.TypeSpec {
inter := &ast.InterfaceType{
Methods: &ast.FieldList{},
}
for _, fieldInfo := range f.sortedImplFields() {
fn := &ast.FuncType{
Params: fieldInfo.input,
Results: fieldInfo.output,
}
inter.Methods.List = append(inter.Methods.List, &ast.Field{
Names: []*ast.Ident{fieldInfo.serviceFuncName},
Type: fn,
})
}
return &ast.TypeSpec{
Name: interfaceName,
Type: inter,
}
}
func (g Generator) genFlow(flowFunc *ast.FuncDecl, buildFlowFuncCall *ast.CallExpr, f *flowGen, typesInfo *types.Info) (*flowGenRes, error) {
flowComponents, failureComponent, err := g.loader.LoadFlow(buildFlowFuncCall.Args, f.pkgFuncDecls)
if err != nil {
return nil, err
}
fn, imports, err := g.strategy.BuildFlow(flowComponents, failureComponent, typesInfo)
if err != nil {
return nil, err
}
resFunc, ok := fn.(*ast.FuncLit)
if !ok {
return nil, errors.New("something goes wrong")
}
for _, flowComponent := range flowComponents {
f.genImplFields(flowComponent)
}
if failureComponent != nil {
f.genImplFields(failureComponent)
}
interfaceName := ast.NewIdent(flowFunc.Name.Name + g.settings.InterfaceNamePostfix())
implName := ast.NewIdent(flowFunc.Name.Name + g.settings.ImplPostfix())
newImplFuncName := ast.NewIdent(g.settings.NewImplFuncPrefix() + implName.Name)
flowDeclTypeSpec, flowDecl := g.genFlowFunc(flowFunc.Name, interfaceName, resFunc)
implTypeSpec, implInitializationFunc, implMethods := g.genImplementation(implName, newImplFuncName, f)
serviceInterfaceSpec := genInterface(interfaceName, f)
res := &flowGenRes{
imports: imports,
flowFuncDecl: flowDecl,
implFuncDecls: implMethods,
depInitializerFuncDecl: implInitializationFunc,
}
res.typeSpecs = append(res.typeSpecs, serviceInterfaceSpec)
res.typeSpecs = append(res.typeSpecs, implTypeSpec)
res.typeSpecs = append(res.typeSpecs, flowDeclTypeSpec)
return res, nil
}