-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflow_gen.go
122 lines (110 loc) · 2.6 KB
/
flow_gen.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
package generator
import (
"go/ast"
"sort"
"strconv"
"strings"
"github.com/GettEngineering/effe/fields"
"github.com/GettEngineering/effe/types"
)
type flowGen struct {
pkgFuncDecls map[string]*ast.FuncDecl
implFields map[string]implFieldInfo
}
func (f *flowGen) genImplFields(c types.Component) {
switch c := c.(type) {
case *types.SimpleComponent:
f.genImplField(c)
case *types.WrapComponent:
if c.Before != nil {
f.genImplField(c.Before)
}
if c.Success != nil {
f.genImplField(c.Success)
}
if c.Failure != nil {
f.genImplField(c.Failure)
}
for _, child := range c.Children {
f.genImplFields(child)
}
case *types.DecisionComponent:
if c.Failure != nil {
f.genImplFields(c.Failure)
}
for _, decisionCase := range c.Cases {
f.genImplFields(decisionCase)
}
case *types.CaseComponent:
for _, child := range c.Children {
f.genImplFields(child)
}
}
}
func (f *flowGen) genImplField(simple *types.SimpleComponent) {
_, ok := f.implFields[simple.FuncName.Name]
if ok {
return
}
f.implFields[simple.FuncName.Name] = implFieldInfo{
input: simple.Input,
output: simple.Output,
serviceFuncName: simple.FuncName,
originalFuncName: simple.OriginalFuncName,
deps: simple.Deps,
}
}
func (f *flowGen) sortedImplFields() []implFieldInfo {
implFields := []implFieldInfo{}
for _, field := range f.implFields {
implFields = append(implFields, field)
}
sort.SliceStable(implFields, func(i, j int) bool {
return implFields[i].serviceFuncName.Name <= implFields[j].serviceFuncName.Name
})
return implFields
}
func (f flowGen) getSortedFlowDependecies() []*ast.Field {
depsSet := map[string]*ast.Field{}
for _, fieldInfo := range f.implFields {
for _, dep := range fieldInfo.deps.List {
strTypeName := fields.GetTypeStrName(dep.Type)
_, ok := depsSet[strTypeName]
if ok {
continue
}
depsSet[strTypeName] = &ast.Field{
Type: dep.Type,
Names: []*ast.Ident{
{
Name: dep.Names[0].Name,
},
},
}
}
}
allDeps := []*ast.Field{}
for _, dep := range depsSet {
allDeps = append(allDeps, dep)
}
sort.SliceStable(allDeps, func(i, j int) bool {
return fields.GetTypeStrName(allDeps[i].Type) < fields.GetTypeStrName(allDeps[j].Type)
})
for i, dep := range allDeps {
depCounter := 0
for j := 0; j < len(allDeps); j++ {
if i == j {
continue
}
if dep.Names[0].Name != allDeps[j].Names[0].Name {
continue
}
depCounter++
allDeps[j].Names[0].Name = strings.Join([]string{
allDeps[j].Names[0].Name,
strconv.Itoa(depCounter),
}, "")
}
}
return allDeps
}