@@ -135,15 +135,16 @@ func (operation *Operation) ParseComment(comment string, astFile *ast.File) erro
135
135
}
136
136
137
137
// ParseCodeSample godoc.
138
- func (operation * Operation ) ParseCodeSample (attribute , commentLine , lineRemainder string ) error {
138
+ func (operation * Operation ) ParseCodeSample (attribute , _ , lineRemainder string ) error {
139
139
if lineRemainder == "file" {
140
140
data , err := getCodeExampleForSummary (operation .Summary , operation .codeExampleFilesDir )
141
141
if err != nil {
142
142
return err
143
143
}
144
144
145
145
var valueJSON interface {}
146
- if err := json .Unmarshal (data , & valueJSON ); err != nil {
146
+ err = json .Unmarshal (data , & valueJSON )
147
+ if err != nil {
147
148
return fmt .Errorf ("annotation %s need a valid json value" , attribute )
148
149
}
149
150
@@ -176,7 +177,8 @@ func (operation *Operation) ParseMetadata(attribute, lowerAttribute, lineRemaind
176
177
}
177
178
178
179
var valueJSON interface {}
179
- if err := json .Unmarshal ([]byte (lineRemainder ), & valueJSON ); err != nil {
180
+ err := json .Unmarshal ([]byte (lineRemainder ), & valueJSON )
181
+ if err != nil {
180
182
return fmt .Errorf ("annotation %s need a valid json value" , attribute )
181
183
}
182
184
@@ -189,6 +191,16 @@ func (operation *Operation) ParseMetadata(attribute, lowerAttribute, lineRemaind
189
191
190
192
var paramPattern = regexp .MustCompile (`(\S+)[\s]+([\w]+)[\s]+([\S.]+)[\s]+([\w]+)[\s]+"([^"]+)"` )
191
193
194
+ func findInSlice (arr []string , target string ) bool {
195
+ for _ , str := range arr {
196
+ if str == target {
197
+ return true
198
+ }
199
+ }
200
+
201
+ return false
202
+ }
203
+
192
204
// ParseParamComment parses params return []string of param properties
193
205
// E.g. @Param queryText formData string true "The email for login"
194
206
// [param name] [paramType] [data type] [is mandatory?] [Comment]
@@ -248,15 +260,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
248
260
if len (schema .Properties ) == 0 {
249
261
return nil
250
262
}
251
- find := func (arr []string , target string ) bool {
252
- for _ , str := range arr {
253
- if str == target {
254
- return true
255
- }
256
- }
257
263
258
- return false
259
- }
260
264
items := schema .Properties .ToOrderedSchemaItems ()
261
265
for _ , item := range items {
262
266
name := item .Name
@@ -269,7 +273,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
269
273
prop .Items .Schema != nil &&
270
274
len (prop .Items .Schema .Type ) > 0 &&
271
275
IsSimplePrimitiveType (prop .Items .Schema .Type [0 ]):
272
- param = createParameter (paramType , prop .Description , name , prop .Type [0 ], find (schema .Required , name ))
276
+ param = createParameter (paramType , prop .Description , name , prop .Type [0 ], findInSlice (schema .Required , name ))
273
277
param .SimpleSchema .Type = prop .Type [0 ]
274
278
if operation .parser != nil && operation .parser .collectionFormatInQuery != "" && param .CollectionFormat == "" {
275
279
param .CollectionFormat = TransToValidCollectionFormat (operation .parser .collectionFormatInQuery )
@@ -280,7 +284,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
280
284
},
281
285
}
282
286
case IsSimplePrimitiveType (prop .Type [0 ]):
283
- param = createParameter (paramType , prop .Description , name , prop .Type [0 ], find (schema .Required , name ))
287
+ param = createParameter (paramType , prop .Description , name , prop .Type [0 ], findInSlice (schema .Required , name ))
284
288
default :
285
289
Println (fmt .Sprintf ("skip field [%s] in %s is not supported type for %s" , name , refType , paramType ))
286
290
@@ -318,7 +322,8 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
318
322
return fmt .Errorf ("%s is not supported paramType" , paramType )
319
323
}
320
324
321
- if err := operation .parseAndExtractionParamAttribute (commentLine , objectType , refType , & param ); err != nil {
325
+ err := operation .parseAndExtractionParamAttribute (commentLine , objectType , refType , & param )
326
+ if err != nil {
322
327
return err
323
328
}
324
329
operation .Operation .Parameters = append (operation .Operation .Parameters , param )
@@ -495,9 +500,9 @@ func defineType(schemaType string, value string) (interface{}, error) {
495
500
}
496
501
497
502
return v , nil
503
+ default :
504
+ return nil , fmt .Errorf ("%s is unsupported type in enum value %s" , schemaType , value )
498
505
}
499
-
500
- return nil , fmt .Errorf ("%s is unsupported type in enum value %s" , schemaType , value )
501
506
}
502
507
503
508
// ParseTagsComment parses comment for given `tag` comment string.
@@ -529,13 +534,12 @@ func parseMimeTypeList(mimeTypeList string, typeList *[]string, format string) e
529
534
530
535
continue
531
536
}
532
- if aliasMimeType , ok := mimeTypeAliases [typeName ]; ok {
533
- * typeList = append (* typeList , aliasMimeType )
534
-
535
- continue
537
+ aliasMimeType , ok := mimeTypeAliases [typeName ]
538
+ if ! ok {
539
+ return fmt .Errorf (format , typeName )
536
540
}
537
541
538
- return fmt . Errorf ( format , typeName )
542
+ * typeList = append ( * typeList , aliasMimeType )
539
543
}
540
544
541
545
return nil
@@ -545,9 +549,8 @@ var routerPattern = regexp.MustCompile(`^(/[\w\.\/\-{}\+:]*)[[:blank:]]+\[(\w+)]
545
549
546
550
// ParseRouterComment parses comment for gived `router` comment string.
547
551
func (operation * Operation ) ParseRouterComment (commentLine string ) error {
548
- var matches []string
549
-
550
- if matches = routerPattern .FindStringSubmatch (commentLine ); len (matches ) != 3 {
552
+ matches := routerPattern .FindStringSubmatch (commentLine )
553
+ if len (matches ) != 3 {
551
554
return fmt .Errorf ("can not parse router comment \" %s\" " , commentLine )
552
555
}
553
556
path := matches [1 ]
@@ -572,8 +575,7 @@ func (operation *Operation) ParseSecurityComment(commentLine string) error {
572
575
scopes := securitySource [l + 1 : r ]
573
576
var s []string
574
577
for _ , scope := range strings .Split (scopes , "," ) {
575
- scope = strings .TrimSpace (scope )
576
- s = append (s , scope )
578
+ s = append (s , strings .TrimSpace (scope ))
577
579
}
578
580
securityKey := securitySource [0 :l ]
579
581
securityMap := map [string ][]string {}
@@ -731,7 +733,8 @@ func (operation *Operation) parseCombinedObjectSchema(refType string, astFile *a
731
733
fields := parseFields (matches [2 ])
732
734
props := map [string ]spec.Schema {}
733
735
for _ , field := range fields {
734
- if matches := strings .SplitN (field , "=" , 2 ); len (matches ) == 2 {
736
+ matches := strings .SplitN (field , "=" , 2 )
737
+ if len (matches ) == 2 {
735
738
schema , err := operation .parseObjectSchema (matches [1 ], astFile )
736
739
if err != nil {
737
740
return nil , err
@@ -777,9 +780,8 @@ func (operation *Operation) parseAPIObjectSchema(schemaType, refType string, ast
777
780
778
781
// ParseResponseComment parses comment for given `response` comment string.
779
782
func (operation * Operation ) ParseResponseComment (commentLine string , astFile * ast.File ) error {
780
- var matches []string
781
-
782
- if matches = responsePattern .FindStringSubmatch (commentLine ); len (matches ) != 5 {
783
+ matches := responsePattern .FindStringSubmatch (commentLine )
784
+ if len (matches ) != 5 {
783
785
err := operation .ParseEmptyResponseComment (commentLine )
784
786
if err != nil {
785
787
return operation .ParseEmptyResponseOnly (commentLine )
@@ -789,9 +791,7 @@ func (operation *Operation) ParseResponseComment(commentLine string, astFile *as
789
791
}
790
792
791
793
responseDescription := strings .Trim (matches [4 ], "\" " )
792
- schemaType := strings .Trim (matches [2 ], "{}" )
793
- refType := matches [3 ]
794
- schema , err := operation .parseAPIObjectSchema (schemaType , refType , astFile )
794
+ schema , err := operation .parseAPIObjectSchema (strings .Trim (matches [2 ], "{}" ), matches [3 ], astFile )
795
795
if err != nil {
796
796
return err
797
797
}
@@ -800,27 +800,29 @@ func (operation *Operation) ParseResponseComment(commentLine string, astFile *as
800
800
if strings .EqualFold (codeStr , "default" ) {
801
801
operation .DefaultResponse ().Schema = schema
802
802
operation .DefaultResponse ().Description = responseDescription
803
- } else if code , err := strconv .Atoi (codeStr ); err == nil {
804
- resp := & spec.Response {
805
- ResponseProps : spec.ResponseProps {Schema : schema , Description : responseDescription },
806
- }
807
- if resp .Description == "" {
808
- resp .Description = http .StatusText (code )
809
- }
810
- operation .AddResponse (code , resp )
811
- } else {
803
+
804
+ continue
805
+ }
806
+ code , err := strconv .Atoi (codeStr )
807
+ if err != nil {
812
808
return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
813
809
}
810
+ resp := & spec.Response {
811
+ ResponseProps : spec.ResponseProps {Schema : schema , Description : responseDescription },
812
+ }
813
+ if resp .Description == "" {
814
+ resp .Description = http .StatusText (code )
815
+ }
816
+ operation .AddResponse (code , resp )
814
817
}
815
818
816
819
return nil
817
820
}
818
821
819
- // ParseResponseHeaderComment parses comment for gived `response header` comment string.
820
- func (operation * Operation ) ParseResponseHeaderComment (commentLine string , astFile * ast.File ) error {
821
- var matches []string
822
-
823
- if matches = responsePattern .FindStringSubmatch (commentLine ); len (matches ) != 5 {
822
+ // ParseResponseHeaderComment parses comment for given `response header` comment string.
823
+ func (operation * Operation ) ParseResponseHeaderComment (commentLine string , _ * ast.File ) error {
824
+ matches := responsePattern .FindStringSubmatch (commentLine )
825
+ if len (matches ) != 5 {
824
826
return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
825
827
}
826
828
@@ -862,22 +864,22 @@ func (operation *Operation) ParseResponseHeaderComment(commentLine string, astFi
862
864
863
865
continue
864
866
}
865
- if code , err := strconv .Atoi (codeStr ); err == nil {
866
- if operation .Responses != nil && operation .Responses .StatusCodeResponses != nil {
867
- if response , responseExist := operation .Responses .StatusCodeResponses [code ]; responseExist {
868
- if response .Headers == nil {
869
- response .Headers = make (map [string ]spec.Header )
870
- }
871
- response .Headers [headerKey ] = header
872
867
873
- operation .Responses .StatusCodeResponses [code ] = response
868
+ code , err := strconv .Atoi (codeStr )
869
+ if err != nil {
870
+ return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
871
+ }
872
+ if operation .Responses != nil && operation .Responses .StatusCodeResponses != nil {
873
+ response , responseExist := operation .Responses .StatusCodeResponses [code ]
874
+ if responseExist {
875
+ if response .Headers == nil {
876
+ response .Headers = make (map [string ]spec.Header )
874
877
}
875
- }
878
+ response . Headers [ headerKey ] = header
876
879
877
- continue
880
+ operation .Responses .StatusCodeResponses [code ] = response
881
+ }
878
882
}
879
-
880
- return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
881
883
}
882
884
883
885
return nil
@@ -887,9 +889,8 @@ var emptyResponsePattern = regexp.MustCompile(`([\w,]+)[\s]+"(.*)"`)
887
889
888
890
// ParseEmptyResponseComment parse only comment out status code and description,eg: @Success 200 "it's ok".
889
891
func (operation * Operation ) ParseEmptyResponseComment (commentLine string ) error {
890
- var matches []string
891
-
892
- if matches = emptyResponsePattern .FindStringSubmatch (commentLine ); len (matches ) != 3 {
892
+ matches := emptyResponsePattern .FindStringSubmatch (commentLine )
893
+ if len (matches ) != 3 {
893
894
return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
894
895
}
895
896
@@ -901,15 +902,14 @@ func (operation *Operation) ParseEmptyResponseComment(commentLine string) error
901
902
continue
902
903
}
903
904
904
- if code , err := strconv .Atoi (codeStr ); err == nil {
905
- var response spec.Response
906
- response .Description = responseDescription
907
- operation .AddResponse (code , & response )
908
-
909
- continue
905
+ code , err := strconv .Atoi (codeStr )
906
+ if err != nil {
907
+ return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
910
908
}
911
909
912
- return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
910
+ var response spec.Response
911
+ response .Description = responseDescription
912
+ operation .AddResponse (code , & response )
913
913
}
914
914
915
915
return nil
@@ -923,15 +923,14 @@ func (operation *Operation) ParseEmptyResponseOnly(commentLine string) error {
923
923
924
924
continue
925
925
}
926
- if code , err := strconv .Atoi (codeStr ); err == nil {
927
- var response spec.Response
928
- // response.Description = http.StatusText(code)
929
- operation .AddResponse (code , & response )
930
-
931
- continue
926
+ code , err := strconv .Atoi (codeStr )
927
+ if err != nil {
928
+ return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
932
929
}
933
930
934
- return fmt .Errorf ("can not parse response comment \" %s\" " , commentLine )
931
+ var response spec.Response
932
+ // response.Description = http.StatusText(code)
933
+ operation .AddResponse (code , & response )
935
934
}
936
935
937
936
return nil
0 commit comments