-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtreefarm.gen.go
More file actions
533 lines (449 loc) · 17.6 KB
/
treefarm.gen.go
File metadata and controls
533 lines (449 loc) · 17.6 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
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
// Code generated by oapi-codegen; DO NOT EDIT.
package treefarm
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"github.com/google/uuid"
)
// #/components/schemas/Tree
// A tree to be planted
type Tree struct {
// Where to plant the tree (e.g. "north meadow")
Location string `form:"location" json:"location"`
// What kind of tree to plant (e.g. "oak")
Kind string `form:"kind" json:"kind"`
}
// ApplyDefaults sets default values for fields that are nil.
func (s *Tree) ApplyDefaults() {
}
// #/components/schemas/TreePlantingRequest
// A tree planting request, combining the tree details with a callback URL
// for completion notification.
type TreePlantingRequest struct {
// Where to plant the tree (e.g. "north meadow")
Location string `form:"location" json:"location"`
// What kind of tree to plant (e.g. "oak")
Kind string `form:"kind" json:"kind"`
// URL to receive the planting result callback
CallbackURL string `form:"callbackUrl" json:"callbackUrl"`
}
// ApplyDefaults sets default values for fields that are nil.
func (s *TreePlantingRequest) ApplyDefaults() {
}
// #/components/schemas/TreeWithId
// A tree with a server-assigned identifier
type TreeWithID struct {
// Where to plant the tree (e.g. "north meadow")
Location string `form:"location" json:"location"`
// What kind of tree to plant (e.g. "oak")
Kind string `form:"kind" json:"kind"`
// Unique identifier for this planting request
ID UUID `form:"id" json:"id"`
}
// ApplyDefaults sets default values for fields that are nil.
func (s *TreeWithID) ApplyDefaults() {
}
// #/components/schemas/TreePlantingResult
// The result of a tree planting operation, sent via callback
type TreePlantingResult struct {
// Where to plant the tree (e.g. "north meadow")
Location string `form:"location" json:"location"`
// What kind of tree to plant (e.g. "oak")
Kind string `form:"kind" json:"kind"`
// Unique identifier for this planting request
ID UUID `form:"id" json:"id"`
// Whether the tree was successfully planted
Success bool `form:"success" json:"success"`
}
// ApplyDefaults sets default values for fields that are nil.
func (s *TreePlantingResult) ApplyDefaults() {
}
// #/components/schemas/Error
type Error struct {
// Error code
Code int32 `form:"code" json:"code"`
// Error message
Message string `form:"message" json:"message"`
}
// ApplyDefaults sets default values for fields that are nil.
func (s *Error) ApplyDefaults() {
}
// Base64-encoded, gzip-compressed OpenAPI spec.
var openAPISpecJSON = []string{
"H4sIAAAAAAAC/8RXwW7bOBC96ysGaQG3QCO76U23bJEFAhSbIHGQ44IWxxYbilTJUbzG7gL7EfuF+yUL",
"khJFy4rr9NKcopDzZvhm5s1EN6hYIwo4+5R/zBdnmVBrXWQAz2is0KqAj/kiX2QAJEhiAUuDCL8yU2cA",
"HG1pREP+3l8ZAMAlkLvQSKZIqA1YNM+iRKCKEXCstbJkGKGFmwbV5e01lEzKFSufbO4BlhVCKQUqAtuu",
"akEW2AjT4LcWLQGTWm1gK6gCFmHg4e5LQHqsUAFVGKwrZmGFqAIM8g/+yIWHBiwq7vzc3twvgbQ/SgE9",
"HmlQmsR6F45DjHrtvwzaVpLzK0WJymLhTRSrsYDLhpUVwoWnEaA1soCKqLHFfL7dbnPmz3NtNvPO2s6/",
"XH+++u3+6vwiX+QV1TJrGFXWoc5ZI+b+Eb+7hwVHjbYUfgOwbV0zsyvgrudpn7/u2kTy3M99JL2nmXQw",
"7XEYBeoaLMVaIAepS+Zg8giyrIQF4UBcis5Nq5RLnG7Q+Jvw3z//pvyzssSGbEeldxuxmOIgGaEJ7Au0",
"MT1o4Fkw/9kY/Sw48ok6gMHvNS/g1r3FVXF32Pn7RfNdEZ2+NbguYPZmXuq60QoV2flwUaCdO4TbjtFZ",
"hLKNdukbgGYXi4vZ8DmifTld154N5IlVqRWhohQIgDWNFIH6+Ver1f4pgC0rrNn4r9OPC3fDsx4FVdd8",
"lg0hr1kr6cVXtAr/aLAk5IDGaPMz4r5yjvuQo6QMGNSnC3kKPPvzbcd6vtJ892bemz4Y+fdsP4S0x2Ko",
"fa+NM+nkoCvY8NKR4QvdF7vQactql/bIhCzBdk/iRg0+/Ax955iT6OT3nW3LEq1dt1LuQPvuep9nLxi6",
"xlkOFI6uTbZQeigM8gLItHhwPFkhp9XJ8Wo5rdb7Fr7zGZsdPOygoZPGXsymvO5l9nOfLYMlimfkkNKe",
"HdoedNprO+4EXk9l9nvcntSRw4mD6Q4D4jKOLwDaNViAXn3FkqYGVLdXkIYV9iM8GxVYDPM8jqTkT09C",
"9RaNcYVNIs1rb5E+NgRlyew31V5gjxUaHGZkbMZ3mG9yOFPaUAU1Mq63Z+8jiAvm9Z4YeUO/dHRsBK+d",
"M82ezt5nkduhtn17FscH/+X0lvXBacZK+PkdH8eRmJB2aveKeGtterlxypNqYS8zTMqbdZq273XrLLk7",
"UTHT1RCuJ8KenExVQjpBHowcV/8LqeqeXDMqoDXimNw75SbdS0JYX0aTo/c/JDMM5eJIb3TJCOPinFkr",
"Ngo5CI7K703mJ7Au+AlkC/4jHLeCHyVZiW8tJq/3BUluMR2X+FTH2ESG91e2uO27Nhz/axIH5gf3PwX5",
"9TTm8gfpH69jr05CN3JOyER3czodK60lsqOLzGOFVLldpReKLbN7Ey9Kt0fxI+LIBJhS9lJzTD5rtJZt",
"8IiwO4NDqRWKcIPp4OwLSyj6dPGSBvuI92PoInitmgekPvwsXaJi+GlJThfjlGJnR/aug63g2CYwNf1P",
"36d8LLPs/wAAAP//y9dX5mEQAAA=",
}
// decodeOpenAPISpec decodes and decompresses the embedded spec.
func decodeOpenAPISpec() ([]byte, error) {
joined := strings.Join(openAPISpecJSON, "")
raw, err := base64.StdEncoding.DecodeString(joined)
if err != nil {
return nil, fmt.Errorf("decoding base64: %w", err)
}
r, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil {
return nil, fmt.Errorf("creating gzip reader: %w", err)
}
defer r.Close()
var out bytes.Buffer
if _, err := out.ReadFrom(r); err != nil {
return nil, fmt.Errorf("decompressing: %w", err)
}
return out.Bytes(), nil
}
// decodeOpenAPISpecCached returns a closure that caches the decoded spec.
func decodeOpenAPISpecCached() func() ([]byte, error) {
var cached []byte
var cachedErr error
var once sync.Once
return func() ([]byte, error) {
once.Do(func() {
cached, cachedErr = decodeOpenAPISpec()
})
return cached, cachedErr
}
}
var openAPISpec = decodeOpenAPISpecCached()
// GetOpenAPISpecJSON returns the raw OpenAPI spec as JSON bytes.
func GetOpenAPISpecJSON() ([]byte, error) {
return openAPISpec()
}
// ServerInterface represents all server handlers.
type ServerInterface interface {
// Request a tree planting
// (POST /api/plant_tree)
PlantTree(w http.ResponseWriter, r *http.Request)
}
// ServerInterfaceWrapper converts HTTP requests to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
HandlerMiddlewares []MiddlewareFunc
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
}
// MiddlewareFunc is a middleware function type.
type MiddlewareFunc func(http.Handler) http.Handler
// PlantTree operation middleware
func (siw *ServerInterfaceWrapper) PlantTree(w http.ResponseWriter, r *http.Request) {
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
siw.Handler.PlantTree(w, r)
}))
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler.ServeHTTP(w, r)
}
// Handler creates http.Handler with routing matching OpenAPI spec.
func Handler(si ServerInterface) http.Handler {
return HandlerWithOptions(si, StdHTTPServerOptions{})
}
// ServeMux is an abstraction of http.ServeMux.
type ServeMux interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
// StdHTTPServerOptions configures the StdHTTP server.
type StdHTTPServerOptions struct {
BaseURL string
BaseRouter ServeMux
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, m ServeMux) http.Handler {
return HandlerWithOptions(si, StdHTTPServerOptions{
BaseRouter: m,
})
}
// HandlerFromMuxWithBaseURL creates http.Handler with routing and a base URL.
func HandlerFromMuxWithBaseURL(si ServerInterface, m ServeMux, baseURL string) http.Handler {
return HandlerWithOptions(si, StdHTTPServerOptions{
BaseURL: baseURL,
BaseRouter: m,
})
}
// HandlerWithOptions creates http.Handler with additional options.
func HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.Handler {
m := options.BaseRouter
if m == nil {
m = http.NewServeMux()
}
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,
}
m.HandleFunc("POST "+options.BaseURL+"/api/plant_tree", wrapper.PlantTree)
return m
}
// UnescapedCookieParamError is returned when a cookie parameter cannot be unescaped.
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
}
// UnmarshalingParamError is returned when a parameter cannot be unmarshaled.
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
}
// RequiredParamError is returned when a required parameter is missing.
type RequiredParamError struct {
ParamName string
}
func (e *RequiredParamError) Error() string {
return fmt.Sprintf("Query argument %s is required, but not found", e.ParamName)
}
// RequiredHeaderError is returned when a required header is missing.
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
}
// InvalidParamFormatError is returned when a parameter has an invalid format.
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
}
// TooManyValuesForParamError is returned when a parameter has too many values.
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)
}
type TreePlantedJSONRequestBody = TreePlantingResult
// RequestEditorFn is the function signature for the RequestEditor callback function.
// It may already be defined if client code is also generated; this is a compatible redeclaration.
type RequestEditorFn func(ctx context.Context, req *http.Request) error
// HttpRequestDoer performs HTTP requests.
// The standard http.Client implements this interface.
type HttpRequestDoer interface {
Do(req *http.Request) (*http.Response, error)
}
// CallbackInitiator sends callback requests to target URLs.
// Unlike Client, it has no stored base URL — the full target URL is provided per-call.
type CallbackInitiator struct {
// Doer for performing requests, typically a *http.Client with any
// customized settings, such as certificate chains.
Client HttpRequestDoer
// A list of callbacks for modifying requests which are generated before sending over
// the network.
RequestEditors []RequestEditorFn
}
// CallbackInitiatorOption allows setting custom parameters during construction.
type CallbackInitiatorOption func(*CallbackInitiator) error
// NewCallbackInitiator creates a new CallbackInitiator with reasonable defaults.
func NewCallbackInitiator(opts ...CallbackInitiatorOption) (*CallbackInitiator, error) {
initiator := CallbackInitiator{}
for _, o := range opts {
if err := o(&initiator); err != nil {
return nil, err
}
}
if initiator.Client == nil {
initiator.Client = &http.Client{}
}
return &initiator, nil
}
// WithCallbackHTTPClient allows overriding the default Doer, which is
// automatically created using http.Client. This is useful for tests.
func WithCallbackHTTPClient(doer HttpRequestDoer) CallbackInitiatorOption {
return func(p *CallbackInitiator) error {
p.Client = doer
return nil
}
}
// WithCallbackRequestEditorFn allows setting up a callback function, which will be
// called right before sending the request. This can be used to mutate the request.
func WithCallbackRequestEditorFn(fn RequestEditorFn) CallbackInitiatorOption {
return func(p *CallbackInitiator) error {
p.RequestEditors = append(p.RequestEditors, fn)
return nil
}
}
func (p *CallbackInitiator) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error {
for _, r := range p.RequestEditors {
if err := r(ctx, req); err != nil {
return err
}
}
for _, r := range additionalEditors {
if err := r(ctx, req); err != nil {
return err
}
}
return nil
}
// CallbackInitiatorInterface is the interface specification for the callback initiator.
type CallbackInitiatorInterface interface {
// TreePlantedWithBody sends a POST callback request
TreePlantedWithBody(ctx context.Context, targetURL string, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error)
TreePlanted(ctx context.Context, targetURL string, body TreePlantedJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)
}
// TreePlantedWithBody sends a POST callback request
// Tree planting result notification
func (p *CallbackInitiator) TreePlantedWithBody(ctx context.Context, targetURL string, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) {
req, err := NewTreePlantedCallbackRequestWithBody(targetURL, contentType, body)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
if err := p.applyEditors(ctx, req, reqEditors); err != nil {
return nil, err
}
return p.Client.Do(req)
}
// TreePlanted sends a POST callback request with application/json body
func (p *CallbackInitiator) TreePlanted(ctx context.Context, targetURL string, body TreePlantedJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) {
req, err := NewTreePlantedCallbackRequest(targetURL, body)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
if err := p.applyEditors(ctx, req, reqEditors); err != nil {
return nil, err
}
return p.Client.Do(req)
}
// NewTreePlantedCallbackRequest creates a POST request for the callback with application/json body
func NewTreePlantedCallbackRequest(targetURL string, body TreePlantedJSONRequestBody) (*http.Request, error) {
var bodyReader io.Reader
buf, err := json.Marshal(body)
if err != nil {
return nil, err
}
bodyReader = bytes.NewReader(buf)
return NewTreePlantedCallbackRequestWithBody(targetURL, "application/json", bodyReader)
}
// NewTreePlantedCallbackRequestWithBody creates a POST request for the callback with any body
func NewTreePlantedCallbackRequestWithBody(targetURL string, contentType string, body io.Reader) (*http.Request, error) {
var err error
reqURL, err := url.Parse(targetURL)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", reqURL.String(), body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", contentType)
return req, nil
}
// CallbackHttpError represents an HTTP error response from the callback.
// The type parameter E is the type of the parsed error body.
type CallbackHttpError[E any] struct {
StatusCode int
Body E
RawBody []byte
}
func (e *CallbackHttpError[E]) Error() string {
return fmt.Sprintf("HTTP %d", e.StatusCode)
}
// SimpleCallbackInitiator wraps CallbackInitiator with typed responses for operations that have
// unambiguous response types. Methods return the success type directly,
// and HTTP errors are returned as *CallbackHttpError[E] where E is the error type.
type SimpleCallbackInitiator struct {
*CallbackInitiator
}
// NewSimpleCallbackInitiator creates a new SimpleCallbackInitiator which wraps a CallbackInitiator.
func NewSimpleCallbackInitiator(opts ...CallbackInitiatorOption) (*SimpleCallbackInitiator, error) {
inner, err := NewCallbackInitiator(opts...)
if err != nil {
return nil, err
}
return &SimpleCallbackInitiator{CallbackInitiator: inner}, nil
}
// CallbackReceiverInterface represents handlers for receiving callback requests.
type CallbackReceiverInterface interface {
// Tree planting result notification
// HandleTreePlantedCallback handles the POST callback request.
HandleTreePlantedCallback(w http.ResponseWriter, r *http.Request)
}
// CallbackReceiverMiddlewareFunc is a middleware function for callback receiver handlers.
type CallbackReceiverMiddlewareFunc func(http.Handler) http.Handler
// TreePlantedCallbackHandler returns an http.Handler for the TreePlanted callback.
// The caller is responsible for registering this handler at the appropriate path.
func TreePlantedCallbackHandler(si CallbackReceiverInterface, errHandler func(w http.ResponseWriter, r *http.Request, err error), middlewares ...CallbackReceiverMiddlewareFunc) http.Handler {
if errHandler == nil {
errHandler = func(w http.ResponseWriter, r *http.Request, err error) {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
si.HandleTreePlantedCallback(w, r)
}))
for _, middleware := range middlewares {
handler = middleware(handler)
}
handler.ServeHTTP(w, r)
})
}
type UUID = uuid.UUID
// ---------------------------------------------------------------------------
// Deep object internals
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Exploded object binding
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Internal style helpers
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Deep object marshaling
// ---------------------------------------------------------------------------