forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsl.go
1096 lines (915 loc) · 26.3 KB
/
dsl.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ogen
import (
"encoding/json"
"strings"
"github.com/go-faster/jx"
"golang.org/x/exp/slices"
"github.com/ogen-go/ogen/jsonschema"
"github.com/ogen-go/ogen/openapi"
)
// NewSpec returns a new Spec.
func NewSpec() *Spec {
return new(Spec)
}
// SetOpenAPI sets the OpenAPI Specification version of the document.
func (s *Spec) SetOpenAPI(v string) *Spec {
s.OpenAPI = v
return s
}
// SetInfo sets the Info of the Spec.
func (s *Spec) SetInfo(i *Info) *Spec {
if i != nil {
s.Info = *i
}
return s
}
// SetServers sets the Servers of the Spec.
func (s *Spec) SetServers(srvs []Server) *Spec {
s.Servers = slices.Clone(srvs)
return s
}
// AddServers adds Servers to the Servers of the Spec.
func (s *Spec) AddServers(srvs ...*Server) *Spec {
for _, srv := range srvs {
if srv != nil {
s.Servers = append(s.Servers, *srv)
}
}
return s
}
// SetPaths sets the Paths of the Spec.
func (s *Spec) SetPaths(p Paths) *Spec {
s.Paths = p
return s
}
// AddPathItem adds the given PathItem under the given Name to the Paths of the Spec.
func (s *Spec) AddPathItem(n string, p *PathItem) *Spec {
s.initPaths()
s.Paths[n] = p
return s
}
// AddNamedPathItems adds the given namedPaths to the Paths of the Spec.
func (s *Spec) AddNamedPathItems(ps ...*NamedPathItem) *Spec {
for _, p := range ps {
s.AddPathItem(p.Name, p.PathItem)
}
return s
}
// SetComponents sets the Components of the Spec.
func (s *Spec) SetComponents(c *Components) *Spec {
s.Components = c
return s
}
// initPaths ensures the Paths map is allocated.
func (s *Spec) initPaths() {
if s.Paths == nil {
s.Paths = make(Paths)
}
}
// AddSchema adds the given Schema under the given Name to the Components of the Spec.
func (s *Spec) AddSchema(n string, sc *Schema) *Spec {
s.initSchemas()
s.Components.Schemas[n] = sc
return s
}
// AddNamedSchemas adds the given namedSchemas to the Components of the Spec.
func (s *Spec) AddNamedSchemas(scs ...*NamedSchema) *Spec {
for _, sc := range scs {
s.AddSchema(sc.Name, sc.Schema)
}
return s
}
// AddResponse adds the given Response under the given Name to the Components of the Spec.
func (s *Spec) AddResponse(n string, sc *Response) *Spec {
s.initResponses()
s.Components.Responses[n] = sc
return s
}
// AddNamedResponses adds the given namedResponses to the Components of the Spec.
func (s *Spec) AddNamedResponses(scs ...*NamedResponse) *Spec {
for _, sc := range scs {
s.AddResponse(sc.Name, sc.Response)
}
return s
}
// AddParameter adds the given Parameter under the given Name to the Components of the Spec.
func (s *Spec) AddParameter(n string, p *Parameter) *Spec {
s.initParameters()
s.Components.Parameters[n] = p
return s
}
// AddNamedParameters adds the given namedParameters to the Components of the Spec.
func (s *Spec) AddNamedParameters(ps ...*NamedParameter) *Spec {
for _, p := range ps {
s.AddParameter(p.Name, p.Parameter)
}
return s
}
// AddRequestBody adds the given RequestBody under the given Name to the Components of the Spec.
func (s *Spec) AddRequestBody(n string, sc *RequestBody) *Spec {
s.initRequestBodies()
s.Components.RequestBodies[n] = sc
return s
}
// AddNamedRequestBodies adds the given namedRequestBodies to the Components of the Spec.
func (s *Spec) AddNamedRequestBodies(scs ...*NamedRequestBody) *Spec {
for _, sc := range scs {
s.AddRequestBody(sc.Name, sc.RequestBody)
}
return s
}
// RefSchema returns a new Schema referencing the given name.
func (s *Spec) RefSchema(n string) *NamedSchema {
if s.Components != nil && s.Components.Schemas != nil {
if r, ok := s.Components.Schemas[n]; ok {
return NewNamedSchema(n, r).AsLocalRef().ToNamed(n)
}
}
return nil
}
// RefResponse returns a new Response referencing the given name.
func (s *Spec) RefResponse(n string) *NamedResponse {
if s.Components != nil && s.Components.Responses != nil {
if r, ok := s.Components.Responses[n]; ok {
return NewNamedResponse(n, r).AsLocalRef().ToNamed(n)
}
}
return nil
}
// RefRequestBody returns a new RequestBody referencing the given name.
func (s *Spec) RefRequestBody(n string) *NamedRequestBody {
if s.Components != nil && s.Components.RequestBodies != nil {
if r, ok := s.Components.RequestBodies[n]; ok {
return NewNamedRequestBody(n, r).AsLocalRef().ToNamed(n)
}
}
return nil
}
// initComponents ensures the Components property is non-nil.
func (s *Spec) initComponents() {
if s.Components == nil {
s.Components = new(Components)
}
}
// initParameters ensures the Parameters map is allocated.
func (s *Spec) initParameters() {
s.initComponents()
if s.Components.Parameters == nil {
s.Components.Parameters = make(map[string]*Parameter)
}
}
// initSchemas ensures the Schemas map is allocated.
func (s *Spec) initSchemas() {
s.initComponents()
if s.Components.Schemas == nil {
s.Components.Schemas = make(map[string]*Schema)
}
}
// initResponses ensures the Responses map is allocated.
func (s *Spec) initResponses() {
s.initComponents()
if s.Components.Responses == nil {
s.Components.Responses = make(map[string]*Response)
}
}
// initRequestBodies ensures the RequestBodies map is allocated.
func (s *Spec) initRequestBodies() {
s.initComponents()
if s.Components.RequestBodies == nil {
s.Components.RequestBodies = make(map[string]*RequestBody)
}
}
// NewRequestBody returns a new RequestBody.
func NewRequestBody() *RequestBody {
return new(RequestBody)
}
// SetRef sets the Ref of the RequestBody.
func (r *RequestBody) SetRef(ref string) *RequestBody {
r.Ref = ref
return r
}
// SetDescription sets the Description of the RequestBody.
func (r *RequestBody) SetDescription(d string) *RequestBody {
r.Description = d
return r
}
// SetContent sets the Content of the RequestBody.
func (r *RequestBody) SetContent(c map[string]Media) *RequestBody {
r.Content = c
return r
}
// AddContent adds the given Schema under the MediaType to the Content of the Response.
func (r *RequestBody) AddContent(mt string, s *Schema) *RequestBody {
if s != nil {
r.initContent()
r.Content[mt] = Media{Schema: s}
}
return r
}
// SetJSONContent sets the given Schema under the JSON MediaType to the Content of the Response.
func (r *RequestBody) SetJSONContent(s *Schema) *RequestBody {
return r.AddContent("application/json", s)
}
// initContent ensures the Content map is allocated.
func (r *RequestBody) initContent() {
if r.Content == nil {
r.Content = make(map[string]Media)
}
}
// SetRequired sets the Required of the RequestBody.
func (r *RequestBody) SetRequired(req bool) *RequestBody {
r.Required = req
return r
}
// ToNamed returns a NamedRequestBody wrapping the receiver.
func (r *RequestBody) ToNamed(n string) *NamedRequestBody {
return NewNamedRequestBody(n, r)
}
// NamedRequestBody can be used to construct a reference to the wrapped RequestBody.
type NamedRequestBody struct {
RequestBody *RequestBody
Name string
}
// NewNamedRequestBody returns a new NamedRequestBody.
func NewNamedRequestBody(n string, p *RequestBody) *NamedRequestBody {
return &NamedRequestBody{p, n}
}
// AsLocalRef returns a new RequestBody referencing the wrapped RequestBody in the local document.
func (p *NamedRequestBody) AsLocalRef() *RequestBody {
return NewRequestBody().SetRef("#/components/requestBodies/" + escapeRef(p.Name))
}
// NewInfo returns a new Info.
func NewInfo() *Info {
return new(Info)
}
// SetTitle sets the title of the Info.
func (i *Info) SetTitle(t string) *Info {
i.Title = t
return i
}
// SetDescription sets the description of the Info.
func (i *Info) SetDescription(d string) *Info {
i.Description = d
return i
}
// SetTermsOfService sets the terms of service of the Info.
func (i *Info) SetTermsOfService(t string) *Info {
i.TermsOfService = t
return i
}
// SetContact sets the Contact of the Info.
func (i *Info) SetContact(c *Contact) *Info {
i.Contact = c
return i
}
// SetLicense sets the License of the Info.
func (i *Info) SetLicense(l *License) *Info {
i.License = l
return i
}
// SetVersion sets the version of the Info.
func (i *Info) SetVersion(v string) *Info {
i.Version = v
return i
}
// NewContact returns a new Contact.
func NewContact() *Contact {
return new(Contact)
}
// SetName sets the Name of the Contact.
func (c *Contact) SetName(n string) *Contact {
c.Name = n
return c
}
// SetURL sets the URL of the Contact.
func (c *Contact) SetURL(url string) *Contact {
c.URL = url
return c
}
// SetEmail sets the Email of the Contact.
func (c *Contact) SetEmail(e string) *Contact {
c.Email = e
return c
}
// NewLicense returns a new License.
func NewLicense() *License {
return new(License)
}
// SetName sets the Name of the License.
func (l *License) SetName(n string) *License {
l.Name = n
return l
}
// SetURL sets the URL of the License.
func (l *License) SetURL(url string) *License {
l.URL = url
return l
}
// NewServer returns a new Server.
func NewServer() *Server {
return new(Server)
}
// SetDescription sets the Description of the Server.
func (s *Server) SetDescription(d string) *Server {
s.Description = d
return s
}
// SetURL sets the URL of the Server.
func (s *Server) SetURL(url string) *Server {
s.URL = url
return s
}
// NewPathItem returns a new PathItem.
func NewPathItem() *PathItem {
return new(PathItem)
}
// SetRef sets the Ref of the PathItem.
func (p *PathItem) SetRef(r string) *PathItem {
p.Ref = r
return p
}
// SetDescription sets the Description of the PathItem.
func (p *PathItem) SetDescription(d string) *PathItem {
p.Description = d
return p
}
// SetGet sets the Get of the PathItem.
func (p *PathItem) SetGet(o *Operation) *PathItem {
p.Get = o
return p
}
// SetPut sets the Put of the PathItem.
func (p *PathItem) SetPut(o *Operation) *PathItem {
p.Put = o
return p
}
// SetPost sets the Post of the PathItem.
func (p *PathItem) SetPost(o *Operation) *PathItem {
p.Post = o
return p
}
// SetDelete sets the Delete of the PathItem.
func (p *PathItem) SetDelete(o *Operation) *PathItem {
p.Delete = o
return p
}
// SetOptions sets the Options of the PathItem.
func (p *PathItem) SetOptions(o *Operation) *PathItem {
p.Options = o
return p
}
// SetHead sets the Head of the PathItem.
func (p *PathItem) SetHead(o *Operation) *PathItem {
p.Head = o
return p
}
// SetPatch sets the Patch of the PathItem.
func (p *PathItem) SetPatch(o *Operation) *PathItem {
p.Patch = o
return p
}
// SetTrace sets the Trace of the PathItem.
func (p *PathItem) SetTrace(o *Operation) *PathItem {
p.Trace = o
return p
}
// SetServers sets the Servers of the PathItem.
func (p *PathItem) SetServers(srvs []Server) *PathItem {
p.Servers = slices.Clone(srvs)
return p
}
// AddServers adds Servers to the Servers of the PathItem.
func (p *PathItem) AddServers(srvs ...*Server) *PathItem {
for _, srv := range srvs {
if srv != nil {
p.Servers = append(p.Servers, *srv)
}
}
return p
}
// SetParameters sets the Parameters of the PathItem.
func (p *PathItem) SetParameters(ps []*Parameter) *PathItem {
p.Parameters = slices.Clone(ps)
return p
}
// AddParameters adds Parameters to the Parameters of the PathItem.
func (p *PathItem) AddParameters(ps ...*Parameter) *PathItem {
p.Parameters = append(p.Parameters, ps...)
return p
}
// ToNamed returns a NamedPathItem wrapping the receiver.
func (p *PathItem) ToNamed(n string) *NamedPathItem {
return NewNamedPathItem(n, p)
}
// NamedPathItem can be used to construct a reference to the wrapped PathItem.
type NamedPathItem struct {
PathItem *PathItem
Name string
}
// NewNamedPathItem returns a new NamedPathItem.
func NewNamedPathItem(n string, p *PathItem) *NamedPathItem {
return &NamedPathItem{p, n}
}
// AsLocalRef returns a new PathItem referencing the wrapped PathItem in the local document.
func (p *NamedPathItem) AsLocalRef() *PathItem {
return NewPathItem().SetRef("#/paths/" + escapeRef(p.Name))
}
// NewOperation returns a new Operation.
func NewOperation() *Operation {
return new(Operation)
}
// SetTags sets the Tags of the Operation.
func (o *Operation) SetTags(ts []string) *Operation {
o.Tags = slices.Clone(ts)
return o
}
// AddTags adds Tags to the Tags of the Operation.
func (o *Operation) AddTags(ts ...string) *Operation {
o.Tags = append(o.Tags, ts...)
return o
}
// SetSummary sets the Summary of the Operation.
func (o *Operation) SetSummary(s string) *Operation {
o.Summary = s
return o
}
// SetDescription sets the Description of the Operation.
func (o *Operation) SetDescription(d string) *Operation {
o.Description = d
return o
}
// SetOperationID sets the OperationID of the Operation.
func (o *Operation) SetOperationID(id string) *Operation {
o.OperationID = id
return o
}
// SetParameters sets the Parameters of the Operation.
func (o *Operation) SetParameters(ps []*Parameter) *Operation {
o.Parameters = slices.Clone(ps)
return o
}
// AddParameters adds Parameters to the Parameters of the Operation.
func (o *Operation) AddParameters(ps ...*Parameter) *Operation {
o.Parameters = append(o.Parameters, ps...)
return o
}
// SetRequestBody sets the RequestBody of the Operation.
func (o *Operation) SetRequestBody(r *RequestBody) *Operation {
o.RequestBody = r
return o
}
// SetResponses sets the Responses of the Operation.
func (o *Operation) SetResponses(r Responses) *Operation {
o.Responses = r
return o
}
// AddResponse adds the given Response under the given Name to the Responses of the Operation.
func (o *Operation) AddResponse(n string, p *Response) *Operation {
o.initResponses()
o.Responses[n] = p
return o
}
// AddNamedResponses adds the given namedResponses to the Responses of the Operation.
func (o *Operation) AddNamedResponses(ps ...*NamedResponse) *Operation {
for _, p := range ps {
o.AddResponse(p.Name, p.Response)
}
return o
}
// initResponses ensures the Responses map is allocated.
func (o *Operation) initResponses() {
if o.Responses == nil {
o.Responses = make(Responses)
}
}
// NewParameter returns a new Parameter.
func NewParameter() *Parameter {
return new(Parameter)
}
// SetRef sets the Ref of the Parameter.
func (p *Parameter) SetRef(r string) *Parameter {
p.Ref = r
return p
}
// SetName sets the Name of the Parameter.
func (p *Parameter) SetName(n string) *Parameter {
p.Name = n
return p
}
// SetIn sets the In of the Parameter.
func (p *Parameter) SetIn(i string) *Parameter {
p.In = i
return p
}
// InPath sets the In of the Parameter to "path".
func (p *Parameter) InPath() *Parameter {
return p.SetIn(openapi.LocationPath.String())
}
// InQuery sets the In of the Parameter to "query".
func (p *Parameter) InQuery() *Parameter {
return p.SetIn(openapi.LocationQuery.String())
}
// InHeader sets the In of the Parameter to "header".
func (p *Parameter) InHeader() *Parameter {
return p.SetIn(openapi.LocationHeader.String())
}
// InCookie sets the In of the Parameter to "cookie".
func (p *Parameter) InCookie() *Parameter {
return p.SetIn(openapi.LocationCookie.String())
}
// SetDescription sets the Description of the Parameter.
func (p *Parameter) SetDescription(d string) *Parameter {
p.Description = d
return p
}
// SetSchema sets the Schema of the Parameter.
func (p *Parameter) SetSchema(s *Schema) *Parameter {
if s != nil {
p.Schema = s
}
return p
}
// SetRequired sets the Required of the Parameter.
func (p *Parameter) SetRequired(r bool) *Parameter {
p.Required = r
return p
}
// SetDeprecated sets the Deprecated of the Parameter.
func (p *Parameter) SetDeprecated(d bool) *Parameter {
p.Deprecated = d
return p
}
// SetContent sets the Content of the Parameter.
func (p *Parameter) SetContent(c map[string]Media) *Parameter {
p.Content = c
return p
}
// TODO(masseelch): Add Content helpers for Parameter
// SetStyle sets the Style of the Parameter.
func (p *Parameter) SetStyle(s string) *Parameter {
p.Style = s
return p
}
// SetExplode sets the Explode of the Parameter.
func (p *Parameter) SetExplode(e bool) *Parameter {
p.Explode = &e
return p
}
// ToNamed returns a NamedParameter wrapping the receiver.
func (p *Parameter) ToNamed(n string) *NamedParameter {
return NewNamedParameter(n, p)
}
// NamedParameter can be used to construct a reference to the wrapped Parameter.
type NamedParameter struct {
Parameter *Parameter
Name string
}
// NewNamedParameter returns a new NamedParameter.
func NewNamedParameter(n string, p *Parameter) *NamedParameter {
return &NamedParameter{p, n}
}
// AsLocalRef returns a new Parameter referencing the wrapped Parameter in the local document.
func (p *NamedParameter) AsLocalRef() *Parameter {
return NewParameter().SetRef("#/components/parameters/" + escapeRef(p.Name))
}
// NewResponse returns a new Response.
func NewResponse() *Response {
return new(Response)
}
// SetRef sets the Ref of the Response.
func (r *Response) SetRef(ref string) *Response {
r.Ref = ref
return r
}
// SetDescription sets the Description of the Response.
func (r *Response) SetDescription(d string) *Response {
r.Description = d
return r
}
// SetHeaders sets the Headers of the Response.
func (r *Response) SetHeaders(h map[string]*Header) *Response {
r.Headers = h
return r
}
// SetContent sets the Content of the Response.
func (r *Response) SetContent(c map[string]Media) *Response {
r.Content = c
return r
}
// AddContent adds the given Schema under the MediaType to the Content of the Response.
func (r *Response) AddContent(mt string, s *Schema) *Response {
if s != nil {
r.initContent()
r.Content[mt] = Media{Schema: s}
}
return r
}
// SetJSONContent sets the given Schema under the JSON MediaType to the Content of the Response.
func (r *Response) SetJSONContent(s *Schema) *Response {
return r.AddContent("application/json", s)
}
// initContent ensures the Content map is allocated.
func (r *Response) initContent() {
if r.Content == nil {
r.Content = make(map[string]Media)
}
}
// SetLinks sets the Links of the Response.
func (r *Response) SetLinks(l map[string]*Link) *Response {
r.Links = l
return r
}
// ToNamed returns a NamedResponse wrapping the receiver.
func (r *Response) ToNamed(n string) *NamedResponse {
return NewNamedResponse(n, r)
}
// NamedResponse can be used to construct a reference to the wrapped Response.
type NamedResponse struct {
Response *Response
Name string
}
// NewNamedResponse returns a new NamedResponse.
func NewNamedResponse(n string, p *Response) *NamedResponse {
return &NamedResponse{p, n}
}
// AsLocalRef returns a new Response referencing the wrapped Response in the local document.
func (p *NamedResponse) AsLocalRef() *Response {
return NewResponse().SetRef("#/components/responses/" + escapeRef(p.Name))
}
// TODO(masseelch): Discriminator
// NewSchema returns a new Schema.
func NewSchema() *Schema {
return new(Schema)
}
// SetRef sets the Ref of the Schema.
func (s *Schema) SetRef(r string) *Schema {
s.Ref = r
return s
}
// SetSummary sets the Summary of the Schema.
func (s *Schema) SetSummary(smry string) *Schema {
s.Summary = smry
return s
}
// SetDescription sets the Description of the Schema.
func (s *Schema) SetDescription(d string) *Schema {
s.Description = d
return s
}
// SetType sets the Type of the Schema.
func (s *Schema) SetType(t string) *Schema {
s.Type = t
return s
}
// SetFormat sets the Format of the Schema.
func (s *Schema) SetFormat(f string) *Schema {
s.Format = f
return s
}
// SetProperties sets the Properties of the Schema.
func (s *Schema) SetProperties(p *Properties) *Schema {
s.SetType("object")
if p != nil {
s.Properties = *p
}
return s
}
// AddOptionalProperties adds the Properties to the Properties of the Schema.
func (s *Schema) AddOptionalProperties(ps ...*Property) *Schema {
s.SetType("object")
for _, p := range ps {
if p != nil {
s.Properties = append(s.Properties, *p)
}
}
return s
}
// AddRequiredProperties adds the Properties to the Properties of the Schema and marks them as required.
func (s *Schema) AddRequiredProperties(ps ...*Property) *Schema {
s.AddOptionalProperties(ps...)
for _, p := range ps {
if p != nil {
s.Required = append(s.Required, p.Name)
}
}
return s
}
// SetRequired sets the Required of the Schema.
func (s *Schema) SetRequired(r []string) *Schema {
s.Required = slices.Clone(r)
return s
}
// SetItems sets the Items of the Schema.
func (s *Schema) SetItems(i *Schema) *Schema {
s.Items = &Items{
Item: i,
}
return s
}
// SetNullable sets the Nullable of the Schema.
func (s *Schema) SetNullable(n bool) *Schema {
s.Nullable = n
return s
}
// SetAllOf sets the AllOf of the Schema.
func (s *Schema) SetAllOf(a []*Schema) *Schema {
s.AllOf = slices.Clone(a)
return s
}
// SetOneOf sets the OneOf of the Schema.
func (s *Schema) SetOneOf(o []*Schema) *Schema {
s.OneOf = slices.Clone(o)
return s
}
// SetAnyOf sets the AnyOf of the Schema.
func (s *Schema) SetAnyOf(a []*Schema) *Schema {
s.AnyOf = slices.Clone(a)
return s
}
// SetDiscriminator sets the Discriminator of the Schema.
func (s *Schema) SetDiscriminator(d *Discriminator) *Schema {
s.Discriminator = d
return s
}
// SetEnum sets the Enum of the Schema.
func (s *Schema) SetEnum(e []json.RawMessage) *Schema {
for _, val := range e {
s.Enum = append(s.Enum, val)
}
return s
}
// SetMultipleOf sets the MultipleOf of the Schema.
func (s *Schema) SetMultipleOf(m *uint64) *Schema {
if m != nil {
val := *m
e := &jx.Encoder{}
e.UInt64(val)
s.MultipleOf = e.Bytes()
}
return s
}
// SetMaximum sets the Maximum of the Schema.
func (s *Schema) SetMaximum(m *int64) *Schema {
if m != nil {
val := *m
e := &jx.Encoder{}
e.Int64(val)
s.Maximum = e.Bytes()
}
return s
}
// SetExclusiveMaximum sets the ExclusiveMaximum of the Schema.
func (s *Schema) SetExclusiveMaximum(e bool) *Schema {
s.ExclusiveMaximum = e
return s
}
// SetMinimum sets the Minimum of the Schema.
func (s *Schema) SetMinimum(m *int64) *Schema {
if m != nil {
val := *m
e := &jx.Encoder{}
e.Int64(val)
s.Minimum = e.Bytes()
}
return s
}
// SetExclusiveMinimum sets the ExclusiveMinimum of the Schema.
func (s *Schema) SetExclusiveMinimum(e bool) *Schema {
s.ExclusiveMinimum = e
return s
}
// SetMaxLength sets the MaxLength of the Schema.
func (s *Schema) SetMaxLength(m *uint64) *Schema {
s.MaxLength = m
return s
}
// SetMinLength sets the MinLength of the Schema.
func (s *Schema) SetMinLength(m *uint64) *Schema {
s.MinLength = m
return s
}
// SetPattern sets the Pattern of the Schema.
func (s *Schema) SetPattern(p string) *Schema {
s.Pattern = p
return s
}
// SetMaxItems sets the MaxItems of the Schema.
func (s *Schema) SetMaxItems(m *uint64) *Schema {
s.MaxItems = m
return s
}
// SetMinItems sets the MinItems of the Schema.
func (s *Schema) SetMinItems(m *uint64) *Schema {
s.MinItems = m
return s
}
// SetUniqueItems sets the UniqueItems of the Schema.
func (s *Schema) SetUniqueItems(u bool) *Schema {
s.UniqueItems = u
return s
}
// SetMaxProperties sets the MaxProperties of the Schema.
func (s *Schema) SetMaxProperties(m *uint64) *Schema {
s.MaxProperties = m
return s
}
// SetMinProperties sets the MinProperties of the Schema.
func (s *Schema) SetMinProperties(m *uint64) *Schema {
s.MinProperties = m
return s
}
// SetDefault sets the Default of the Schema.
func (s *Schema) SetDefault(d json.RawMessage) *Schema {
s.Default = Default(d)
return s
}
// SetDeprecated sets the Deprecated of the Schema.
func (s *Schema) SetDeprecated(d bool) *Schema {
s.Deprecated = d
return s
}
// ToNamed returns a NamedSchema wrapping the receiver.
func (s *Schema) ToNamed(n string) *NamedSchema {
return NewNamedSchema(n, s)
}
// Int returns an integer OAS data type (Schema).
func Int() *Schema { return schema("integer", "") }
// Int32 returns an 32-bit integer OAS data type (Schema).
func Int32() *Schema { return schema("integer", "int32") }
// Int64 returns an 64-bit integer OAS data type (Schema).
func Int64() *Schema { return schema("integer", "int64") }