-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
220 lines (185 loc) · 5.37 KB
/
tree.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"fmt"
"net/http"
"strings"
"github.com/dave/jennifer/jen"
"github.com/rs/zerolog/log"
)
type SyntaxNode interface {
render(childCodes *[]jen.Code, isFirst, isLast bool) []jen.Code
children() map[string]SyntaxNode
addChild(child SyntaxNode)
value() string
}
var (
root = &Root{
Value: "",
Children: make(map[string]SyntaxNode),
}
mockServerPort = 8080
externalAPIToMockServerMap = make(map[string]int)
)
type Node struct {
Value string
Children map[string]SyntaxNode
}
type Root Node
type Host Node
type Path Node
type Method Node
type QueryParameter Node
type ReqBody Node
type RespBody struct {
Header http.Header
StatusCode int
Value string
Children map[string]SyntaxNode
}
func (h *Root) render(childCodes *[]jen.Code, isFirst, isLast bool) []jen.Code {
var codes []jen.Code
codes = append(codes, *childCodes...)
codes = append(codes, generateSignalHandler()...)
return []jen.Code{
jen.Func().Id("main").Params().Block(
codes...,
),
}
}
func (h *Root) children() map[string]SyntaxNode {
return h.Children
}
func (h *Root) addChild(child SyntaxNode) {
h.Children[child.value()] = child
}
func (h *Root) value() string {
return h.Value
}
func (h *Host) render(childCodes *[]jen.Code, isFirst, isLast bool) []jen.Code {
var codes []jen.Code
codes = append(codes, jen.Id("mux").Op(":=").Qual("net/http", "NewServeMux").Call())
codes = append(codes, *childCodes...)
codes = append(codes,
jen.Id("port").Op(":=").Lit(mockServerPort),
jen.Id("server").Op(":=").Qual("net/http", "Server").Values(jen.Dict{
jen.Id("Addr"): jen.Lit("0.0.0.0:").Op("+").Qual("fmt", "Sprint").Call(jen.Id("port")),
jen.Id("Handler"): jen.Id("enableLogRequest").Call(jen.Id("mux"), jen.Id("port")),
}),
jen.Qual("fmt", "Printf").Call(jen.Lit("Listening on %v\n"), jen.Id("server").Dot("Addr")),
)
if strings.HasPrefix(h.value(), "https") {
codes = append(codes, jen.Go().Id("server").Dot("ListenAndServeTLS").Call(jen.Lit("cert.pem"), jen.Lit("key.pem")))
} else {
codes = append(codes, jen.Go().Id("server").Dot("ListenAndServe").Call())
}
externalAPIToMockServerMap[h.value()] = mockServerPort
mockServerPort++
return []jen.Code{
jen.Func().Params().Block(
codes...,
).Call(),
}
}
func (h *Host) children() map[string]SyntaxNode {
return h.Children
}
func (h *Host) addChild(child SyntaxNode) {
h.Children[child.value()] = child
}
func (h *Host) value() string {
return h.Value
}
func (h *ReqBody) render(childCodes *[]jen.Code, isFirst, isLast bool) []jen.Code {
return []jen.Code{
jen.If(jen.Id("body").Op("==").Lit(h.value())).Block(*childCodes...),
}
}
func (h *ReqBody) children() map[string]SyntaxNode {
return h.Children
}
func (h *ReqBody) addChild(child SyntaxNode) {
h.Children[child.value()] = child
}
func (h *ReqBody) value() string {
return h.Value
}
func (r *RespBody) render(childCodes *[]jen.Code, isFirst, isLast bool) []jen.Code {
var codes []jen.Code
for k, vv := range r.Header {
for _, v := range vv {
codes = append(codes, jen.Id("rw").Dot("Header").Call().Dot("Set").Call(jen.Lit(k), jen.Lit(v)))
}
}
return append(codes, []jen.Code{
jen.Id("rw").Dot("WriteHeader").Call(jen.Lit(r.StatusCode)),
jen.Qual("fmt", "Fprint").Call(jen.Id("rw"), jen.Lit(r.Value)),
jen.Return(),
}...)
}
func (h *RespBody) children() map[string]SyntaxNode {
return h.Children
}
func (h *RespBody) addChild(child SyntaxNode) {
log.Error().Msg("not supported")
}
func (h *RespBody) value() string {
return createResponseKey(h)
}
func (h *QueryParameter) render(childCodes *[]jen.Code, isFirst, isLast bool) []jen.Code {
return []jen.Code{
jen.If(jen.List(jen.Id("q"), jen.Id("_")).Op(":=").Id("stringifyUrlValues").Call(jen.Id("r").Dot("URL").Dot("Query").Call()), jen.Id("q").Op("==").Lit(h.value())).Block(
*childCodes...,
),
}
}
func (h *QueryParameter) children() map[string]SyntaxNode {
return h.Children
}
func (h *QueryParameter) addChild(child SyntaxNode) {
h.Children[child.value()] = child
}
func (h *QueryParameter) value() string {
return h.Value
}
func (h *Method) render(childCodes *[]jen.Code, isFirst, isLast bool) []jen.Code {
var codes []jen.Code
if isFirst {
codes = append(codes,
jen.List(jen.Id("body"), jen.Id("_")).Op(":=").Id("stringify").Call(jen.Id("r").Dot("Body")),
)
}
return append(codes, jen.If(jen.Id("r").Dot("Method").Op("==").Lit(h.value())).Block(*childCodes...))
}
func (h *Method) children() map[string]SyntaxNode {
return h.Children
}
func (h *Method) addChild(child SyntaxNode) {
h.Children[child.value()] = child
}
func (h *Method) value() string {
return h.Value
}
func (h *Path) render(childCodes *[]jen.Code, isFirst, isLast bool) []jen.Code {
return []jen.Code{
jen.Id("mux").Dot("HandleFunc").Call(jen.Lit(h.value()), jen.Func().Params(jen.Id("rw").Qual("net/http", "ResponseWriter"), jen.Id("r").Add(jen.Op("*")).Qual("net/http", "Request")).Block(
*childCodes...,
)),
}
}
func (h *Path) children() map[string]SyntaxNode {
return h.Children
}
func (h *Path) addChild(child SyntaxNode) {
h.Children[child.value()] = child
}
func (h *Path) value() string {
return h.Value
}
func createResponseKey(r *RespBody) string {
header, err := stringifyHeader(r.Header)
if err != nil {
// 失敗しても最低限のコード生成は可能なので続行する
log.Error().Err(err)
}
return fmt.Sprintf("%d-%s-%s", r.StatusCode, header, r.Value)
}