-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblockcontext.go
156 lines (137 loc) · 3.51 KB
/
blockcontext.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
package strategies
import (
"go/ast"
"github.com/GettEngineering/effe/fields"
)
type VarBuilder func(t ast.Expr) *ast.Ident
func NewBlockContext() *BlockContext {
return &BlockContext{
Input: new(ast.FieldList),
Output: new(ast.FieldList),
Vars: make(map[string]*ast.Ident),
}
}
type BlockContext struct {
Input *ast.FieldList
Output *ast.FieldList
Vars map[string]*ast.Ident
Builder VarBuilder
}
//nolint:gocognit
func (b *BlockContext) CalculateInput(calls []ComponentCall) {
for index, c := range calls {
if c.Input() == nil {
continue
}
for _, inputField := range c.Input().List {
var foundSourceOfArg bool
for _, previous := range calls[:index] {
if previous.Output() != nil && fields.FindFieldWithType(previous.Output().List, inputField.Type) != nil {
foundSourceOfArg = true
break
}
}
if !foundSourceOfArg {
existsInInput := fields.FindFieldWithType(b.Input.List, inputField.Type)
ident, _ := b.genVariable(inputField.Type)
if existsInInput == nil {
b.addInput(ident, inputField.Type)
} else {
existsInInput.Names = []*ast.Ident{ident}
}
}
}
}
}
func (b *BlockContext) OutputList() []*ast.Field {
return b.Output.List
}
//nolint:gocognit
func (b *BlockContext) CalculateOutput(calls []ComponentCall) {
for index, c := range calls {
if c.Output() == nil {
continue
}
for _, outputField := range c.Output().List {
var foundUsageOfOutput bool
for _, next := range calls[index+1:] {
if next.Input() == nil {
continue
}
if next.Input() != nil && fields.FindFieldWithType(next.Input().List, outputField.Type) != nil {
foundUsageOfOutput = true
break
}
}
if !foundUsageOfOutput && fields.FindFieldWithType(b.Output.List, outputField.Type) == nil {
b.addOutput(outputField.Type)
}
}
}
}
func (b *BlockContext) addInput(name *ast.Ident, t ast.Expr) {
b.Input.List = append(b.Input.List, &ast.Field{
Names: []*ast.Ident{name},
Type: t,
})
}
func (b *BlockContext) addOutput(t ast.Expr) {
b.Output.List = append(b.Output.List, &ast.Field{
Type: t,
})
}
func (b BlockContext) FindInputByType(t ast.Expr) *ast.Field {
return fields.FindFieldWithType(b.Input.List, t)
}
func (b *BlockContext) AddInput(t ast.Expr) *ast.Field {
f := b.FindInputByType(t)
if f != nil {
return f
}
v := b.Builder(t)
field := &ast.Field{
Names: []*ast.Ident{v},
Type: t,
}
b.Input.List = append(b.Input.List, field)
b.Vars[fields.GetTypeStrName(t)] = v
return field
}
func (b *BlockContext) genVariable(t ast.Expr) (*ast.Ident, bool) {
typeStr := fields.GetTypeStrName(t)
v, ok := b.Vars[typeStr]
if !ok {
v = b.Builder(t)
b.Vars[typeStr] = v
}
return v, ok
}
func (b *BlockContext) BuildInputVars(input *ast.FieldList) []*ast.Field {
args := make([]*ast.Field, len(input.List))
for index, inputField := range input.List {
v, ok := b.genVariable(inputField.Type)
if !ok && fields.FindFieldWithType(b.Input.List, inputField.Type) == nil {
b.addInput(v, inputField.Type)
}
args[index] = &ast.Field{
Type: inputField.Type,
Names: []*ast.Ident{v},
}
}
return args
}
func (b *BlockContext) BuildOutputVars(output *ast.FieldList) ([]*ast.Field, bool) {
vars := make([]*ast.Field, len(output.List))
allOutputVarsExist := true
for index, outputField := range output.List {
v, ok := b.genVariable(outputField.Type)
vars[index] = &ast.Field{
Type: outputField.Type,
Names: []*ast.Ident{v},
}
if !ok {
allOutputVarsExist = false
}
}
return vars, allOutputVarsExist
}