@@ -348,25 +348,37 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
348348 return nil
349349}
350350
351+ const (
352+ defaultTag = "default"
353+ enumsTag = "enums"
354+ formatTag = "format"
355+ minimumTag = "minimum"
356+ maximumTag = "maximum"
357+ minLengthTag = "minlength"
358+ maxLengthTag = "maxlength"
359+ extensionsTag = "extensions"
360+ collectionFormatTag = "collectionFormat"
361+ )
362+
351363var regexAttributes = map [string ]* regexp.Regexp {
352364 // for Enums(A, B)
353- "enums" : regexp .MustCompile (`(?i)\s+enums\(.*\)` ),
365+ enumsTag : regexp .MustCompile (`(?i)\s+enums\(.*\)` ),
354366 // for maximum(0)
355- "maximum" : regexp .MustCompile (`(?i)\s+maxinum|maximum\(.*\)` ),
367+ maximumTag : regexp .MustCompile (`(?i)\s+maxinum|maximum\(.*\)` ),
356368 // for minimum(0)
357- "minimum" : regexp .MustCompile (`(?i)\s+mininum|minimum\(.*\)` ),
369+ minimumTag : regexp .MustCompile (`(?i)\s+mininum|minimum\(.*\)` ),
358370 // for default(0)
359- "default" : regexp .MustCompile (`(?i)\s+default\(.*\)` ),
371+ defaultTag : regexp .MustCompile (`(?i)\s+default\(.*\)` ),
360372 // for minlength(0)
361- "minlength" : regexp .MustCompile (`(?i)\s+minlength\(.*\)` ),
373+ minLengthTag : regexp .MustCompile (`(?i)\s+minlength\(.*\)` ),
362374 // for maxlength(0)
363- "maxlength" : regexp .MustCompile (`(?i)\s+maxlength\(.*\)` ),
375+ maxLengthTag : regexp .MustCompile (`(?i)\s+maxlength\(.*\)` ),
364376 // for format(email)
365- "format" : regexp .MustCompile (`(?i)\s+format\(.*\)` ),
377+ formatTag : regexp .MustCompile (`(?i)\s+format\(.*\)` ),
366378 // for extensions(x-example=test)
367- "extensions" : regexp .MustCompile (`(?i)\s+extensions\(.*\)` ),
379+ extensionsTag : regexp .MustCompile (`(?i)\s+extensions\(.*\)` ),
368380 // for collectionFormat(csv)
369- "collectionFormat" : regexp .MustCompile (`(?i)\s+collectionFormat\(.*\)` ),
381+ collectionFormatTag : regexp .MustCompile (`(?i)\s+collectionFormat\(.*\)` ),
370382}
371383
372384func (operation * Operation ) parseAndExtractionParamAttribute (commentLine , objectType , schemaType string , param * spec.Parameter ) error {
@@ -377,52 +389,23 @@ func (operation *Operation) parseAndExtractionParamAttribute(commentLine, object
377389 continue
378390 }
379391 switch attrKey {
380- case "enums" :
381- err := setEnumParam (attr , objectType , schemaType , param )
382- if err != nil {
383- return err
384- }
385- case "maximum" :
386- n , err := setNumberParam (attrKey , schemaType , attr , commentLine )
387- if err != nil {
388- return err
389- }
390- param .Maximum = & n
391- case "minimum" :
392- n , err := setNumberParam (attrKey , schemaType , attr , commentLine )
393- if err != nil {
394- return err
395- }
396- param .Minimum = & n
397- case "default" :
398- value , err := defineType (schemaType , attr )
399- if err != nil {
400- return nil
401- }
402- param .Default = value
403- case "maxlength" :
404- n , err := setStringParam (attrKey , schemaType , attr , commentLine )
405- if err != nil {
406- return err
407- }
408- param .MaxLength = & n
409- case "minlength" :
410- n , err := setStringParam (attrKey , schemaType , attr , commentLine )
411- if err != nil {
412- return err
413- }
414- param .MinLength = & n
415- case "format" :
392+ case enumsTag :
393+ err = setEnumParam (param , attr , objectType , schemaType )
394+ case minimumTag , maximumTag :
395+ err = setNumberParam (param , attrKey , schemaType , attr , commentLine )
396+ case defaultTag :
397+ err = setDefault (param , schemaType , attr )
398+ case minLengthTag , maxLengthTag :
399+ err = setStringParam (param , attrKey , schemaType , attr , commentLine )
400+ case formatTag :
416401 param .Format = attr
417- case "extensions" :
418- param .Extensions = map [string ]interface {}{}
419- _ = setExtensionParam (attr , param )
420- case "collectionFormat" :
421- n , err := setCollectionFormatParam (attrKey , objectType , attr , commentLine )
422- if err != nil {
423- return err
424- }
425- param .CollectionFormat = n
402+ case extensionsTag :
403+ _ = setExtensionParam (param , attr )
404+ case collectionFormatTag :
405+ err = setCollectionFormatParam (param , attrKey , objectType , attr , commentLine )
406+ }
407+ if err != nil {
408+ return err
426409 }
427410 }
428411
@@ -440,34 +423,46 @@ func findAttr(re *regexp.Regexp, commentLine string) (string, error) {
440423 return strings .TrimSpace (attr [l + 1 : r ]), nil
441424}
442425
443- func setStringParam (name , schemaType , attr , commentLine string ) ( int64 , error ) {
426+ func setStringParam (param * spec. Parameter , name , schemaType , attr , commentLine string ) error {
444427 if schemaType != STRING {
445- return 0 , fmt .Errorf ("%s is attribute to set to a number. comment=%s got=%s" , name , commentLine , schemaType )
428+ return fmt .Errorf ("%s is attribute to set to a number. comment=%s got=%s" , name , commentLine , schemaType )
446429 }
447430
448431 n , err := strconv .ParseInt (attr , 10 , 64 )
449432 if err != nil {
450- return 0 , fmt .Errorf ("%s is allow only a number got=%s" , name , attr )
433+ return fmt .Errorf ("%s is allow only a number got=%s" , name , attr )
434+ }
435+
436+ switch name {
437+ case minLengthTag :
438+ param .MinLength = & n
439+ case maxLengthTag :
440+ param .MaxLength = & n
451441 }
452442
453- return n , nil
443+ return nil
454444}
455445
456- func setNumberParam (name , schemaType , attr , commentLine string ) ( float64 , error ) {
446+ func setNumberParam (param * spec. Parameter , name , schemaType , attr , commentLine string ) error {
457447 switch schemaType {
458448 case INTEGER , NUMBER :
459449 n , err := strconv .ParseFloat (attr , 64 )
460450 if err != nil {
461- return 0 , fmt .Errorf ("maximum is allow only a number. comment=%s got=%s" , commentLine , attr )
451+ return fmt .Errorf ("maximum is allow only a number. comment=%s got=%s" , commentLine , attr )
462452 }
463-
464- return n , nil
453+ switch name {
454+ case minimumTag :
455+ param .Minimum = & n
456+ case maximumTag :
457+ param .Maximum = & n
458+ }
459+ return nil
460+ default :
461+ return fmt .Errorf ("%s is attribute to set to a number. comment=%s got=%s" , name , commentLine , schemaType )
465462 }
466-
467- return 0 , fmt .Errorf ("%s is attribute to set to a number. comment=%s got=%s" , name , commentLine , schemaType )
468463}
469464
470- func setEnumParam (attr , objectType , schemaType string , param * spec. Parameter ) error {
465+ func setEnumParam (param * spec. Parameter , attr , objectType , schemaType string ) error {
471466 for _ , e := range strings .Split (attr , "," ) {
472467 e = strings .TrimSpace (e )
473468
@@ -487,7 +482,8 @@ func setEnumParam(attr, objectType, schemaType string, param *spec.Parameter) er
487482 return nil
488483}
489484
490- func setExtensionParam (attr string , param * spec.Parameter ) error {
485+ func setExtensionParam (param * spec.Parameter , attr string ) error {
486+ param .Extensions = map [string ]interface {}{}
491487 for _ , val := range strings .Split (attr , "," ) {
492488 parts := strings .SplitN (val , "=" , 2 )
493489 if len (parts ) == 2 {
@@ -500,44 +496,50 @@ func setExtensionParam(attr string, param *spec.Parameter) error {
500496 return nil
501497}
502498
503- func setCollectionFormatParam (name , schemaType , attr , commentLine string ) ( string , error ) {
499+ func setCollectionFormatParam (param * spec. Parameter , name , schemaType , attr , commentLine string ) error {
504500 if schemaType == ARRAY {
505- return TransToValidCollectionFormat (attr ), nil
501+ param .CollectionFormat = TransToValidCollectionFormat (attr )
502+ return nil
506503 }
507504
508- return "" , fmt .Errorf ("%s is attribute to set to an array. comment=%s got=%s" , name , commentLine , schemaType )
505+ return fmt .Errorf ("%s is attribute to set to an array. comment=%s got=%s" , name , commentLine , schemaType )
506+ }
507+
508+ func setDefault (param * spec.Parameter , schemaType string , value string ) error {
509+ val , err := defineType (schemaType , value )
510+ if err != nil {
511+ return nil // Don't set a default value if it's not valid
512+ }
513+ param .Default = val
514+ return nil
509515}
510516
511517// defineType enum value define the type (object and array unsupported).
512- func defineType (schemaType string , value string ) (interface {}, error ) {
518+ func defineType (schemaType string , value string ) (v interface {}, err error ) {
513519 schemaType = TransToValidSchemeType (schemaType )
514520 switch schemaType {
515521 case STRING :
516522 return value , nil
517523 case NUMBER :
518- v , err : = strconv .ParseFloat (value , 64 )
524+ v , err = strconv .ParseFloat (value , 64 )
519525 if err != nil {
520526 return nil , fmt .Errorf ("enum value %s can't convert to %s err: %s" , value , schemaType , err )
521527 }
522-
523- return v , nil
524528 case INTEGER :
525- v , err : = strconv .Atoi (value )
529+ v , err = strconv .Atoi (value )
526530 if err != nil {
527531 return nil , fmt .Errorf ("enum value %s can't convert to %s err: %s" , value , schemaType , err )
528532 }
529-
530- return v , nil
531533 case BOOLEAN :
532- v , err : = strconv .ParseBool (value )
534+ v , err = strconv .ParseBool (value )
533535 if err != nil {
534536 return nil , fmt .Errorf ("enum value %s can't convert to %s err: %s" , value , schemaType , err )
535537 }
536-
537- return v , nil
538538 default :
539539 return nil , fmt .Errorf ("%s is unsupported type in enum value %s" , schemaType , value )
540540 }
541+
542+ return v , nil
541543}
542544
543545// ParseTagsComment parses comment for given `tag` comment string.
@@ -836,7 +838,7 @@ func (operation *Operation) ParseResponseComment(commentLine string, astFile *as
836838 }
837839
838840 for _ , codeStr := range strings .Split (matches [1 ], "," ) {
839- if strings .EqualFold (codeStr , "default" ) {
841+ if strings .EqualFold (codeStr , defaultTag ) {
840842 operation .DefaultResponse ().Schema = schema
841843 operation .DefaultResponse ().Description = responseDescription
842844
@@ -901,7 +903,7 @@ func (operation *Operation) ParseResponseHeaderComment(commentLine string, _ *as
901903 }
902904
903905 for _ , codeStr := range strings .Split (matches [1 ], "," ) {
904- if strings .EqualFold (codeStr , "default" ) {
906+ if strings .EqualFold (codeStr , defaultTag ) {
905907 if operation .Responses .Default != nil {
906908 operation .Responses .Default .Headers [headerKey ] = header
907909 }
@@ -940,7 +942,7 @@ func (operation *Operation) ParseEmptyResponseComment(commentLine string) error
940942
941943 responseDescription := strings .Trim (matches [2 ], "\" " )
942944 for _ , codeStr := range strings .Split (matches [1 ], "," ) {
943- if strings .EqualFold (codeStr , "default" ) {
945+ if strings .EqualFold (codeStr , defaultTag ) {
944946 operation .DefaultResponse ().Description = responseDescription
945947
946948 continue
@@ -962,7 +964,7 @@ func (operation *Operation) ParseEmptyResponseComment(commentLine string) error
962964// ParseEmptyResponseOnly parse only comment out status code ,eg: @Success 200.
963965func (operation * Operation ) ParseEmptyResponseOnly (commentLine string ) error {
964966 for _ , codeStr := range strings .Split (commentLine , "," ) {
965- if strings .EqualFold (codeStr , "default" ) {
967+ if strings .EqualFold (codeStr , defaultTag ) {
966968 _ = operation .DefaultResponse ()
967969
968970 continue
@@ -998,6 +1000,7 @@ func (operation *Operation) AddResponse(code int, response *spec.Response) {
9981000 },
9991001 }
10001002 }
1003+
10011004 operation .Responses .StatusCodeResponses [code ] = * response
10021005}
10031006
@@ -1019,12 +1022,14 @@ func createParameter(paramType, description, paramName, schemaType string, requi
10191022 Type : []string {schemaType },
10201023 },
10211024 }
1025+
10221026 return result
10231027 }
10241028
10251029 result .SimpleSchema = spec.SimpleSchema {
10261030 Type : schemaType ,
10271031 }
1032+
10281033 return result
10291034}
10301035
0 commit comments