/
testcustomization.go
156 lines (140 loc) · 3.08 KB
/
testcustomization.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 testcustomization
import (
"fmt"
"go/ast"
"go/token"
"github.com/GettEngineering/effe/loaders"
"github.com/GettEngineering/effe/strategies"
"github.com/GettEngineering/effe/types"
"github.com/pkg/errors"
)
//go:generate go run ./cmd/updater/main.go -- ./testdata
func POST(args interface{}) interface{} {
panic("implementation is not generated, run myeffe")
}
type PostRequestComponent struct {
URI ast.Expr
}
func (p PostRequestComponent) Name() *ast.Ident {
return ast.NewIdent(fmt.Sprintf("POST request to %s", p.URI))
}
type postRequestComponentCall struct {
fn ast.Expr
input *ast.FieldList
output *ast.FieldList
name *ast.Ident
}
func (c postRequestComponentCall) Name() *ast.Ident {
return c.name
}
func (c postRequestComponentCall) Input() *ast.FieldList {
return c.input
}
func (c postRequestComponentCall) Output() *ast.FieldList {
return c.output
}
func (c postRequestComponentCall) Fn() ast.Expr {
return c.fn
}
func LoadPostRequestComponent(effeConditionCall *ast.CallExpr, f loaders.FlowLoader) (types.Component, error) {
return &PostRequestComponent{
URI: effeConditionCall.Args[0],
}, nil
}
func GenPostRequestComponent(f strategies.FlowGen, c types.Component) (strategies.ComponentCall, error) {
component, ok := c.(*PostRequestComponent)
if !ok {
return nil, errors.Errorf("component %s is not a component with type PostRequestComponent", component.Name())
}
f.AddImport("gopkg.in/h2non/gentleman.v2")
cli := ast.NewIdent("cli")
req := ast.NewIdent("req")
output := &ast.FieldList{
List: []*ast.Field{
{
Type: &ast.StarExpr{
X: &ast.SelectorExpr{
X: ast.NewIdent("gentleman"),
Sel: ast.NewIdent("Response"),
},
},
},
{
Type: ast.NewIdent("error"),
},
},
}
fn := &ast.FuncLit{
Type: &ast.FuncType{
Params: &ast.FieldList{},
Results: output,
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.AssignStmt{
Lhs: []ast.Expr{cli},
Rhs: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("gentleman"),
Sel: ast.NewIdent("New"),
},
},
},
Tok: token.DEFINE,
},
&ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: cli,
Sel: ast.NewIdent("URI"),
},
Args: []ast.Expr{
component.URI,
},
},
},
&ast.AssignStmt{
Lhs: []ast.Expr{
req,
},
Rhs: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: cli,
Sel: ast.NewIdent("Request"),
},
},
},
Tok: token.DEFINE,
},
&ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: req,
Sel: ast.NewIdent("Method"),
},
Args: []ast.Expr{
ast.NewIdent("POST"),
},
},
},
&ast.ReturnStmt{
Results: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: cli,
Sel: ast.NewIdent("Send"),
},
},
},
},
},
},
}
return &postRequestComponentCall{
input: &ast.FieldList{},
output: output,
fn: fn,
}, nil
}