init exceptMethod

This commit is contained in:
Ming Deng 2021-04-19 23:37:05 +08:00
parent 7579d83551
commit ad81d0ce7c
4 changed files with 22 additions and 21 deletions

View File

@ -36,6 +36,7 @@
- Feature issue #4402 finish router get example. [4416](https://github.com/beego/beego/pull/4416)
- Proposal: Add Bind() method for `web.Controller` [4491](https://github.com/beego/beego/issues/4579)
- Optimize AddAutoPrefix: only register one router in case-insensitive mode. [4582](https://github.com/beego/beego/pull/4582)
- Init exceptMethod by using reflection. [4583](https://github.com/beego/beego/pull/4583)
## Fix Sonar
- [4473](https://github.com/beego/beego/pull/4473)

View File

@ -2,13 +2,12 @@ package web
import (
"encoding/json"
"mime"
"net/http"
"path/filepath"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web/context"
"github.com/beego/beego/v2/server/web/session"
"mime"
"net/http"
"path/filepath"
)
// register MIME type with content type
@ -95,4 +94,4 @@ func registerGzip() error {
)
}
return nil
}
}

View File

@ -69,20 +69,8 @@ var (
"UNLOCK": true,
}
// these web.Controller's methods shouldn't reflect to AutoRouter
exceptMethod = []string{"Abort", "CheckXSRFCookie", "CustomAbort", "DelSession",
"DestroySession", "Finish", "GetBool", "GetControllerAndAction",
"GetFile", "GetFiles", "GetFloat", "GetInt", "GetInt16",
"GetInt32", "GetInt64", "GetInt8", "GetSecureCookie", "GetSession",
"GetString", "GetStrings", "GetUint16", "GetUint32", "GetUint64",
"GetUint8", "HandlerFunc", "Init", "Input",
"IsAjax", "Mapping", "ParseForm",
"Prepare", "Redirect", "Render", "RenderBytes",
"RenderString", "SaveToFile", "SaveToFileWithBuffer", "SaveToFileWithBuffer",
"ServeFormatted", "ServeJSON", "ServeJSONP", "ServeXML", "ServeYAML",
"SessionRegenerateID", "SetData", "SetSecureCookie", "SetSession", "StartSession",
"StopRun", "URLFor", "URLMapping", "XSRFFormHTML",
"XSRFToken",
}
// see registerControllerExceptMethods
exceptMethod = initExceptMethod()
urlPlaceholder = "{{placeholder}}"
// DefaultAccessLogFilter will skip the accesslog if return true
@ -116,6 +104,17 @@ func ExceptMethodAppend(action string) {
exceptMethod = append(exceptMethod, action)
}
func initExceptMethod() []string {
res := make([]string, 0, 32)
c := &Controller{}
t := reflect.TypeOf(c)
for i := 0; i < t.NumMethod(); i++ {
m := t.Method(i)
res = append(res, m.Name)
}
return res
}
// ControllerInfo holds information about the controller.
type ControllerInfo struct {
pattern string

View File

@ -137,10 +137,12 @@ func TestUrlFor(t *testing.T) {
func TestUrlFor3(t *testing.T) {
handler := NewControllerRegister()
handler.AddAuto(&TestController{})
if a := handler.URLFor("TestController.Myext"); a != "/test/myext" && a != "/Test/Myext" {
a := handler.URLFor("TestController.Myext")
if a != "/test/myext" && a != "/Test/Myext" {
t.Errorf("TestController.Myext must equal to /test/myext, but get " + a)
}
if a := handler.URLFor("TestController.GetURL"); a != "/test/geturl" && a != "/Test/GetURL" {
a = handler.URLFor("TestController.GetURL")
if a != "/test/geturl" && a != "/Test/GetURL" {
t.Errorf("TestController.GetURL must equal to /test/geturl, but get " + a)
}
}