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

beego:change ControllerComments exported

This commit is contained in:
astaxie 2014-06-09 17:46:13 +08:00
parent 21cb8ea4a3
commit 2570f075d9
3 changed files with 17 additions and 17 deletions

View File

@ -40,10 +40,10 @@ var (
// store the comment for the controller method // store the comment for the controller method
type ControllerComments struct { type ControllerComments struct {
method string Method string
router string Router string
allowHTTPMethods []string AllowHTTPMethods []string
params []map[string]string Params []map[string]string
} }
// Controller defines some basic http request handler operations, such as // Controller defines some basic http request handler operations, such as

View File

@ -69,23 +69,23 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat
} }
key := pkgpath + ":" + controllerName key := pkgpath + ":" + controllerName
cc := ControllerComments{} cc := ControllerComments{}
cc.method = funcName cc.Method = funcName
cc.router = e1[0] cc.Router = e1[0]
if len(e1) == 2 && e1[1] != "" { if len(e1) == 2 && e1[1] != "" {
e1 = strings.SplitN(e1[1], " ", 2) e1 = strings.SplitN(e1[1], " ", 2)
if len(e1) >= 1 { if len(e1) >= 1 {
cc.allowHTTPMethods = strings.Split(strings.Trim(e1[0], "[]"), ",") cc.AllowHTTPMethods = strings.Split(strings.Trim(e1[0], "[]"), ",")
} else { } else {
cc.allowHTTPMethods = append(cc.allowHTTPMethods, "get") cc.AllowHTTPMethods = append(cc.AllowHTTPMethods, "get")
} }
} else { } else {
cc.allowHTTPMethods = append(cc.allowHTTPMethods, "get") cc.AllowHTTPMethods = append(cc.AllowHTTPMethods, "get")
} }
if len(e1) == 2 && e1[1] != "" { if len(e1) == 2 && e1[1] != "" {
keyval := strings.Split(strings.Trim(e1[1], "[]"), " ") keyval := strings.Split(strings.Trim(e1[1], "[]"), " ")
for _, kv := range keyval { for _, kv := range keyval {
kk := strings.Split(kv, ":") kk := strings.Split(kv, ":")
cc.params = append(cc.params, map[string]string{strings.Join(kk[:len(kk)-1], ":"): kk[len(kk)-1]}) cc.Params = append(cc.Params, map[string]string{strings.Join(kk[:len(kk)-1], ":"): kk[len(kk)-1]})
} }
} }
genInfoList[key] = append(genInfoList[key], cc) genInfoList[key] = append(genInfoList[key], cc)
@ -107,25 +107,25 @@ func genRouterCode() {
for k, cList := range genInfoList { for k, cList := range genInfoList {
for _, c := range cList { for _, c := range cList {
allmethod := "nil" allmethod := "nil"
if len(c.allowHTTPMethods) > 0 { if len(c.AllowHTTPMethods) > 0 {
allmethod = "[]string{" allmethod = "[]string{"
for _, m := range c.allowHTTPMethods { for _, m := range c.AllowHTTPMethods {
allmethod += `"` + m + `",` allmethod += `"` + m + `",`
} }
allmethod = strings.TrimRight(allmethod, ",") + "}" allmethod = strings.TrimRight(allmethod, ",") + "}"
} }
params := "nil" params := "nil"
if len(c.params) > 0 { if len(c.Params) > 0 {
params = "[]map[string]string{" params = "[]map[string]string{"
for _, p := range c.params { for _, p := range c.Params {
for k, v := range p { for k, v := range p {
params = params + `map[string]string{` + k + `:"` + v + `"},` params = params + `map[string]string{` + k + `:"` + v + `"},`
} }
} }
params = strings.TrimRight(params, ",") + "}" params = strings.TrimRight(params, ",") + "}"
} }
globalinfo = globalinfo + fmt.Sprintln(`beego.GlobalControllerRouter["`+k+`"] = &ControllerComments{"`+ globalinfo = globalinfo + fmt.Sprintln(`beego.GlobalControllerRouter["`+k+`"] = &beego.ControllerComments{"`+
strings.TrimSpace(c.method)+`", "`+c.router+`", `+allmethod+", "+params+"}") strings.TrimSpace(c.Method)+`", "`+c.Router+`", `+allmethod+", "+params+"}")
} }
} }
f.WriteString(strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1)) f.WriteString(strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1))

View File

@ -185,7 +185,7 @@ func (p *ControllerRegistor) Include(cList ...ControllerInterface) {
key := t.PkgPath() + ":" + t.Name() key := t.PkgPath() + ":" + t.Name()
if comm, ok := GlobalControllerRouter[key]; ok { if comm, ok := GlobalControllerRouter[key]; ok {
for _, a := range comm { for _, a := range comm {
p.Add(a.router, c, strings.Join(a.allowHTTPMethods, ",")+":"+a.method) p.Add(a.Router, c, strings.Join(a.AllowHTTPMethods, ",")+":"+a.Method)
} }
} }
} }