MCPcopy
hub / github.com/swaggo/swag / ParseParamComment

Method ParseParamComment

operation.go:250–407  ·  view source on GitHub ↗

ParseParamComment parses params return []string of param properties E.g. @Param queryText formData string true "The email for login" [param name] [paramType] [data type] [is mandatory?] [Comment] E.g. @Param some_id path int true "Some ID".

(commentLine string, astFile *ast.File)

Source from the content-addressed store, hash-verified

248//
249// E.g. @Param some_id path int true "Some ID".
250func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.File) error {
251 matches := paramPattern.FindStringSubmatch(commentLine)
252 if len(matches) != 6 {
253 return fmt.Errorf("missing required param comment parameters \"%s\"", commentLine)
254 }
255
256 name := matches[1]
257 paramType := matches[2]
258 refType, format := TransToValidSchemeTypeWithFormat(matches[3])
259
260 // Detect refType
261 objectType := OBJECT
262
263 if strings.HasPrefix(refType, "[]") {
264 objectType = ARRAY
265 refType = strings.TrimPrefix(refType, "[]")
266 refType, format = TransToValidSchemeTypeWithFormat(refType)
267 } else if IsPrimitiveType(refType) ||
268 paramType == "formData" && refType == "file" {
269 objectType = PRIMITIVE
270 }
271
272 var enums []interface{}
273 if !IsPrimitiveType(refType) {
274 schema, _ := operation.parser.getTypeSchema(refType, astFile, false)
275 if schema != nil && len(schema.Type) == 1 && schema.Enum != nil {
276 if objectType == OBJECT {
277 objectType = PRIMITIVE
278 }
279 refType, format = TransToValidSchemeTypeWithFormat(schema.Type[0])
280 enums = schema.Enum
281 }
282 }
283
284 requiredText := strings.ToLower(matches[4])
285 required := requiredText == "true" || requiredText == requiredLabel
286 description := strings.Join(strings.Split(matches[5], "\\n"), "\n")
287
288 param := createParameter(paramType, description, name, objectType, refType, format, required, enums, operation.parser.collectionFormatInQuery)
289
290 switch paramType {
291 case "path", "header", "query", "formData":
292 switch objectType {
293 case ARRAY:
294 if !IsPrimitiveType(refType) && !(refType == "file" && paramType == "formData") {
295 return fmt.Errorf("%s is not supported array type for %s", refType, paramType)
296 }
297 case PRIMITIVE:
298 break
299 case OBJECT:
300 schema, err := operation.parser.getTypeSchema(refType, astFile, false)
301 if err != nil {
302 return err
303 }
304
305 if len(schema.Properties) == 0 {
306 return nil
307 }

Callers 1

ParseCommentMethod · 0.95

Calls 11

parseAPIObjectSchemaMethod · 0.95
parseParamAttributeMethod · 0.95
IsPrimitiveTypeFunction · 0.85
createParameterFunction · 0.85
IsSimplePrimitiveTypeFunction · 0.85
findInSliceFunction · 0.85
PrimitiveSchemaFunction · 0.85
getTypeSchemaMethod · 0.80
getUnderlyingSchemaMethod · 0.80
PrintfMethod · 0.65

Tested by

no test coverage detected