forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.go
856 lines (776 loc) · 25.4 KB
/
schema.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
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scan
import (
"fmt"
"go/ast"
"reflect"
"regexp"
"strconv"
"strings"
"golang.org/x/tools/go/loader"
"github.com/go-swagger/go-swagger/spec"
)
type schemaTypable struct {
schema *spec.Schema
level int
}
func (st schemaTypable) Typed(tpe, format string) {
st.schema.Typed(tpe, format)
}
func (st schemaTypable) SetRef(ref spec.Ref) {
st.schema.Ref = ref
}
func (st schemaTypable) Schema() *spec.Schema {
return st.schema
}
func (st schemaTypable) Items() swaggerTypable {
if st.schema.Items == nil {
st.schema.Items = new(spec.SchemaOrArray)
}
if st.schema.Items.Schema == nil {
st.schema.Items.Schema = new(spec.Schema)
}
st.schema.Typed("array", "")
return schemaTypable{st.schema.Items.Schema, st.level + 1}
}
func (st schemaTypable) Level() int { return st.level }
type schemaValidations struct {
current *spec.Schema
}
func (sv schemaValidations) SetMaximum(val float64, exclusive bool) {
sv.current.Maximum = &val
sv.current.ExclusiveMaximum = exclusive
}
func (sv schemaValidations) SetMinimum(val float64, exclusive bool) {
sv.current.Minimum = &val
sv.current.ExclusiveMinimum = exclusive
}
func (sv schemaValidations) SetMultipleOf(val float64) { sv.current.MultipleOf = &val }
func (sv schemaValidations) SetMinItems(val int64) { sv.current.MinItems = &val }
func (sv schemaValidations) SetMaxItems(val int64) { sv.current.MaxItems = &val }
func (sv schemaValidations) SetMinLength(val int64) { sv.current.MinLength = &val }
func (sv schemaValidations) SetMaxLength(val int64) { sv.current.MaxLength = &val }
func (sv schemaValidations) SetPattern(val string) { sv.current.Pattern = val }
func (sv schemaValidations) SetUnique(val bool) { sv.current.UniqueItems = val }
func newSchemaAnnotationParser(goName string) *schemaAnnotationParser {
return &schemaAnnotationParser{GoName: goName, rx: rxModelOverride}
}
type schemaAnnotationParser struct {
GoName string
Name string
rx *regexp.Regexp
}
func (sap *schemaAnnotationParser) Matches(line string) bool {
return sap.rx.MatchString(line)
}
func (sap *schemaAnnotationParser) Parse(lines []string) error {
if sap.Name != "" {
return nil
}
if len(lines) > 0 {
for _, line := range lines {
matches := sap.rx.FindStringSubmatch(line)
if len(matches) > 1 && len(matches[1]) > 0 {
sap.Name = matches[1]
return nil
}
}
}
return nil
}
type schemaDecl struct {
File *ast.File
Decl *ast.GenDecl
TypeSpec *ast.TypeSpec
GoName string
Name string
annotated bool
}
func newSchemaDecl(file *ast.File, decl *ast.GenDecl, ts *ast.TypeSpec) *schemaDecl {
sd := &schemaDecl{
File: file,
Decl: decl,
TypeSpec: ts,
}
sd.inferNames()
return sd
}
func (sd *schemaDecl) hasAnnotation() bool {
sd.inferNames()
return sd.annotated
}
func (sd *schemaDecl) inferNames() (goName string, name string) {
if sd.GoName != "" {
goName, name = sd.GoName, sd.Name
return
}
goName = sd.TypeSpec.Name.Name
name = goName
if sd.Decl.Doc != nil {
DECLS:
for _, cmt := range sd.Decl.Doc.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
matches := rxModelOverride.FindStringSubmatch(ln)
if len(matches) > 0 {
sd.annotated = true
}
if len(matches) > 1 && len(matches[1]) > 0 {
name = matches[1]
break DECLS
}
}
}
}
sd.GoName = goName
sd.Name = name
return
}
type schemaParser struct {
program *loader.Program
postDecls []schemaDecl
known map[string]spec.Schema
}
func newSchemaParser(prog *loader.Program) *schemaParser {
scp := new(schemaParser)
scp.program = prog
scp.known = make(map[string]spec.Schema)
return scp
}
func (scp *schemaParser) Parse(gofile *ast.File, target interface{}) error {
tgt := target.(map[string]spec.Schema)
for _, decl := range gofile.Decls {
gd, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, spc := range gd.Specs {
if ts, ok := spc.(*ast.TypeSpec); ok {
sd := newSchemaDecl(gofile, gd, ts)
if err := scp.parseDecl(tgt, sd); err != nil {
return err
}
}
}
}
return nil
}
func (scp *schemaParser) parseDecl(definitions map[string]spec.Schema, decl *schemaDecl) error {
// check if there is a swagger:model tag that is followed by a word,
// this word is the type name for swagger
// the package and type are recorded in the extensions
// once type name is found convert it to a schema, by looking up the schema in the
// definitions dictionary that got passed into this parse method
decl.inferNames()
schema := definitions[decl.Name]
schPtr := &schema
// analyze doc comment for the model
sp := new(sectionedParser)
sp.setTitle = func(lines []string) { schema.Title = joinDropLast(lines) }
sp.setDescription = func(lines []string) { schema.Description = joinDropLast(lines) }
if err := sp.Parse(decl.Decl.Doc); err != nil {
return err
}
// analyze struct body for fields etc
// each exported struct field:
// * gets a type mapped to a go primitive
// * perhaps gets a format
// * has to document the validations that apply for the type and the field
// * when the struct field points to a model it becomes a ref: #/definitions/ModelName
// * the first line of the comment is the title
// * the following lines are the description
switch tpe := decl.TypeSpec.Type.(type) {
case *ast.StructType:
if err := scp.parseStructType(decl.File, schPtr, tpe, make(map[string]struct{})); err != nil {
return err
}
case *ast.InterfaceType:
if err := scp.parseInterfaceType(decl.File, schPtr, tpe, make(map[string]struct{})); err != nil {
return err
}
default:
}
if decl.Name != decl.GoName {
schPtr.AddExtension("x-go-name", decl.GoName)
}
for _, pkgInfo := range scp.program.AllPackages {
if pkgInfo.Importable {
for _, fil := range pkgInfo.Files {
if fil.Pos() == decl.File.Pos() {
schPtr.AddExtension("x-go-package", pkgInfo.Pkg.Path())
}
}
}
}
definitions[decl.Name] = schema
return nil
}
func (scp *schemaParser) parseEmbeddedType(gofile *ast.File, schema *spec.Schema, expr ast.Expr, seenPreviously map[string]struct{}) error {
switch tpe := expr.(type) {
case *ast.Ident:
// do lookup of type
// take primitives into account, they should result in an error for swagger
pkg, err := scp.packageForFile(gofile)
if err != nil {
return err
}
file, _, ts, err := findSourceFile(pkg, tpe.Name)
if err != nil {
return err
}
if st, ok := ts.Type.(*ast.StructType); ok {
return scp.parseStructType(file, schema, st, seenPreviously)
}
if st, ok := ts.Type.(*ast.InterfaceType); ok {
return scp.parseInterfaceType(file, schema, st, seenPreviously)
}
case *ast.SelectorExpr:
// look up package, file and then type
pkg, err := scp.packageForSelector(gofile, tpe.X)
if err != nil {
return fmt.Errorf("embedded struct: %v", err)
}
file, _, ts, err := findSourceFile(pkg, tpe.Sel.Name)
if err != nil {
return fmt.Errorf("embedded struct: %v", err)
}
if st, ok := ts.Type.(*ast.StructType); ok {
return scp.parseStructType(file, schema, st, seenPreviously)
}
if st, ok := ts.Type.(*ast.InterfaceType); ok {
return scp.parseInterfaceType(file, schema, st, seenPreviously)
}
}
return fmt.Errorf("unable to resolve embedded struct for: %v\n", expr)
}
func (scp *schemaParser) parseAllOfMember(gofile *ast.File, schema *spec.Schema, expr ast.Expr, seenPreviously map[string]struct{}) error {
// TODO: check if struct is annotated with swagger:model or known in the definitions otherwise
var pkg *loader.PackageInfo
var file *ast.File
var gd *ast.GenDecl
var ts *ast.TypeSpec
var err error
switch tpe := expr.(type) {
case *ast.Ident:
// do lookup of type
// take primitives into account, they should result in an error for swagger
pkg, err = scp.packageForFile(gofile)
if err != nil {
return err
}
file, gd, ts, err = findSourceFile(pkg, tpe.Name)
if err != nil {
return err
}
case *ast.SelectorExpr:
// look up package, file and then type
pkg, err = scp.packageForSelector(gofile, tpe.X)
if err != nil {
return fmt.Errorf("embedded struct: %v", err)
}
file, gd, ts, err = findSourceFile(pkg, tpe.Sel.Name)
if err != nil {
return fmt.Errorf("embedded struct: %v", err)
}
default:
return fmt.Errorf("unable to resolve allOf member for: %v\n", expr)
}
sd := newSchemaDecl(file, gd, ts)
if sd.hasAnnotation() {
ref, err := spec.NewRef("#/definitions/" + sd.Name)
if err != nil {
return err
}
schema.Ref = ref
scp.postDecls = append(scp.postDecls, *sd)
} else {
switch st := ts.Type.(type) {
case *ast.StructType:
return scp.parseStructType(file, schema, st, seenPreviously)
case *ast.InterfaceType:
return scp.parseInterfaceType(file, schema, st, seenPreviously)
}
}
return nil
}
func (scp *schemaParser) parseInterfaceType(gofile *ast.File, bschema *spec.Schema, tpe *ast.InterfaceType, seenPreviously map[string]struct{}) error {
if tpe.Methods == nil {
return nil
}
// first check if this has embedded interfaces, if so make sure to refer to those by ref
// when they are decorated with an allOf annotation
// go over the method list again and this time collect the nullary methods and parse the comments
// as if they are properties on a struct
var schema *spec.Schema
seenProperties := seenPreviously
hasAllOf := false
for _, fld := range tpe.Methods.List {
if len(fld.Names) == 0 {
// if this created an allOf property then we have to rejig the schema var
// because all the fields collected that aren't from embedded structs should go in
// their own proper schema
// first process embedded structs in order of embedding
if allOfMember(fld.Doc) {
hasAllOf = true
if schema == nil {
schema = new(spec.Schema)
}
var newSch spec.Schema
// when the embedded struct is annotated with swagger:allOf it will be used as allOf property
// otherwise the fields will just be included as normal properties
if err := scp.parseAllOfMember(gofile, &newSch, fld.Type, seenProperties); err != nil {
return err
}
if fld.Doc != nil {
for _, cmt := range fld.Doc.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
matches := rxAllOf.FindStringSubmatch(ln)
ml := len(matches)
if ml > 1 {
mv := matches[ml-1]
if mv != "" {
bschema.AddExtension("x-class", mv)
}
}
}
}
}
bschema.AllOf = append(bschema.AllOf, newSch)
continue
}
var newSch spec.Schema
// when the embedded struct is annotated with swagger:allOf it will be used as allOf property
// otherwise the fields will just be included as normal properties
if err := scp.parseEmbeddedType(gofile, &newSch, fld.Type, seenProperties); err != nil {
return err
}
bschema.AllOf = append(bschema.AllOf, newSch)
hasAllOf = true
}
}
if schema == nil {
schema = bschema
}
// then add and possibly override values
if schema.Properties == nil {
schema.Properties = make(map[string]spec.Schema)
}
schema.Typed("object", "")
for _, fld := range tpe.Methods.List {
if mtpe, ok := fld.Type.(*ast.FuncType); ok && mtpe.Params.NumFields() == 0 && mtpe.Results.NumFields() == 1 {
gnm := fld.Names[0].Name
nm := gnm
if fld.Doc != nil {
for _, cmt := range fld.Doc.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
matches := rxName.FindStringSubmatch(ln)
ml := len(matches)
if ml > 1 {
nm = matches[ml-1]
}
}
}
}
ps := schema.Properties[nm]
if err := parseProperty(scp, gofile, mtpe.Results.List[0].Type, schemaTypable{&ps, 0}); err != nil {
return err
}
if err := scp.createParser(nm, schema, &ps, fld).Parse(fld.Doc); err != nil {
return err
}
if nm != gnm {
ps.AddExtension("x-go-name", gnm)
}
seenProperties[nm] = struct{}{}
schema.Properties[nm] = ps
}
}
if schema != nil && hasAllOf {
bschema.AllOf = append(bschema.AllOf, *schema)
}
for k := range schema.Properties {
if _, ok := seenProperties[k]; !ok {
delete(schema.Properties, k)
}
}
return nil
}
func (scp *schemaParser) parseStructType(gofile *ast.File, bschema *spec.Schema, tpe *ast.StructType, seenPreviously map[string]struct{}) error {
if tpe.Fields == nil {
return nil
}
var schema *spec.Schema
seenProperties := seenPreviously
hasAllOf := false
for _, fld := range tpe.Fields.List {
if len(fld.Names) == 0 {
// if this created an allOf property then we have to rejig the schema var
// because all the fields collected that aren't from embedded structs should go in
// their own proper schema
// first process embedded structs in order of embedding
if allOfMember(fld.Doc) {
hasAllOf = true
if schema == nil {
schema = new(spec.Schema)
}
var newSch spec.Schema
// when the embedded struct is annotated with swagger:allOf it will be used as allOf property
// otherwise the fields will just be included as normal properties
if err := scp.parseAllOfMember(gofile, &newSch, fld.Type, seenProperties); err != nil {
return err
}
if fld.Doc != nil {
for _, cmt := range fld.Doc.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
matches := rxAllOf.FindStringSubmatch(ln)
ml := len(matches)
if ml > 1 {
mv := matches[ml-1]
if mv != "" {
bschema.AddExtension("x-class", mv)
}
}
}
}
}
bschema.AllOf = append(bschema.AllOf, newSch)
continue
}
if schema == nil {
schema = bschema
}
// when the embedded struct is annotated with swagger:allOf it will be used as allOf property
// otherwise the fields will just be included as normal properties
if err := scp.parseEmbeddedType(gofile, schema, fld.Type, seenProperties); err != nil {
return err
}
}
}
if schema == nil {
schema = bschema
}
// then add and possibly override values
if schema.Properties == nil {
schema.Properties = make(map[string]spec.Schema)
}
schema.Typed("object", "")
for _, fld := range tpe.Fields.List {
var tag string
if fld.Tag != nil {
val, err := strconv.Unquote(fld.Tag.Value)
if err == nil {
tag = reflect.StructTag(val).Get("json")
}
}
if len(fld.Names) > 0 && fld.Names[0] != nil && fld.Names[0].IsExported() && (tag == "" || tag[0] != '-') {
var nm, gnm string
nm = fld.Names[0].Name
gnm = nm
if fld.Tag != nil && len(strings.TrimSpace(fld.Tag.Value)) > 0 /*&& fld.Tag.Value[0] != '-'*/ {
tv, err := strconv.Unquote(fld.Tag.Value)
if err != nil {
return err
}
if strings.TrimSpace(tv) != "" {
st := reflect.StructTag(tv)
if st.Get("json") != "" {
nm = strings.Split(st.Get("json"), ",")[0]
}
}
}
ps := schema.Properties[nm]
if err := parseProperty(scp, gofile, fld.Type, schemaTypable{&ps, 0}); err != nil {
return err
}
if err := scp.createParser(nm, schema, &ps, fld).Parse(fld.Doc); err != nil {
return err
}
if nm != gnm {
ps.AddExtension("x-go-name", gnm)
}
seenProperties[nm] = struct{}{}
schema.Properties[nm] = ps
}
}
if schema != nil && hasAllOf {
bschema.AllOf = append(bschema.AllOf, *schema)
}
for k := range schema.Properties {
if _, ok := seenProperties[k]; !ok {
delete(schema.Properties, k)
}
}
return nil
}
func (scp *schemaParser) createParser(nm string, schema, ps *spec.Schema, fld *ast.Field) *sectionedParser {
sp := new(sectionedParser)
sp.setDescription = func(lines []string) { ps.Description = joinDropLast(lines) }
if ps.Ref.String() == "" {
sp.taggers = []tagParser{
newSingleLineTagParser("maximum", &setMaximum{schemaValidations{ps}, rxf(rxMaximumFmt, "")}),
newSingleLineTagParser("minimum", &setMinimum{schemaValidations{ps}, rxf(rxMinimumFmt, "")}),
newSingleLineTagParser("multipleOf", &setMultipleOf{schemaValidations{ps}, rxf(rxMultipleOfFmt, "")}),
newSingleLineTagParser("minLength", &setMinLength{schemaValidations{ps}, rxf(rxMinLengthFmt, "")}),
newSingleLineTagParser("maxLength", &setMaxLength{schemaValidations{ps}, rxf(rxMaxLengthFmt, "")}),
newSingleLineTagParser("pattern", &setPattern{schemaValidations{ps}, rxf(rxPatternFmt, "")}),
newSingleLineTagParser("minItems", &setMinItems{schemaValidations{ps}, rxf(rxMinItemsFmt, "")}),
newSingleLineTagParser("maxItems", &setMaxItems{schemaValidations{ps}, rxf(rxMaxItemsFmt, "")}),
newSingleLineTagParser("unique", &setUnique{schemaValidations{ps}, rxf(rxUniqueFmt, "")}),
newSingleLineTagParser("required", &setRequiredSchema{schema, nm}),
newSingleLineTagParser("readOnly", &setReadOnlySchema{ps}),
newSingleLineTagParser("discriminator", &setDiscriminator{schema, nm}),
}
itemsTaggers := func(items *spec.Schema, level int) []tagParser {
// the expression is 1-index based not 0-index
itemsPrefix := fmt.Sprintf(rxItemsPrefixFmt, level+1)
return []tagParser{
newSingleLineTagParser(fmt.Sprintf("items%dMaximum", level), &setMaximum{schemaValidations{items}, rxf(rxMaximumFmt, itemsPrefix)}),
newSingleLineTagParser(fmt.Sprintf("items%dMinimum", level), &setMinimum{schemaValidations{items}, rxf(rxMinimumFmt, itemsPrefix)}),
newSingleLineTagParser(fmt.Sprintf("items%dMultipleOf", level), &setMultipleOf{schemaValidations{items}, rxf(rxMultipleOfFmt, itemsPrefix)}),
newSingleLineTagParser(fmt.Sprintf("items%dMinLength", level), &setMinLength{schemaValidations{items}, rxf(rxMinLengthFmt, itemsPrefix)}),
newSingleLineTagParser(fmt.Sprintf("items%dMaxLength", level), &setMaxLength{schemaValidations{items}, rxf(rxMaxLengthFmt, itemsPrefix)}),
newSingleLineTagParser(fmt.Sprintf("items%dPattern", level), &setPattern{schemaValidations{items}, rxf(rxPatternFmt, itemsPrefix)}),
newSingleLineTagParser(fmt.Sprintf("items%dMinItems", level), &setMinItems{schemaValidations{items}, rxf(rxMinItemsFmt, itemsPrefix)}),
newSingleLineTagParser(fmt.Sprintf("items%dMaxItems", level), &setMaxItems{schemaValidations{items}, rxf(rxMaxItemsFmt, itemsPrefix)}),
newSingleLineTagParser(fmt.Sprintf("items%dUnique", level), &setUnique{schemaValidations{items}, rxf(rxUniqueFmt, itemsPrefix)}),
}
}
// check if this is a primitive, if so parse the validations from the
// doc comments of the slice declaration.
if ftped, ok := fld.Type.(*ast.ArrayType); ok {
ftpe := ftped
items, level := ps.Items, 0
for items != nil && items.Schema != nil {
switch iftpe := ftpe.Elt.(type) {
case *ast.ArrayType:
eleTaggers := itemsTaggers(items.Schema, level)
sp.taggers = append(eleTaggers, sp.taggers...)
ftpe = iftpe
case *ast.Ident:
if iftpe.Obj == nil {
sp.taggers = append(itemsTaggers(items.Schema, level), sp.taggers...)
}
break
//default:
//return fmt.Errorf("unknown field type (%T) ele for %q", iftpe, nm)
}
items = items.Schema.Items
level = level + 1
}
}
} else {
sp.taggers = []tagParser{
newSingleLineTagParser("required", &setRequiredSchema{schema, nm}),
}
}
return sp
}
func (scp *schemaParser) packageForFile(gofile *ast.File) (*loader.PackageInfo, error) {
for pkg, pkgInfo := range scp.program.AllPackages {
if pkg.Name() == gofile.Name.Name {
return pkgInfo, nil
}
}
fn := scp.program.Fset.File(gofile.Pos()).Name()
return nil, fmt.Errorf("unable to determine package for %s", fn)
}
func (scp *schemaParser) packageForSelector(gofile *ast.File, expr ast.Expr) (*loader.PackageInfo, error) {
if pth, ok := expr.(*ast.Ident); ok {
// lookup import
var selPath string
for _, imp := range gofile.Imports {
pv, err := strconv.Unquote(imp.Path.Value)
if err != nil {
pv = imp.Path.Value
}
if imp.Name != nil {
if imp.Name.Name == pth.Name {
selPath = pv
break
}
} else {
parts := strings.Split(pv, "/")
if len(parts) > 0 && parts[len(parts)-1] == pth.Name {
selPath = pv
break
}
}
}
// find actual struct
if selPath == "" {
return nil, fmt.Errorf("no import found for %s", pth.Name)
}
pkg := scp.program.Package(selPath)
if pkg == nil {
return nil, fmt.Errorf("no package found for %s", selPath)
}
return pkg, nil
}
return nil, fmt.Errorf("can't determine selector path from %v", expr)
}
func (scp *schemaParser) parseIdentProperty(pkg *loader.PackageInfo, expr *ast.Ident, prop swaggerTypable) error {
// find the file this selector points to
file, gd, ts, err := findSourceFile(pkg, expr.Name)
if err != nil {
return swaggerSchemaForType(expr.Name, prop)
}
if at, ok := ts.Type.(*ast.ArrayType); ok {
// the swagger spec defines strfmt base64 as []byte.
// in that case we don't actually want to turn it into an array
// but we want to turn it into a string
if _, ok := at.Elt.(*ast.Ident); ok {
if strfmtName, ok := strfmtName(gd.Doc); ok {
prop.Typed("string", strfmtName)
return nil
}
}
// this is a selector, so most likely not base64
if strfmtName, ok := strfmtName(gd.Doc); ok {
prop.Items().Typed("string", strfmtName)
return nil
}
}
// look at doc comments for swagger:strfmt [name]
// when found this is the format name, create a schema with that name
if strfmtName, ok := strfmtName(gd.Doc); ok {
prop.Typed("string", strfmtName)
return nil
}
switch tpe := ts.Type.(type) {
case *ast.ArrayType:
switch atpe := tpe.Elt.(type) {
case *ast.Ident:
return scp.parseIdentProperty(pkg, atpe, prop.Items())
case *ast.SelectorExpr:
return scp.typeForSelector(file, atpe, prop.Items())
default:
return fmt.Errorf("unknown selector type: %#v", tpe)
}
case *ast.StructType:
sd := newSchemaDecl(file, gd, ts)
sd.inferNames()
ref, err := spec.NewRef("#/definitions/" + sd.Name)
if err != nil {
return err
}
prop.SetRef(ref)
scp.postDecls = append(scp.postDecls, *sd)
return nil
case *ast.Ident:
return scp.parseIdentProperty(pkg, tpe, prop)
case *ast.SelectorExpr:
return scp.typeForSelector(file, tpe, prop)
default:
return swaggerSchemaForType(expr.Name, prop)
}
}
func (scp *schemaParser) typeForSelector(gofile *ast.File, expr *ast.SelectorExpr, prop swaggerTypable) error {
pkg, err := scp.packageForSelector(gofile, expr.X)
if err != nil {
return err
}
return scp.parseIdentProperty(pkg, expr.Sel, prop)
}
func findSourceFile(pkg *loader.PackageInfo, typeName string) (*ast.File, *ast.GenDecl, *ast.TypeSpec, error) {
for _, file := range pkg.Files {
for _, decl := range file.Decls {
if gd, ok := decl.(*ast.GenDecl); ok {
for _, gs := range gd.Specs {
if ts, ok := gs.(*ast.TypeSpec); ok {
strfmtNme, isStrfmt := strfmtName(gd.Doc)
if (isStrfmt && strfmtNme == typeName) || ts.Name != nil && ts.Name.Name == typeName {
return file, gd, ts, nil
}
}
}
}
}
}
return nil, nil, nil, fmt.Errorf("unable to find %s in %s", typeName, pkg.String())
}
func allOfMember(comments *ast.CommentGroup) bool {
if comments != nil {
for _, cmt := range comments.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
if rxAllOf.MatchString(ln) {
return true
}
}
}
}
return false
}
func strfmtName(comments *ast.CommentGroup) (string, bool) {
if comments != nil {
for _, cmt := range comments.List {
for _, ln := range strings.Split(cmt.Text, "\n") {
matches := rxStrFmt.FindStringSubmatch(ln)
if len(matches) > 1 && len(strings.TrimSpace(matches[1])) > 0 {
return strings.TrimSpace(matches[1]), true
}
}
}
}
return "", false
}
func parseProperty(scp *schemaParser, gofile *ast.File, fld ast.Expr, prop swaggerTypable) error {
switch ftpe := fld.(type) {
case *ast.Ident: // simple value
pkg, err := scp.packageForFile(gofile)
if err != nil {
return err
}
return scp.parseIdentProperty(pkg, ftpe, prop)
case *ast.StarExpr: // pointer to something, optional by default
parseProperty(scp, gofile, ftpe.X, prop)
case *ast.ArrayType: // slice type
if err := parseProperty(scp, gofile, ftpe.Elt, prop.Items()); err != nil {
return err
}
case *ast.StructType:
schema := prop.Schema()
if schema == nil {
return fmt.Errorf("items doesn't support embedded structs")
}
return scp.parseStructType(gofile, prop.Schema(), ftpe, make(map[string]struct{}))
case *ast.SelectorExpr:
err := scp.typeForSelector(gofile, ftpe, prop)
return err
case *ast.MapType:
// check if key is a string type, if not print a message
// and skip the map property. Only maps with string keys can go into additional properties
sch := prop.Schema()
if sch == nil {
return fmt.Errorf("items doesn't support maps")
}
if keyIdent, ok := ftpe.Key.(*ast.Ident); sch != nil && ok {
if keyIdent.Name == "string" {
if sch.AdditionalProperties == nil {
sch.AdditionalProperties = new(spec.SchemaOrBool)
}
sch.AdditionalProperties.Allows = false
if sch.AdditionalProperties.Schema == nil {
sch.AdditionalProperties.Schema = new(spec.Schema)
}
parseProperty(scp, gofile, ftpe.Value, schemaTypable{sch.AdditionalProperties.Schema, 0})
sch.Typed("object", "")
}
}
case *ast.InterfaceType:
prop.Schema().Typed("object", "")
default:
return fmt.Errorf("%s is unsupported for a schema", ftpe)
}
return nil
}