1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:00:54 +00:00

Merge pull request #3217 from jinxjinxagain/develop

fix: When multiply comment routers on one func
This commit is contained in:
astaxie 2018-07-08 16:13:21 +08:00 committed by GitHub
commit 654ebebe3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -114,20 +114,21 @@ type parsedParam struct {
func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error { func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
if f.Doc != nil { if f.Doc != nil {
parsedComment, err := parseComment(f.Doc.List) parsedComments, err := parseComment(f.Doc.List)
if err != nil { if err != nil {
return err return err
} }
if parsedComment.routerPath != "" { for _, parsedComment := range parsedComments {
key := pkgpath + ":" + controllerName if parsedComment.routerPath != "" {
cc := ControllerComments{} key := pkgpath + ":" + controllerName
cc.Method = f.Name.String() cc := ControllerComments{}
cc.Router = parsedComment.routerPath cc.Method = f.Name.String()
cc.AllowHTTPMethods = parsedComment.methods cc.Router = parsedComment.routerPath
cc.MethodParams = buildMethodParams(f.Type.Params.List, parsedComment) cc.AllowHTTPMethods = parsedComment.methods
genInfoList[key] = append(genInfoList[key], cc) cc.MethodParams = buildMethodParams(f.Type.Params.List, parsedComment)
genInfoList[key] = append(genInfoList[key], cc)
}
} }
} }
return nil return nil
} }
@ -177,26 +178,13 @@ func paramInPath(name, route string) bool {
var routeRegex = regexp.MustCompile(`@router\s+(\S+)(?:\s+\[(\S+)\])?`) var routeRegex = regexp.MustCompile(`@router\s+(\S+)(?:\s+\[(\S+)\])?`)
func parseComment(lines []*ast.Comment) (pc *parsedComment, err error) { func parseComment(lines []*ast.Comment) (pcs []*parsedComment, err error) {
pc = &parsedComment{} pcs = []*parsedComment{}
params := map[string]parsedParam{}
for _, c := range lines { for _, c := range lines {
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//")) t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
if strings.HasPrefix(t, "@router") { if strings.HasPrefix(t, "@Param") {
matches := routeRegex.FindStringSubmatch(t)
if len(matches) == 3 {
pc.routerPath = matches[1]
methods := matches[2]
if methods == "" {
pc.methods = []string{"get"}
//pc.hasGet = true
} else {
pc.methods = strings.Split(methods, ",")
//pc.hasGet = strings.Contains(methods, "get")
}
} else {
return nil, errors.New("Router information is missing")
}
} else if strings.HasPrefix(t, "@Param") {
pv := getparams(strings.TrimSpace(strings.TrimLeft(t, "@Param"))) pv := getparams(strings.TrimSpace(strings.TrimLeft(t, "@Param")))
if len(pv) < 4 { if len(pv) < 4 {
logs.Error("Invalid @Param format. Needs at least 4 parameters") logs.Error("Invalid @Param format. Needs at least 4 parameters")
@ -217,10 +205,32 @@ func parseComment(lines []*ast.Comment) (pc *parsedComment, err error) {
p.defValue = pv[3] p.defValue = pv[3]
p.required, _ = strconv.ParseBool(pv[4]) p.required, _ = strconv.ParseBool(pv[4])
} }
if pc.params == nil { params[funcParamName] = p
pc.params = map[string]parsedParam{} }
}
for _, c := range lines {
var pc = &parsedComment{}
pc.params = params
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
if strings.HasPrefix(t, "@router") {
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
matches := routeRegex.FindStringSubmatch(t)
if len(matches) == 3 {
pc.routerPath = matches[1]
methods := matches[2]
if methods == "" {
pc.methods = []string{"get"}
//pc.hasGet = true
} else {
pc.methods = strings.Split(methods, ",")
//pc.hasGet = strings.Contains(methods, "get")
}
pcs = append(pcs, pc)
} else {
return nil, errors.New("Router information is missing")
} }
pc.params[funcParamName] = p
} }
} }
return return