-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_test.go
327 lines (280 loc) · 6.29 KB
/
new_test.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package ginplus
import (
"context"
"errors"
"log"
"testing"
"github.com/gin-gonic/gin"
)
type People struct {
*Img
}
type Img struct {
File *File
}
type File struct {
}
var _ IMiddleware = (*People)(nil)
var _ Controller = (*People)(nil)
var _ MethodeMiddleware = (*People)(nil)
func (l *Img) GetImgInfo() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.String(200, "GetInfo")
}
}
func (l *File) GetFiles() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.String(200, "GetFiles")
}
}
func (p *People) GetInfo() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.String(200, "GetInfo")
}
}
func (p *People) List() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.String(200, "List")
}
}
func (p *People) PostCreateInfo(_ context.Context, _ struct{ Name string }) (string, error) {
return "PostCreateInfo", errors.New("custom error")
}
func (p *People) PutUpdateInfo(_ context.Context, req struct{ Name string }) (struct{ Name string }, error) {
return struct{ Name string }{Name: req.Name}, nil
}
func (p *People) Middlewares() []gin.HandlerFunc {
return []gin.HandlerFunc{
func(context *gin.Context) {
log.Println("middleware1")
},
func(context *gin.Context) {
log.Println("middleware2")
},
}
}
func (p *People) BasePath() string {
return "/people/v1"
}
func (p *People) MethodeMiddlewares() map[string][]gin.HandlerFunc {
return map[string][]gin.HandlerFunc{
"GetInfo": {
func(ctx *gin.Context) {
log.Println("GetInfo middleware1")
},
},
}
}
type Slice []string
var _ IMiddleware = (*Slice)(nil)
func (l *Slice) Middlewares() []gin.HandlerFunc {
return nil
}
func (l *Slice) GetInfo() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.String(200, "GetInfo")
}
}
func TestNew(t *testing.T) {
r := gin.Default()
opts := []OptionFun{
WithMiddlewares(func(ctx *gin.Context) {
log.Println("main middleware")
}),
WithBasePath("aide-cloud"),
WithHttpMethodPrefixes(HttpMethod{
Prefix: "Get",
Method: Get,
}, HttpMethod{
Prefix: "Post",
Method: Post,
}, HttpMethod{
Prefix: "Put",
Method: Put,
}, HttpMethod{
Prefix: "Delete",
Method: Delete,
}),
WithControllers(&People{
Img: &Img{
File: &File{},
},
}, &Slice{}),
WithRouteNamingRuleFunc(func(methodName string) string {
return routeToCamel(methodName)
}),
WithApiConfig(ApiConfig{
Title: "aide-cloud-api",
Version: "v1",
}),
}
ginInstance := New(r, opts...)
_ = ginInstance.Run(":8080")
}
type (
MyController struct {
}
MyControllerReq struct {
Name string `form:"name"`
Id uint `uri:"id"`
Keyword string `form:"keyword"`
}
MyControllerResp struct {
Name string `json:"name"`
Id uint `json:"id"`
Age int `json:"-"`
Data any `json:"data"`
}
)
func (l *MyController) GetInfo(_ context.Context, req MyControllerReq) (*MyControllerResp, error) {
log.Println(req)
return nil, nil
}
func TestGenApi(t *testing.T) {
r := gin.Default()
opts := []OptionFun{
WithBasePath("aide-cloud"),
WithControllers(&MyController{}),
}
ginInstance := New(r, opts...)
ginInstance.genOpenApiYaml()
}
func TestGenApiRun(t *testing.T) {
r := gin.Default()
opts := []OptionFun{
WithBasePath("aide-cloud"),
WithControllers(&MyController{}),
}
ginInstance := New(r, opts...)
_ = ginInstance.Run()
}
type (
MidController struct {
ChildMidController *ChildMidController
}
ChildMidController struct {
}
)
func (l *ChildMidController) Middlewares() []gin.HandlerFunc {
return []gin.HandlerFunc{
func(ctx *gin.Context) {
log.Println("ChildMidController")
},
}
}
func (l *ChildMidController) Info() gin.HandlerFunc {
return func(ctx *gin.Context) {
log.Println("Info action")
}
}
type DetailReq struct {
Id uint `uri:"id"`
}
type DetailResp struct {
Name string `json:"name"`
Id uint `json:"id"`
}
func (l *ChildMidController) Detail(_ context.Context, req *DetailReq) (*DetailResp, error) {
log.Println("Detail")
return &DetailResp{Name: "aide-cloud", Id: req.Id}, nil
}
func (l *MidController) Middlewares() []gin.HandlerFunc {
return []gin.HandlerFunc{
func(ctx *gin.Context) {
log.Println("MidController")
},
}
}
func (l *MidController) GetParent() gin.HandlerFunc {
return func(ctx *gin.Context) {
log.Println("Parent action")
}
}
var _ IMiddleware = (*MidController)(nil)
var _ IMiddleware = (*ChildMidController)(nil)
func TestGenApiRunMid(t *testing.T) {
r := gin.Default()
opts := []OptionFun{
WithBasePath("aide-cloud"),
WithControllers(&MidController{
ChildMidController: &ChildMidController{},
}),
}
ginInstance := New(r, opts...)
_ = ginInstance.Run()
}
type (
Path1 struct {
Path2 *Path2
}
Path2 struct {
}
)
func (p *Path2) MethodeMiddlewares() map[string][]gin.HandlerFunc {
return map[string][]gin.HandlerFunc{
"GetInfoByID": {
func(ctx *gin.Context) {
log.Println("Path2 GetInfoByID middleware1")
},
func(ctx *gin.Context) {
log.Println("Path2 GetInfoByID middleware2")
},
},
}
}
func (p *Path2) Middlewares() []gin.HandlerFunc {
return []gin.HandlerFunc{
func(ctx *gin.Context) {
log.Println("Path2 Middlewares 1")
},
func(ctx *gin.Context) {
log.Println("Path2 Middlewares 2")
},
}
}
func (p *Path1) Middlewares() []gin.HandlerFunc {
return []gin.HandlerFunc{
func(ctx *gin.Context) {
log.Println("Path1 Middlewares 1")
},
func(ctx *gin.Context) {
log.Println("Path1 Middlewares 2")
},
}
}
var _ IMiddleware = (*Path1)(nil)
var _ IMiddleware = (*Path2)(nil)
var _ MethodeMiddleware = (*Path2)(nil)
func (p *Path1) GetInfo() gin.HandlerFunc {
return func(ctx *gin.Context) {
log.Println("Path1 GetInfo")
}
}
func (p *Path1) GetInfoByID(_ context.Context, req *struct {
Id uint `uri:"id"`
}) (*struct {
Id uint `json:"id"`
}, error) {
log.Println("Path1 GetInfoByID")
return (*struct {
Id uint `json:"id"`
})(&struct{ Id uint }{Id: req.Id}), nil
}
func (p *Path2) GetInfo() gin.HandlerFunc {
return func(ctx *gin.Context) {
log.Println("Path2 GetInfo")
}
}
func (p *Path2) GetInfoByID(_ context.Context, req *struct {
Id uint `uri:"id"`
}) (*struct {
Id uint `json:"id"`
}, error) {
log.Println("Path2 GetInfoByID")
return (*struct {
Id uint `json:"id"`
})(&struct{ Id uint }{Id: req.Id}), nil
}
func TestRouteGroup(t *testing.T) {
New(gin.Default(), WithControllers(&Path1{Path2: &Path2{}}))
}