-
Notifications
You must be signed in to change notification settings - Fork 63
/
demo.gen.go
655 lines (522 loc) · 19.8 KB
/
demo.gen.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Package demo provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.9.1 DO NOT EDIT.
package demo
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"github.com/deepmap/oapi-codegen/pkg/runtime"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-chi/chi/v5"
)
// Version defines model for Version.
type Version struct {
Version string `json:"version"`
}
// PostAlertEventIDParams defines parameters for PostAlertEventID.
type PostAlertEventIDParams struct {
Type PostAlertEventIDParamsType `json:"type"`
}
// PostAlertEventIDParamsType defines parameters for PostAlertEventID.
type PostAlertEventIDParamsType string
// PostApiApiIDAnnotationAnnotationJSONBody defines parameters for PostApiApiIDAnnotationAnnotation.
type PostApiApiIDAnnotationAnnotationJSONBody struct {
Data *string `json:"data,omitempty"`
}
// PostEventEventIDAnnotationAnnotationJSONBody defines parameters for PostEventEventIDAnnotationAnnotation.
type PostEventEventIDAnnotationAnnotationJSONBody struct {
Data *string `json:"data,omitempty"`
}
// PostEventsJSONBody defines parameters for PostEvents.
type PostEventsJSONBody struct {
AdditionalProperties map[string]interface{} `json:"-"`
}
// PostApiApiIDAnnotationAnnotationJSONRequestBody defines body for PostApiApiIDAnnotationAnnotation for application/json ContentType.
type PostApiApiIDAnnotationAnnotationJSONRequestBody PostApiApiIDAnnotationAnnotationJSONBody
// PostEventEventIDAnnotationAnnotationJSONRequestBody defines body for PostEventEventIDAnnotationAnnotation for application/json ContentType.
type PostEventEventIDAnnotationAnnotationJSONRequestBody PostEventEventIDAnnotationAnnotationJSONBody
// PostEventsJSONRequestBody defines body for PostEvents for application/json ContentType.
type PostEventsJSONRequestBody PostEventsJSONBody
// Getter for additional properties for PostEventsJSONBody. Returns the specified
// element and whether it was found
func (a PostEventsJSONBody) Get(fieldName string) (value interface{}, found bool) {
if a.AdditionalProperties != nil {
value, found = a.AdditionalProperties[fieldName]
}
return
}
// Setter for additional properties for PostEventsJSONBody
func (a *PostEventsJSONBody) Set(fieldName string, value interface{}) {
if a.AdditionalProperties == nil {
a.AdditionalProperties = make(map[string]interface{})
}
a.AdditionalProperties[fieldName] = value
}
// Override default JSON handling for PostEventsJSONBody to handle AdditionalProperties
func (a *PostEventsJSONBody) UnmarshalJSON(b []byte) error {
object := make(map[string]json.RawMessage)
err := json.Unmarshal(b, &object)
if err != nil {
return err
}
if len(object) != 0 {
a.AdditionalProperties = make(map[string]interface{})
for fieldName, fieldBuf := range object {
var fieldVal interface{}
err := json.Unmarshal(fieldBuf, &fieldVal)
if err != nil {
return fmt.Errorf("error unmarshaling field %s: %w", fieldName, err)
}
a.AdditionalProperties[fieldName] = fieldVal
}
}
return nil
}
// Override default JSON handling for PostEventsJSONBody to handle AdditionalProperties
func (a PostEventsJSONBody) MarshalJSON() ([]byte, error) {
var err error
object := make(map[string]json.RawMessage)
for fieldName, field := range a.AdditionalProperties {
object[fieldName], err = json.Marshal(field)
if err != nil {
return nil, fmt.Errorf("error marshaling '%s': %w", fieldName, err)
}
}
return json.Marshal(object)
}
// ServerInterface represents all server handlers.
type ServerInterface interface {
// (POST /alert/{eventID})
PostAlertEventID(w http.ResponseWriter, r *http.Request, eventID int, params PostAlertEventIDParams)
// (GET /api/{apiID})
GetApiApiID(w http.ResponseWriter, r *http.Request, apiID int)
// (DELETE /api/{apiID}/annotation/{annotation})
DeleteApiApiIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request, apiID int, annotation string)
// (GET /api/{apiID}/annotation/{annotation})
GetApiApiIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request, apiID int, annotation string)
// (POST /api/{apiID}/annotation/{annotation})
PostApiApiIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request, apiID int, annotation string)
// (GET /event/{eventID}/annotation/{annotation})
GetEventEventIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request, eventID int, annotation string)
// (POST /event/{eventID}/annotation/{annotation})
PostEventEventIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request, eventID int, annotation string)
// (POST /events)
PostEvents(w http.ResponseWriter, r *http.Request)
// Get the version of this Plugin
// (GET /version)
GetVersion(w http.ResponseWriter, r *http.Request)
}
// ServerInterfaceWrapper converts contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
HandlerMiddlewares []MiddlewareFunc
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
}
type MiddlewareFunc func(http.HandlerFunc) http.HandlerFunc
// PostAlertEventID operation middleware
func (siw *ServerInterfaceWrapper) PostAlertEventID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "eventID" -------------
var eventID int
err = runtime.BindStyledParameter("simple", false, "eventID", chi.URLParam(r, "eventID"), &eventID)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "eventID", Err: err})
return
}
// Parameter object where we will unmarshal all parameters from the context
var params PostAlertEventIDParams
// ------------- Required query parameter "type" -------------
if paramValue := r.URL.Query().Get("type"); paramValue != "" {
} else {
siw.ErrorHandlerFunc(w, r, &RequiredParamError{ParamName: "type"})
return
}
err = runtime.BindQueryParameter("form", true, true, "type", r.URL.Query(), ¶ms.Type)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "type", Err: err})
return
}
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.PostAlertEventID(w, r, eventID, params)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// GetApiApiID operation middleware
func (siw *ServerInterfaceWrapper) GetApiApiID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "apiID" -------------
var apiID int
err = runtime.BindStyledParameter("simple", false, "apiID", chi.URLParam(r, "apiID"), &apiID)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "apiID", Err: err})
return
}
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.GetApiApiID(w, r, apiID)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// DeleteApiApiIDAnnotationAnnotation operation middleware
func (siw *ServerInterfaceWrapper) DeleteApiApiIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "apiID" -------------
var apiID int
err = runtime.BindStyledParameter("simple", false, "apiID", chi.URLParam(r, "apiID"), &apiID)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "apiID", Err: err})
return
}
// ------------- Path parameter "annotation" -------------
var annotation string
err = runtime.BindStyledParameter("simple", false, "annotation", chi.URLParam(r, "annotation"), &annotation)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "annotation", Err: err})
return
}
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.DeleteApiApiIDAnnotationAnnotation(w, r, apiID, annotation)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// GetApiApiIDAnnotationAnnotation operation middleware
func (siw *ServerInterfaceWrapper) GetApiApiIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "apiID" -------------
var apiID int
err = runtime.BindStyledParameter("simple", false, "apiID", chi.URLParam(r, "apiID"), &apiID)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "apiID", Err: err})
return
}
// ------------- Path parameter "annotation" -------------
var annotation string
err = runtime.BindStyledParameter("simple", false, "annotation", chi.URLParam(r, "annotation"), &annotation)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "annotation", Err: err})
return
}
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.GetApiApiIDAnnotationAnnotation(w, r, apiID, annotation)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// PostApiApiIDAnnotationAnnotation operation middleware
func (siw *ServerInterfaceWrapper) PostApiApiIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "apiID" -------------
var apiID int
err = runtime.BindStyledParameter("simple", false, "apiID", chi.URLParam(r, "apiID"), &apiID)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "apiID", Err: err})
return
}
// ------------- Path parameter "annotation" -------------
var annotation string
err = runtime.BindStyledParameter("simple", false, "annotation", chi.URLParam(r, "annotation"), &annotation)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "annotation", Err: err})
return
}
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.PostApiApiIDAnnotationAnnotation(w, r, apiID, annotation)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// GetEventEventIDAnnotationAnnotation operation middleware
func (siw *ServerInterfaceWrapper) GetEventEventIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "eventID" -------------
var eventID int
err = runtime.BindStyledParameter("simple", false, "eventID", chi.URLParam(r, "eventID"), &eventID)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "eventID", Err: err})
return
}
// ------------- Path parameter "annotation" -------------
var annotation string
err = runtime.BindStyledParameter("simple", false, "annotation", chi.URLParam(r, "annotation"), &annotation)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "annotation", Err: err})
return
}
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.GetEventEventIDAnnotationAnnotation(w, r, eventID, annotation)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// PostEventEventIDAnnotationAnnotation operation middleware
func (siw *ServerInterfaceWrapper) PostEventEventIDAnnotationAnnotation(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "eventID" -------------
var eventID int
err = runtime.BindStyledParameter("simple", false, "eventID", chi.URLParam(r, "eventID"), &eventID)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "eventID", Err: err})
return
}
// ------------- Path parameter "annotation" -------------
var annotation string
err = runtime.BindStyledParameter("simple", false, "annotation", chi.URLParam(r, "annotation"), &annotation)
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "annotation", Err: err})
return
}
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.PostEventEventIDAnnotationAnnotation(w, r, eventID, annotation)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// PostEvents operation middleware
func (siw *ServerInterfaceWrapper) PostEvents(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.PostEvents(w, r)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// GetVersion operation middleware
func (siw *ServerInterfaceWrapper) GetVersion(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.GetVersion(w, r)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
type UnescapedCookieParamError struct {
ParamName string
Err error
}
func (e *UnescapedCookieParamError) Error() string {
return fmt.Sprintf("error unescaping cookie parameter '%s'", e.ParamName)
}
func (e *UnescapedCookieParamError) Unwrap() error {
return e.Err
}
type UnmarshalingParamError struct {
ParamName string
Err error
}
func (e *UnmarshalingParamError) Error() string {
return fmt.Sprintf("Error unmarshaling parameter %s as JSON: %s", e.ParamName, e.Err.Error())
}
func (e *UnmarshalingParamError) Unwrap() error {
return e.Err
}
type RequiredParamError struct {
ParamName string
}
func (e *RequiredParamError) Error() string {
return fmt.Sprintf("Query argument %s is required, but not found", e.ParamName)
}
type RequiredHeaderError struct {
ParamName string
Err error
}
func (e *RequiredHeaderError) Error() string {
return fmt.Sprintf("Header parameter %s is required, but not found", e.ParamName)
}
func (e *RequiredHeaderError) Unwrap() error {
return e.Err
}
type InvalidParamFormatError struct {
ParamName string
Err error
}
func (e *InvalidParamFormatError) Error() string {
return fmt.Sprintf("Invalid format for parameter %s: %s", e.ParamName, e.Err.Error())
}
func (e *InvalidParamFormatError) Unwrap() error {
return e.Err
}
type TooManyValuesForParamError struct {
ParamName string
Count int
}
func (e *TooManyValuesForParamError) Error() string {
return fmt.Sprintf("Expected one value for %s, got %d", e.ParamName, e.Count)
}
// Handler creates http.Handler with routing matching OpenAPI spec.
func Handler(si ServerInterface) http.Handler {
return HandlerWithOptions(si, ChiServerOptions{})
}
type ChiServerOptions struct {
BaseURL string
BaseRouter chi.Router
Middlewares []MiddlewareFunc
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
}
// HandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux.
func HandlerFromMux(si ServerInterface, r chi.Router) http.Handler {
return HandlerWithOptions(si, ChiServerOptions{
BaseRouter: r,
})
}
func HandlerFromMuxWithBaseURL(si ServerInterface, r chi.Router, baseURL string) http.Handler {
return HandlerWithOptions(si, ChiServerOptions{
BaseURL: baseURL,
BaseRouter: r,
})
}
// HandlerWithOptions creates http.Handler with additional options
func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handler {
r := options.BaseRouter
if r == nil {
r = chi.NewRouter()
}
if options.ErrorHandlerFunc == nil {
options.ErrorHandlerFunc = func(w http.ResponseWriter, r *http.Request, err error) {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}
wrapper := ServerInterfaceWrapper{
Handler: si,
HandlerMiddlewares: options.Middlewares,
ErrorHandlerFunc: options.ErrorHandlerFunc,
}
r.Group(func(r chi.Router) {
r.Post(options.BaseURL+"/alert/{eventID}", wrapper.PostAlertEventID)
})
r.Group(func(r chi.Router) {
r.Get(options.BaseURL+"/api/{apiID}", wrapper.GetApiApiID)
})
r.Group(func(r chi.Router) {
r.Delete(options.BaseURL+"/api/{apiID}/annotation/{annotation}", wrapper.DeleteApiApiIDAnnotationAnnotation)
})
r.Group(func(r chi.Router) {
r.Get(options.BaseURL+"/api/{apiID}/annotation/{annotation}", wrapper.GetApiApiIDAnnotationAnnotation)
})
r.Group(func(r chi.Router) {
r.Post(options.BaseURL+"/api/{apiID}/annotation/{annotation}", wrapper.PostApiApiIDAnnotationAnnotation)
})
r.Group(func(r chi.Router) {
r.Get(options.BaseURL+"/event/{eventID}/annotation/{annotation}", wrapper.GetEventEventIDAnnotationAnnotation)
})
r.Group(func(r chi.Router) {
r.Post(options.BaseURL+"/event/{eventID}/annotation/{annotation}", wrapper.PostEventEventIDAnnotationAnnotation)
})
r.Group(func(r chi.Router) {
r.Post(options.BaseURL+"/events", wrapper.PostEvents)
})
r.Group(func(r chi.Router) {
r.Get(options.BaseURL+"/version", wrapper.GetVersion)
})
return r
}
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{
"H4sIAAAAAAAC/9xWTW+cMBD9K2jao7Vsmxs30nxopSpd9ZAeohxcmLCOwHbsIdIK8d+rMewCzWY3aZSk",
"7QkLP57fvJkx00BmKms0avKQNOCzFVYyLC/ReWU0L60zFh0pDBv3wwatLUICnpzSBbStAId3tXKYQ3K1",
"BV6LDdD8vMWMoGWk0jeGOXL0mVOWAiWky8WXUjpF62hZ1oXSUbpcgABSVOJ0//jsawpikAPz2Xz2CVoB",
"xqKWVkECR7P57AgEWEmrID6WJTqKG7xHTYuTNoRnPPGTg5SsY5FDAkvjKWXwaQcNLE5WSOg8JFcNKD6U",
"mUGAlhXLwy12MIJcjaJ3dmSa0oQFOnatp7qr0a0HroDbR4S6rtjnxcXZNxDwI/1+MfJ6m5Rr5vDWaN/l",
"7/N8zo/MaEIdApfWlioLoce3vkvtcIzMc8VbslyO6oDFhDxO8+eRomAxJ7kVEEur4kZa1Xtd4A6rz5FS",
"q1IGPcll2SOf4fFfY0IstTbUHdMM67ZrhRIJH/pzEt5vLEq3Hw0reMvwOpnRIL5r6EOpfXfdBaflN9Gv",
"UmxiN9E06gNs4/bdc0Ed8vauRk/HJl8/y9bphZ9L2qmsK+43bqtJ/ri3wpU7XOj7+uuxCg03fH/N/6NV",
"+qIfz+vX6ZMc/p9qtXi8Vv3+meO0w/y5IXt1tu/6HxyNjn0vTj84R4pohVGPi8xNRCvl+1EQxMPW3Uyp",
"Lwzro8MbSOBDPIzDcT8Lx5sjdgR4OdaJG5mM83VVSbc+HBM78ysAAP//xLNfTIYLAAA=",
}
// GetSwagger returns the content of the embedded swagger specification file
// or error if failed to decode
func decodeSpec() ([]byte, error) {
zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, ""))
if err != nil {
return nil, fmt.Errorf("error base64 decoding spec: %s", err)
}
zr, err := gzip.NewReader(bytes.NewReader(zipped))
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %s", err)
}
var buf bytes.Buffer
_, err = buf.ReadFrom(zr)
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %s", err)
}
return buf.Bytes(), nil
}
var rawSpec = decodeSpecCached()
// a naive cached of a decoded swagger spec
func decodeSpecCached() func() ([]byte, error) {
data, err := decodeSpec()
return func() ([]byte, error) {
return data, err
}
}
// Constructs a synthetic filesystem for resolving external references when loading openapi specifications.
func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) {
var res = make(map[string]func() ([]byte, error))
if len(pathToFile) > 0 {
res[pathToFile] = rawSpec
}
return res
}
// GetSwagger returns the Swagger specification corresponding to the generated code
// in this file. The external references of Swagger specification are resolved.
// The logic of resolving external references is tightly connected to "import-mapping" feature.
// Externally referenced files must be embedded in the corresponding golang packages.
// Urls can be supported but this task was out of the scope.
func GetSwagger() (swagger *openapi3.T, err error) {
var resolvePath = PathToRawSpec("")
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) {
var pathToFile = url.String()
pathToFile = path.Clean(pathToFile)
getSpec, ok := resolvePath[pathToFile]
if !ok {
err1 := fmt.Errorf("path not found: %s", pathToFile)
return nil, err1
}
return getSpec()
}
var specData []byte
specData, err = rawSpec()
if err != nil {
return
}
swagger, err = loader.LoadFromData(specData)
if err != nil {
return
}
return
}