-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
option.go
249 lines (225 loc) · 7.5 KB
/
option.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
package fuego
import (
"net/http"
"github.com/getkin/kin-openapi/openapi3"
)
// Group allows to group routes under a common path.
// Useful to group often used middlewares or options and reuse them.
// Example:
//
// optionsPagination := option.Group(
// option.QueryInt("per_page", "Number of items per page", ParamRequired()),
// option.QueryInt("page", "Page number", ParamDefault(1)),
// )
func GroupOptions(options ...func(*BaseRoute)) func(*BaseRoute) {
return func(r *BaseRoute) {
for _, option := range options {
option(r)
}
}
}
// Middleware adds one or more route-scoped middleware.
func OptionMiddleware(middleware ...func(http.Handler) http.Handler) func(*BaseRoute) {
return func(r *BaseRoute) {
r.Middlewares = append(r.Middlewares, middleware...)
}
}
// Declare a query parameter for the route.
// This will be added to the OpenAPI spec.
// Example:
//
// Query("name", "Filter by name", ParamExample("cat name", "felix"), ParamNullable())
//
// The list of options is in the param package.
func OptionQuery(name, description string, options ...func(*OpenAPIParam)) func(*BaseRoute) {
options = append(options, ParamDescription(description), paramType(QueryParamType), ParamString())
return func(r *BaseRoute) {
OptionParam(name, options...)(r)
}
}
// Declare an integer query parameter for the route.
// This will be added to the OpenAPI spec.
// The query parameter is transmitted as a string in the URL, but it is parsed as an integer.
// Example:
//
// QueryInt("age", "Filter by age (in years)", ParamExample("3 years old", 3), ParamNullable())
//
// The list of options is in the param package.
func OptionQueryInt(name, description string, options ...func(*OpenAPIParam)) func(*BaseRoute) {
options = append(options, ParamDescription(description), paramType(QueryParamType), ParamInteger())
return func(r *BaseRoute) {
OptionParam(name, options...)(r)
}
}
// Declare a boolean query parameter for the route.
// This will be added to the OpenAPI spec.
// The query parameter is transmitted as a string in the URL, but it is parsed as a boolean.
// Example:
//
// QueryBool("is_active", "Filter by active status", ParamExample("true", true), ParamNullable())
//
// The list of options is in the param package.
func OptionQueryBool(name, description string, options ...func(*OpenAPIParam)) func(*BaseRoute) {
options = append(options, ParamDescription(description), paramType(QueryParamType), ParamBool())
return func(r *BaseRoute) {
OptionParam(name, options...)(r)
}
}
// Declare a header parameter for the route.
// This will be added to the OpenAPI spec.
// Example:
//
// Header("Authorization", "Bearer token", ParamRequired())
//
// The list of options is in the param package.
func OptionHeader(name, description string, options ...func(*OpenAPIParam)) func(*BaseRoute) {
options = append(options, ParamDescription(description), paramType(HeaderParamType))
return func(r *BaseRoute) {
OptionParam(name, options...)(r)
}
}
// Declare a cookie parameter for the route.
// This will be added to the OpenAPI spec.
// Example:
//
// Cookie("session_id", "Session ID", ParamRequired())
//
// The list of options is in the param package.
func OptionCookie(name, description string, options ...func(*OpenAPIParam)) func(*BaseRoute) {
options = append(options, ParamDescription(description), paramType(CookieParamType))
return func(r *BaseRoute) {
OptionParam(name, options...)(r)
}
}
func paramType(paramType ParamType) func(*OpenAPIParam) {
return func(param *OpenAPIParam) {
param.Type = paramType
}
}
func panicsIfNotCorrectType(openapiParam *openapi3.Parameter, exampleValue any) any {
if exampleValue == nil {
return nil
}
if openapiParam.Schema.Value.Type.Is("integer") {
_, ok := exampleValue.(int)
if !ok {
panic("example value must be an integer")
}
}
if openapiParam.Schema.Value.Type.Is("boolean") {
_, ok := exampleValue.(bool)
if !ok {
panic("example value must be a boolean")
}
}
if openapiParam.Schema.Value.Type.Is("string") {
_, ok := exampleValue.(string)
if !ok {
panic("example value must be a string")
}
}
return exampleValue
}
// Registers a parameter for the route. Prefer using the [Query], [QueryInt], [Header], [Cookie] shortcuts.
func OptionParam(name string, options ...func(*OpenAPIParam)) func(*BaseRoute) {
param := OpenAPIParam{
Name: name,
}
// Applies options to OpenAPIParam
for _, option := range options {
option(¶m)
}
// Applies OpenAPIParam to openapi3.Parameter
// Why not use openapi3.NewHeaderParameter(name) directly?
// Because we might change the openapi3 library in the future,
// and we want to keep the flexibility to change the implementation without changing the API.
openapiParam := &openapi3.Parameter{
Name: name,
In: string(param.Type),
Description: param.Description,
Schema: openapi3.NewStringSchema().NewRef(),
}
if param.GoType != "" {
openapiParam.Schema.Value.Type = &openapi3.Types{param.GoType}
}
openapiParam.Schema.Value.Nullable = param.Nullable
openapiParam.Schema.Value.Default = panicsIfNotCorrectType(openapiParam, param.Default)
if param.Required {
openapiParam.Required = param.Required
}
for name, exampleValue := range param.Examples {
if openapiParam.Examples == nil {
openapiParam.Examples = make(openapi3.Examples)
}
exampleOpenAPI := openapi3.NewExample(name)
exampleOpenAPI.Value = panicsIfNotCorrectType(openapiParam, exampleValue)
openapiParam.Examples[name] = &openapi3.ExampleRef{Value: exampleOpenAPI}
}
return func(r *BaseRoute) {
r.Operation.AddParameter(openapiParam)
if r.Params == nil {
r.Params = make(map[string]OpenAPIParam)
}
r.Params[name] = param
}
}
// Tags adds one or more tags to the route.
func OptionTags(tags ...string) func(*BaseRoute) {
return func(r *BaseRoute) {
r.Operation.Tags = append(r.Operation.Tags, tags...)
}
}
// Summary adds a summary to the route.
func OptionSummary(summary string) func(*BaseRoute) {
return func(r *BaseRoute) {
r.Operation.Summary = summary
}
}
// Description adds a description to the route.
func OptionDescription(description string) func(*BaseRoute) {
return func(r *BaseRoute) {
r.Operation.Description = description
}
}
// OperationID adds an operation ID to the route.
func OptionOperationID(operationID string) func(*BaseRoute) {
return func(r *BaseRoute) {
r.Operation.OperationID = operationID
}
}
// Deprecated marks the route as deprecated.
func OptionDeprecated() func(*BaseRoute) {
return func(r *BaseRoute) {
r.Operation.Deprecated = true
}
}
// AddError adds an error to the route.
func OptionAddError(code int, description string, errorType ...any) func(*BaseRoute) {
var responseSchema SchemaTag
return func(r *BaseRoute) {
if len(errorType) > 0 {
responseSchema = SchemaTagFromType(r.mainRouter, errorType[0])
} else {
responseSchema = SchemaTagFromType(r.mainRouter, HTTPError{})
}
content := openapi3.NewContentWithSchemaRef(&responseSchema.SchemaRef, []string{"application/json"})
response := openapi3.NewResponse().
WithDescription(description).
WithContent(content)
r.Operation.AddResponse(code, response)
}
}
// RequestContentType sets the accepted content types for the route.
// By default, the accepted content types is */*.
// This will override any options set at the server level.
func OptionRequestContentType(consumes ...string) func(*BaseRoute) {
return func(r *BaseRoute) {
r.AcceptedContentTypes = consumes
}
}
// Hide hides the route from the OpenAPI spec.
func OptionHide() func(*BaseRoute) {
return func(r *BaseRoute) {
r.Hidden = true
}
}