-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
331 lines (277 loc) · 8.43 KB
/
builder.go
File metadata and controls
331 lines (277 loc) · 8.43 KB
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
328
329
330
331
package mason
import (
"fmt"
"net/http"
"path"
"strings"
m "github.com/tailbits/mason/model"
)
var successCodes = map[string]int{
http.MethodPost: http.StatusCreated,
http.MethodPut: http.StatusOK,
http.MethodPatch: http.StatusCreated,
http.MethodDelete: http.StatusOK,
http.MethodGet: http.StatusOK,
}
type Builder interface {
ResourceID() string
OpID() string
Path(p string) Builder
WithGroup(group string) Builder
WithOpID(segments ...string) Builder
WithDesc(d string) Builder
WithTags(tags ...string) Builder
WithSuccessCode(code int) Builder
WithSummary(s string) Builder
WithMWs(mw ...Middleware) Builder
WithExtensions(key string, val interface{}) Builder
SkipIf(skip bool) Builder
RegisterBeta(api *API)
Register(api *API)
}
type RouteBuilderBase struct {
opID string
method string
path string
mw []func(WebHandler) WebHandler
desc string
tags []string
summary string
successCode int
skipped bool
group string
keyVals map[string]interface{}
}
func (rb *RouteBuilderBase) validate() error {
if rb.opID == "" {
return fmt.Errorf("operationID is required")
}
if rb.method == "" {
return fmt.Errorf("method is required")
}
if rb.path == "" {
return fmt.Errorf("path is required")
}
return nil
}
type RouteBuilderWithBody[T m.Entity, O m.Entity, Q any] struct {
RouteBuilderBase
handler HandlerWithBody[T, O, Q]
}
// ResourceID returns the resource ID for the route.
func (rb *RouteBuilderWithBody[T, O, Q]) ResourceID() string {
t := m.New[T]()
return RecursivelyUnwrap(t).Name()
}
// Path sets the path for the route. This can include path parameters like /users/{id}
func (rb *RouteBuilderWithBody[T, O, Q]) Path(p string) Builder {
rb.path = p
return rb
}
func (rb *RouteBuilderWithBody[T, O, Q]) WithGroup(group string) Builder {
rb.group = group
return rb
}
// WithOpID sets the operationID for the route. This is used primarily for documentation purposes.
func (rb *RouteBuilderWithBody[T, O, Q]) WithOpID(id ...string) Builder {
rb.opID = strings.ReplaceAll(path.Join(id...), "/", "_")
return rb
}
// OpID returns the operation ID for the route.
func (rb *RouteBuilderWithBody[T, O, Q]) OpID() string {
return rb.opID
}
// WithDesc sets the description for the route. This is used primarily for documentation purposes.
func (rb *RouteBuilderWithBody[T, O, Q]) WithDesc(d string) Builder {
rb.desc = d
return rb
}
// WithTags sets the tags for the route. This is used primarily for documentation purposes.
func (rb *RouteBuilderWithBody[T, O, Q]) WithTags(tags ...string) Builder {
rb.tags = tags
return rb
}
// WithExtensions sets custom x- attributes for the route. This is used for adding OpenAPI extensions..
func (rb *RouteBuilderWithBody[T, O, Q]) WithExtensions(key string, val interface{}) Builder {
if !strings.HasPrefix(key, "x-") {
panic(fmt.Errorf("custom keys must start with 'x-', key '%s' does not start with 'x-'", key))
}
rb.keyVals[key] = val
return rb
}
// WithSuccessCode sets the success code for the route. This can be used to override the default success code for the method.
func (rb *RouteBuilderWithBody[T, O, Q]) WithSuccessCode(code int) Builder {
rb.successCode = code
return rb
}
func (rb *RouteBuilderWithBody[T, O, Q]) WithSummary(s string) Builder {
rb.summary = s
return rb
}
// WithMWs defines a set of middlewares to add to the route.
func (rb *RouteBuilderWithBody[T, O, Q]) WithMWs(mw ...Middleware) Builder {
for _, m := range mw {
h := m.GetHandler(rb)
rb.mw = append(rb.mw, h)
}
return rb
}
// SkipIf ensures that the route is not documented if the condition is true.
func (rb *RouteBuilderWithBody[T, O, Q]) SkipIf(skip bool) Builder {
rb.skipped = skip
return rb
}
// RegisterBeta registers the route and marks it as beta, meaning it will not be included in the OpenAPI documentation.
func (rb *RouteBuilderWithBody[T, O, Q]) RegisterBeta(api *API) {
rb.SkipIf(true).Register(api)
}
// Register registers the route with the mux, and finalizes the route configuration.
func (rb *RouteBuilderWithBody[T, O, Q]) Register(api *API) {
if err := rb.validate(); err != nil {
panic(err)
}
if rb.handler == nil {
panic("handler is required")
}
if rb.group == "" {
msg := fmt.Sprintf("route group name could not be inferred for %s %s; consider using group.WithDefaultName() to set it explicitly", rb.method, rb.path)
panic(msg)
}
var output O
if rb.successCode == 0 {
rb.successCode = DefaultSuccessCode(rb.method, output)
}
if !rb.skipped {
registerModel[T, O, Q](
api,
rb.method,
rb.group,
rb.path,
WithOperationID(rb.opID),
WithSuccessCode((rb.successCode)),
WithDescription(rb.desc),
WithSummary(rb.summary),
WithTags(rb.tags...),
WithExtension(rb.keyVals),
)
}
h := newHandlerWithBody(api, rb.handler, rb.successCode)
api.Handle(rb.method, rb.path, h, rb.mw...)
}
type RouteBuilderNoBody[T m.Entity, Q any] struct {
RouteBuilderBase
handler HandlerNoBody[T, Q]
}
func (rb *RouteBuilderNoBody[T, Q]) ResourceID() string {
t := m.New[T]()
return RecursivelyUnwrap(t).Name()
}
// Path sets the path for the route. This can include path parameters like /users/{id}
func (rb *RouteBuilderNoBody[T, Q]) Path(p string) Builder {
rb.path = p
return rb
}
func (rb *RouteBuilderNoBody[T, Q]) WithGroup(group string) Builder {
rb.group = group
return rb
}
// WithOpID sets the operationID for the route. This is used primarily for documentation purposes.
func (rb *RouteBuilderNoBody[T, Q]) WithOpID(id ...string) Builder {
rb.opID = strings.ReplaceAll(path.Join(id...), "/", "_")
return rb
}
// OpID returns the operation ID for the route.
func (rb *RouteBuilderNoBody[T, Q]) OpID() string {
return rb.opID
}
// WithDesc sets the description for the route. This is used primarily for documentation purposes.
func (rb *RouteBuilderNoBody[T, Q]) WithDesc(d string) Builder {
rb.desc = d
return rb
}
// WithTags sets the tags for the route. This is used primarily for documentation purposes.
func (rb *RouteBuilderNoBody[T, Q]) WithTags(tags ...string) Builder {
rb.tags = tags
return rb
}
// WithExtensions sets custom x- attributes for the route. This is used for adding OpenAPI extensions..
func (rb *RouteBuilderNoBody[T, Q]) WithExtensions(key string, val interface{}) Builder {
if !strings.HasPrefix(key, "x-") {
panic(fmt.Errorf("invalid key [%s]: custom keys must start with 'x-'", key))
}
rb.keyVals[key] = val
return rb
}
// WithSuccessCode sets the success code for the route. This can be used to override the default success code for the method.
func (rb *RouteBuilderNoBody[T, Q]) WithSuccessCode(code int) Builder {
rb.successCode = code
return rb
}
func (rb *RouteBuilderNoBody[T, Q]) WithSummary(s string) Builder {
rb.summary = s
return rb
}
// WithMWs defines a set of middlewares to add to the route.
func (rb *RouteBuilderNoBody[T, Q]) WithMWs(mw ...Middleware) Builder {
for _, m := range mw {
h := m.GetHandler(rb)
rb.mw = append(rb.mw, h)
}
return rb
}
// SkipIf ensures that the route is not documented if the condition is true.
func (rb *RouteBuilderNoBody[T, Q]) SkipIf(skip bool) Builder {
rb.skipped = skip
return rb
}
// RegisterBeta registers the route and marks it as beta, meaning it will not be included in the OpenAPI documentation.
func (rb *RouteBuilderNoBody[T, Q]) RegisterBeta(api *API) {
rb.SkipIf(true).Register(api)
}
// Register registers the route with the mux, and finalizes the route configuration.
func (rb *RouteBuilderNoBody[T, Q]) Register(api *API) {
if err := rb.validate(); err != nil {
panic(err)
}
if rb.handler == nil {
panic("handler is required")
}
if rb.group == "" {
panic("group is required")
}
var output T
if rb.successCode == 0 {
rb.successCode = DefaultSuccessCode(rb.method, output)
}
if !rb.skipped {
registerResponseEntity[T, Q](
api,
rb.method,
rb.group,
rb.path,
WithOperationID(rb.opID),
WithSuccessCode((rb.successCode)),
WithDescription(rb.desc),
WithSummary(rb.summary),
WithTags(rb.tags...),
WithExtension(rb.keyVals),
)
}
h := newHandler(api, rb.handler, rb.successCode)
api.Handle(rb.method, rb.path, h, rb.mw...)
}
func DefaultSuccessCode(method string, output m.WithSchema) int {
if _, ok := any(output).(m.Nil); ok {
return http.StatusNoContent
}
return successCodes[method]
}
func RecursivelyUnwrap(current m.WithSchema) m.WithSchema {
for {
unwrapper, ok := current.(m.DerivedType)
if !ok {
return current
}
current = unwrapper.Unwrap()
}
}