-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathplugin.go
952 lines (819 loc) · 21.2 KB
/
plugin.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
package swagger
import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"sort"
"strconv"
"time"
"github.com/favclip/ucon"
)
// interface compatibility check
var _ ucon.HandlersScannerPlugin = &Plugin{}
var _ ucon.Context = &HandlerInfo{}
var _ ucon.HandlerContainer = &HandlerInfo{}
type swaggerOperationKey struct{}
var httpReqType = reflect.TypeOf(&http.Request{})
var httpRespType = reflect.TypeOf((*http.ResponseWriter)(nil)).Elem()
var netContextType = reflect.TypeOf((*context.Context)(nil)).Elem()
var errorType = reflect.TypeOf((*error)(nil)).Elem()
var uconHTTPErrorType = reflect.TypeOf((*ucon.HTTPErrorResponse)(nil)).Elem()
// DefaultTypeSchemaMapper is used for mapping from go-type to swagger-schema.
var DefaultTypeSchemaMapper = map[reflect.Type]*TypeSchema{
reflect.TypeOf(time.Time{}): &TypeSchema{
RefName: "",
Schema: &Schema{
Type: "string",
Format: "date-time", // RFC3339
},
AllowRef: false,
},
}
// 備忘
// swaggerのJSONを組み上げる上で、色々なTypeを走査せねばならない。
// トップレベルはもちろんTypeからなんだが、Typeの構成要素はTypeだけではない。
// 1つのTypeは built-in type だったり struct する。
// structの場合、要素は 名前 + Type + タグ であり、Typeだけから1つのパーツがなっているわけではない。
// FieldInfo has a information for struct field.
// It contains Type information and Tag information.
type FieldInfo struct {
Base reflect.StructField
TypeSchema *TypeSchema
EmitAsString bool
Enum []interface{} // from tag, e.g. swagger:",enum=ok|ng"
}
// Anonymous is an embedded field.
func (fiInfo *FieldInfo) Anonymous() bool {
return fiInfo.Base.Anonymous
}
// Type returns field type.
func (fiInfo *FieldInfo) Type() reflect.Type {
return fiInfo.Base.Type
}
// Ignored is an ignored field.
func (fiInfo *FieldInfo) Ignored() bool {
tagJSON := ucon.NewTagJSON(fiInfo.Base.Tag)
if tagJSON.Ignored() {
return true
}
return false
}
// Name returns field name on swagger.
func (fiInfo *FieldInfo) Name() string {
tagJSON := ucon.NewTagJSON(fiInfo.Base.Tag)
name := tagJSON.Name()
if name == "" {
name = fiInfo.Base.Name
}
return name
}
// TypeSchema is a container of swagger schema and its attributes.
// RefName must be given if AllowRef is true.
type TypeSchema struct {
RefName string
Schema *Schema
AllowRef bool
}
// SwaggerSchema returns schema that is can use is swagger.json.
func (ts *TypeSchema) SwaggerSchema() (*Schema, error) {
if ts.AllowRef && ts.RefName != "" {
return &Schema{Ref: fmt.Sprintf("#/definitions/%s", ts.RefName)}, nil
} else if ts.AllowRef {
return nil, errors.New("Name is required")
}
return ts.Schema.ShallowCopy(), nil
}
// Plugin is a holder for all of plugin settings.
type Plugin struct {
constructor *swaggerObjectConstructor
options *Options
}
type swaggerObjectConstructor struct {
plugin *Plugin
object *Object
typeSchemaMapper map[reflect.Type]*TypeSchema
finisher []func() error
}
type parameterWrapper struct {
StructField reflect.StructField
}
// HandlerInfo is a container of the handler function and the operation with the context.
// HandlerInfo implements interfaces of ucon.HandlerContainer and ucon.Context.
type HandlerInfo struct {
HandlerFunc interface{}
Operation
Context ucon.Context
}
// Options is a container of optional settings to configure a plugin.
type Options struct {
Object *Object
DefinitionNameModifier func(refT reflect.Type, defName string) string
}
// NewPlugin returns new swagger plugin configured with the options.
func NewPlugin(opts *Options) *Plugin {
if opts == nil {
opts = &Options{}
}
so := opts.Object
if so == nil {
so = &Object{}
}
if so.Paths == nil {
so.Paths = make(Paths, 0)
}
if so.Definitions == nil {
so.Definitions = make(Definitions, 0)
}
p := &Plugin{options: opts}
soConstructor := &swaggerObjectConstructor{
plugin: p,
object: so,
typeSchemaMapper: make(map[reflect.Type]*TypeSchema),
}
for k, v := range DefaultTypeSchemaMapper {
soConstructor.typeSchemaMapper[k] = v
}
p.constructor = soConstructor
return p
}
// HandlersScannerProcess executes scanning all registered handlers to serve swagger.json.
func (p *Plugin) HandlersScannerProcess(m *ucon.ServeMux, rds []*ucon.RouteDefinition) error {
soConstructor := p.constructor
// construct swagger.json
for _, rd := range rds {
err := soConstructor.processHandler(rd)
if err != nil {
return err
}
}
err := soConstructor.object.finish()
if err != nil {
return err
}
// supply swagger.json endpoint
m.HandleFunc("GET", "/api/swagger.json", func(w http.ResponseWriter, r *http.Request) *Object {
return soConstructor.object
})
return nil
}
func (soConstructor *swaggerObjectConstructor) processHandler(rd *ucon.RouteDefinition) error {
item := soConstructor.object.Paths[rd.PathTemplate.PathTemplate]
if item == nil {
item = &PathItem{}
}
var setOperation func(op *Operation)
switch rd.Method {
case "GET":
setOperation = func(op *Operation) {
item.Get = op
}
case "PUT":
setOperation = func(op *Operation) {
item.Put = op
}
case "POST":
setOperation = func(op *Operation) {
item.Post = op
}
case "DELETE":
setOperation = func(op *Operation) {
item.Delete = op
}
case "OPTIONS":
setOperation = func(op *Operation) {
item.Options = op
}
case "HEAD":
setOperation = func(op *Operation) {
item.Head = op
}
case "PATCH":
setOperation = func(op *Operation) {
item.Patch = op
}
case "*":
// swagger.json should skip wildcard method
return nil
default:
return fmt.Errorf("unknown method: %s", rd.Method)
}
if op, err := soConstructor.extractSwaggerOperation(rd); err != nil {
soConstructor.finisher = nil
return err
} else if op != nil {
setOperation(op)
soConstructor.object.Paths[rd.PathTemplate.PathTemplate] = item
err := soConstructor.execFinisher()
if err != nil {
return err
}
for _, ts := range soConstructor.typeSchemaMapper {
if !ts.AllowRef {
continue
}
if ts.RefName == "" {
return errors.New("Name is required")
}
if _, ok := soConstructor.object.Definitions[ts.RefName]; !ok {
soConstructor.object.Definitions[ts.RefName] = ts.Schema
}
}
}
return nil
}
func (soConstructor *swaggerObjectConstructor) extractSwaggerOperation(rd *ucon.RouteDefinition) (*Operation, error) {
var op *Operation
op, ok := rd.HandlerContainer.Value(swaggerOperationKey{}).(*Operation)
if !ok || op == nil {
op = &Operation{
Description: fmt.Sprintf("%s %s", rd.Method, rd.PathTemplate.PathTemplate),
}
}
if len(op.Responses) == 0 {
op.Responses = make(Responses, 0)
op.Responses["200"] = &Response{
Description: fmt.Sprintf("response of %s %s", rd.Method, rd.PathTemplate.PathTemplate),
}
}
var reqType, respType, errType reflect.Type
handlerT := reflect.TypeOf(rd.HandlerContainer.Handler())
for i, numIn := 0, handlerT.NumIn(); i < numIn; i++ {
arg := handlerT.In(i)
if arg == httpReqType {
continue
} else if arg == httpRespType {
continue
} else if arg == netContextType {
continue
}
reqType = arg
break
}
for i, numOut := 0, handlerT.NumOut(); i < numOut; i++ {
ret := handlerT.Out(i)
if ret.AssignableTo(errorType) {
errType = ret
continue
}
respType = ret
}
if respType == nil && errType == nil {
// static file handler...?
return nil, nil
}
// parameter
var bodyParameter *Parameter
if reqType != nil {
paramNames, paramMap, err := soConstructor.extractParameterMapperMap(reqType)
if err != nil {
return nil, err
}
sort.Strings(paramNames)
needBody := false
outer:
for _, paramName := range paramNames {
pw := paramMap[paramName]
// in path
if pw.InPath() {
op.Parameters = append(op.Parameters, &Parameter{
Name: paramName,
In: "path",
Required: true,
Type: pw.ParameterType(),
Format: pw.ParameterFormat(),
Enum: pw.ParameterEnum(),
Minimum: pw.Minimum(),
Maximum: pw.Maximum(),
MinLength: pw.MinLength(),
MaxLength: pw.MaxLength(),
Pattern: pw.Pattern(),
})
continue
} else {
for _, pathParam := range rd.PathTemplate.PathParameters {
if paramName != pathParam {
continue
}
op.Parameters = append(op.Parameters, &Parameter{
Name: paramName,
In: "path",
Required: true,
Type: pw.ParameterType(),
Format: pw.ParameterFormat(),
Enum: pw.ParameterEnum(),
Minimum: pw.Minimum(),
Maximum: pw.Maximum(),
MinLength: pw.MinLength(),
MaxLength: pw.MaxLength(),
Pattern: pw.Pattern(),
})
continue outer
}
}
// in query
if pw.InQuery() {
param := &Parameter{
Name: pw.Name(),
In: "query",
Required: pw.Required(),
Type: pw.ParameterType(),
Format: pw.ParameterFormat(),
Minimum: pw.Minimum(),
Maximum: pw.Maximum(),
MinLength: pw.MinLength(),
MaxLength: pw.MaxLength(),
Pattern: pw.Pattern(),
}
if param.Type == "array" {
fiInfo, err := soConstructor.extractFieldInfo(pw.StructField)
if err != nil {
return nil, err
}
soConstructor.addFinisher(func() error {
ts := fiInfo.TypeSchema
// NOTE(laco) Parameter.Items doesn't allow `$ref`.
// Parameter.Items.Type is required.
if ts.Schema == nil || ts.Schema.Items == nil || ts.Schema.Items.Type == "" {
return errors.New("Items is required")
}
param.Items = &Items{}
param.Items.Type = ts.Schema.Items.Type
param.Items.Format = ts.Schema.Items.Format
if fiInfo.EmitAsString {
param.Items.Type = "string"
}
param.Items.Enum = fiInfo.Enum
return nil
})
} else {
param.Enum = pw.ParameterEnum()
}
op.Parameters = append(op.Parameters, param)
continue
}
if pw.Private() {
continue
}
needBody = true
}
// in body
if needBody {
bodyParameter = &Parameter{
Name: "body",
In: "body",
Required: true,
Schema: nil,
}
switch rd.Method {
case "GET", "DELETE":
bodyParameter.Required = false
}
op.Parameters = append(op.Parameters, bodyParameter)
}
}
if reqType != nil && bodyParameter != nil {
ts, err := soConstructor.extractTypeSchema(reqType)
if err != nil {
return nil, err
}
if bodyParameter != nil {
soConstructor.addFinisher(func() error {
schema, err := ts.SwaggerSchema()
if err != nil {
return err
}
bodyParameter.Schema = schema
return nil
})
}
}
if respType != nil {
ts, err := soConstructor.extractTypeSchema(respType)
if err != nil {
return nil, err
}
soConstructor.addFinisher(func() error {
for _, resp := range op.Responses {
schema, err := ts.SwaggerSchema()
if err != nil {
return err
}
resp.Schema = schema
}
return nil
})
}
if errType != nil {
if errType == errorType {
// pass
} else if errType == uconHTTPErrorType {
// pass
} else {
ts, err := soConstructor.extractTypeSchema(errType)
if err != nil {
return nil, err
}
soConstructor.addFinisher(func() error {
if op.Responses["default"] == nil {
resp := &Response{
Description: "???", // TODO
}
op.Responses["default"] = resp
schema, err := ts.SwaggerSchema()
if err != nil {
return err
}
resp.Schema = schema
}
return nil
})
}
}
return op, nil
}
func (soConstructor *swaggerObjectConstructor) extractFieldInfo(sf reflect.StructField) (*FieldInfo, error) {
fiInfo := &FieldInfo{Base: sf}
if fiInfo.Ignored() {
return fiInfo, nil
}
ts, err := soConstructor.extractTypeSchema(sf.Type)
if err != nil {
return nil, err
}
fiInfo.TypeSchema = ts
fiInfo.EmitAsString = ucon.NewTagJSON(sf.Tag).HasString()
enumAsString := NewTagSwagger(sf.Tag).Enum()
var enum []interface{}
switch sf.Type.Kind() {
case reflect.Struct:
case reflect.Slice, reflect.Array:
case reflect.Bool:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
for _, enumStr := range enumAsString {
v, err := strconv.ParseInt(enumStr, 0, 32)
if err != nil {
return nil, err
}
enum = append(enum, int32(v))
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
for _, enumStr := range enumAsString {
v, err := strconv.ParseUint(enumStr, 0, 32)
if err != nil {
return nil, err
}
enum = append(enum, uint32(v))
}
case reflect.Int64:
for _, enumStr := range enumAsString {
v, err := strconv.ParseInt(enumStr, 0, 64)
if err != nil {
return nil, err
}
enum = append(enum, v)
}
case reflect.Uint64:
for _, enumStr := range enumAsString {
v, err := strconv.ParseUint(enumStr, 0, 64)
if err != nil {
return nil, err
}
enum = append(enum, v)
}
case reflect.Float32:
for _, enumStr := range enumAsString {
v, err := strconv.ParseFloat(enumStr, 32)
if err != nil {
return nil, err
}
enum = append(enum, float32(v))
}
case reflect.Float64:
for _, enumStr := range enumAsString {
v, err := strconv.ParseFloat(enumStr, 64)
if err != nil {
return nil, err
}
enum = append(enum, v)
}
case reflect.String:
for _, enumStr := range enumAsString {
enum = append(enum, enumStr)
}
default:
}
if fiInfo.EmitAsString {
// value format compatibility check was done in above code
enum = nil
for _, enumStr := range enumAsString {
enum = append(enum, enumStr)
}
}
fiInfo.Enum = enum
return fiInfo, nil
}
func (soConstructor *swaggerObjectConstructor) extractTypeSchema(refT reflect.Type) (*TypeSchema, error) {
if refT.Kind() == reflect.Ptr {
refT = refT.Elem()
}
if ts, ok := soConstructor.typeSchemaMapper[refT]; ok {
return ts, nil
}
ts := &TypeSchema{}
soConstructor.typeSchemaMapper[refT] = ts
schema := &Schema{}
schema.Type, schema.Format = extractSwaggerTypeAndFormat(refT)
ts.Schema = schema
defName := refT.Name()
if soConstructor.plugin.options.DefinitionNameModifier != nil {
defName = soConstructor.plugin.options.DefinitionNameModifier(refT, defName)
}
ts.RefName = defName
if defName != "" && refT.PkgPath() != "" {
// reject builtin-type, aka int, bool, string
ts.AllowRef = true
}
switch schema.Type {
case "object":
var process func(refT reflect.Type) error
process = func(refT reflect.Type) error {
if refT.Kind() == reflect.Ptr {
refT = refT.Elem()
}
if refT.Kind() != reflect.Struct {
return nil
}
for i, numField := 0, refT.NumField(); i < numField; i++ {
sf := refT.Field(i)
fiInfo, err := soConstructor.extractFieldInfo(sf)
if err != nil {
return err
}
if fiInfo.Ignored() {
continue
}
if fiInfo.Anonymous() {
// it just means same struct.
err := process(sf.Type)
if err != nil {
return err
}
continue
}
soConstructor.addFinisher(func() error {
fiSchema, err := fiInfo.TypeSchema.SwaggerSchema()
if err != nil {
return err
}
if fiInfo.EmitAsString {
fiSchema.Type = "string"
}
tagSwagger := NewTagSwagger(fiInfo.Base.Tag)
if tagSwagger.Required() {
schema.Required = append(schema.Required, fiInfo.Name())
}
switch fiSchema.Type {
case "string":
fiSchema.Pattern = tagSwagger.Pattern()
fiSchema.MinLength = tagSwagger.MinLength()
fiSchema.MaxLength = tagSwagger.MaxLength()
case "integer", "number":
fiSchema.Minimum = tagSwagger.Minimum()
fiSchema.Maximum = tagSwagger.Maximum()
}
fiSchema.Enum = fiInfo.Enum
schema.Properties[fiInfo.Name()] = fiSchema
return nil
})
}
return nil
}
if schema.Properties == nil {
schema.Properties = make(map[string]*Schema, 0)
}
err := process(refT)
if err != nil {
return nil, err
}
case "array":
{
var ts *TypeSchema
var err error
soConstructor.addFinisher(func() error {
itemSchema, err := ts.SwaggerSchema()
if err != nil {
return err
}
schema.Items = itemSchema
return nil
})
ts, err = soConstructor.extractTypeSchema(refT.Elem())
if err != nil {
return nil, err
}
}
case "":
return nil, fmt.Errorf("unknown schema type: %s", refT.Kind().String())
default:
}
return ts, nil
}
func (soConstructor *swaggerObjectConstructor) extractParameterMapperMap(refT reflect.Type) ([]string, map[string]*parameterWrapper, error) {
paramNames := make([]string, 0)
parameterMap := make(map[string]*parameterWrapper, 0)
var process func(refT reflect.Type) error
process = func(refT reflect.Type) error {
if refT.Kind() == reflect.Ptr {
refT = refT.Elem()
}
for i, numField := 0, refT.NumField(); i < numField; i++ {
sf := refT.Field(i)
pw := ¶meterWrapper{
StructField: sf,
}
if pw.Private() {
continue
}
if sf.Anonymous {
err := process(sf.Type)
if err != nil {
return err
}
continue
}
name := pw.Name()
if name == "" {
name = sf.Name
}
paramNames = append(paramNames, name)
parameterMap[name] = pw
}
return nil
}
err := process(refT)
if err != nil {
return nil, nil, err
}
return paramNames, parameterMap, nil
}
func (soConstructor *swaggerObjectConstructor) addFinisher(f func() error) {
soConstructor.finisher = append(soConstructor.finisher, f)
}
func (soConstructor *swaggerObjectConstructor) execFinisher() error {
for _, f := range soConstructor.finisher {
err := f()
if err != nil {
return err
}
}
soConstructor.finisher = nil
return nil
}
func extractSwaggerTypeAndFormat(refT reflect.Type) (string, string) {
if refT.Kind() == reflect.Ptr {
refT = refT.Elem()
}
var t string
var f string
switch refT.Kind() {
case reflect.Struct:
t = "object"
case reflect.Slice, reflect.Array:
t = "array"
case reflect.Bool:
t = "boolean"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
t = "integer"
f = "int32"
case reflect.Int64, reflect.Uint64:
t = "integer"
f = "int64"
case reflect.Float32:
t = "number"
f = "float"
case reflect.Float64:
t = "number"
f = "double"
case reflect.String:
t = "string"
default:
t = ""
}
return t, f
}
// AddTag adds the tag to top-level tags definition.
func (p *Plugin) AddTag(tag *Tag) *Tag {
p.constructor.object.Tags = append(p.constructor.object.Tags, tag)
return tag
}
func (pw *parameterWrapper) ParameterType() string {
if ucon.NewTagJSON(pw.StructField.Tag).HasString() {
return "string"
}
t, _ := extractSwaggerTypeAndFormat(pw.StructField.Type)
return t
}
func (pw *parameterWrapper) ParameterFormat() string {
refT := pw.StructField.Type
if refT.Kind() == reflect.Ptr {
refT = refT.Elem()
}
switch refT.Kind() {
case reflect.Int64:
return "int64"
default:
return ""
}
}
func (pw *parameterWrapper) ParameterEnum() []interface{} {
enumStrs := pw.Enum()
vs := make([]interface{}, 0, len(enumStrs))
for _, str := range enumStrs {
vs = append(vs, str)
}
return vs
}
func (pw *parameterWrapper) InPath() bool {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.In() == "path"
}
func (pw *parameterWrapper) InQuery() bool {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.In() == "query"
}
func (pw *parameterWrapper) Name() string {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
name := swaggerTag.Name()
if name != "" {
return name
}
jsonTag := ucon.NewTagJSON(pw.StructField.Tag)
name = jsonTag.Name()
if name != "" {
return name
}
return pw.StructField.Name
}
func (pw *parameterWrapper) Required() bool {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.Required()
}
func (pw *parameterWrapper) Enum() []string {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.Enum()
}
func (pw *parameterWrapper) Private() bool {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
if swaggerTag.Private() {
return true
}
jsonTag := ucon.NewTagJSON(pw.StructField.Tag)
if jsonTag.Ignored() {
return true
}
return false
}
func (pw *parameterWrapper) Maximum() *int {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.Maximum()
}
func (pw *parameterWrapper) Minimum() *int {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.Minimum()
}
func (pw *parameterWrapper) MaxLength() *int {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.MaxLength()
}
func (pw *parameterWrapper) MinLength() *int {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.MinLength()
}
func (pw *parameterWrapper) Pattern() string {
swaggerTag := NewTagSwagger(pw.StructField.Tag)
return swaggerTag.Pattern()
}
// NewHandlerInfo returns new HandlerInfo containing given handler function.
func NewHandlerInfo(handler interface{}) *HandlerInfo {
ucon.CheckFunction(handler)
return &HandlerInfo{
HandlerFunc: handler,
}
}
// Handler returns contained handler function.
func (wr *HandlerInfo) Handler() interface{} {
return wr.HandlerFunc
}
// Value returns the value contained with the key.
func (wr *HandlerInfo) Value(key interface{}) interface{} {
if key == (swaggerOperationKey{}) {
return &wr.Operation
}
if wr.Context != nil {
return wr.Context.Value(key)
}
return nil
}