// parse the func comments func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpath string) error { innerapi := swagger.Api{} opts := swagger.Operation{} if comments != nil && comments.List != nil { for _, c := range comments.List { t := strings.TrimSpace(strings.TrimLeft(c.Text, "//")) if strings.HasPrefix(t, "@router") { elements := strings.TrimSpace(t[len("@router"):]) e1 := strings.SplitN(elements, " ", 2) if len(e1) < 1 { return errors.New("you should has router infomation") } innerapi.Path = e1[0] if len(e1) == 2 && e1[1] != "" { e1 = strings.SplitN(e1[1], " ", 2) opts.HttpMethod = strings.ToUpper(strings.Trim(e1[0], "[]")) } else { opts.HttpMethod = "GET" } } else if strings.HasPrefix(t, "@Title") { opts.Nickname = strings.TrimSpace(t[len("@Title"):]) } else if strings.HasPrefix(t, "@Description") { opts.Summary = strings.TrimSpace(t[len("@Description"):]) } else if strings.HasPrefix(t, "@Success") { ss := strings.TrimSpace(t[len("@Success"):]) rs := swagger.ResponseMessage{} st := make([]string, 3) j := 0 var tmp []rune start := false for i, c := range ss { if unicode.IsSpace(c) { if !start && j < 2 { continue } if j == 0 || j == 1 { st[j] = string(tmp) tmp = make([]rune, 0) j += 1 start = false continue } else { st[j] = strings.TrimSpace(ss[i+1:]) break } } else { start = true tmp = append(tmp, c) } } if len(tmp) > 0 && st[2] == "" { st[2] = strings.TrimSpace(string(tmp)) } rs.Message = st[2] if st[1] == "{object}" { if st[2] == "" { panic(controllerName + " " + funcName + " has no object") } cmpath, m, mod, realTypes := getModel(st[2]) //ll := strings.Split(st[2], ".") //opts.Type = ll[len(ll)-1] rs.ResponseModel = m if _, ok := modelsList[pkgpath+controllerName]; !ok { modelsList[pkgpath+controllerName] = make(map[string]swagger.Model, 0) } modelsList[pkgpath+controllerName][st[2]] = mod appendModels(cmpath, pkgpath, controllerName, realTypes) } rs.Code, _ = strconv.Atoi(st[0]) opts.ResponseMessages = append(opts.ResponseMessages, rs) } else if strings.HasPrefix(t, "@Param") { para := swagger.Parameter{} p := getparams(strings.TrimSpace(t[len("@Param "):])) if len(p) < 4 { panic(controllerName + "_" + funcName + "'s comments @Param at least should has 4 params") } para.Name = p[0] para.ParamType = p[1] pp := strings.Split(p[2], ".") para.DataType = pp[len(pp)-1] if len(p) > 4 { para.Required, _ = strconv.ParseBool(p[3]) para.Description = p[4] } else { para.Description = p[3] } opts.Parameters = append(opts.Parameters, para) } else if strings.HasPrefix(t, "@Failure") { rs := swagger.ResponseMessage{} st := strings.TrimSpace(t[len("@Failure"):]) var cd []rune var start bool for i, s := range st { if unicode.IsSpace(s) { if start { rs.Message = strings.TrimSpace(st[i+1:]) break } else { continue } } start = true cd = append(cd, s) } rs.Code, _ = strconv.Atoi(string(cd)) opts.ResponseMessages = append(opts.ResponseMessages, rs) } else if strings.HasPrefix(t, "@Type") { opts.Type = strings.TrimSpace(t[len("@Type"):]) } else if strings.HasPrefix(t, "@Accept") { accepts := strings.Split(strings.TrimSpace(strings.TrimSpace(t[len("@Accept"):])), ",") for _, a := range accepts { switch a { case "json": opts.Consumes = append(opts.Consumes, ajson) opts.Produces = append(opts.Produces, ajson) case "xml": opts.Consumes = append(opts.Consumes, axml) opts.Produces = append(opts.Produces, axml) case "plain": opts.Consumes = append(opts.Consumes, aplain) opts.Produces = append(opts.Produces, aplain) case "html": opts.Consumes = append(opts.Consumes, ahtml) opts.Produces = append(opts.Produces, ahtml) } } } } } innerapi.Operations = append(innerapi.Operations, opts) if innerapi.Path != "" { if _, ok := controllerList[pkgpath+controllerName]; ok { controllerList[pkgpath+controllerName] = append(controllerList[pkgpath+controllerName], innerapi) } else { controllerList[pkgpath+controllerName] = make([]swagger.Api, 1) controllerList[pkgpath+controllerName][0] = innerapi } } return nil }