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

make golint happy with controller.go

This commit is contained in:
astaxie 2015-09-08 10:43:42 +08:00
parent f28a941e26
commit 61570ac2f7
4 changed files with 80 additions and 86 deletions

View File

@ -34,18 +34,19 @@ import (
//commonly used mime-types //commonly used mime-types
const ( const (
applicationJson = "application/json" applicationJSON = "application/json"
applicationXml = "application/xml" applicationXML = "application/xml"
textXml = "text/xml" textXML = "text/xml"
) )
var ( var (
// custom error when user stop request handler manually. // ErrAbort custom error when user stop request handler manually.
USERSTOPRUN = errors.New("User stop run") ErrAbort = errors.New("User stop run")
GlobalControllerRouter map[string][]ControllerComments = make(map[string][]ControllerComments) //pkgpath+controller:comments // GlobalControllerRouter store comments with controller. pkgpath+controller:comments
GlobalControllerRouter = make(map[string][]ControllerComments)
) )
// store the comment for the controller method // ControllerComments store the comment for the controller method
type ControllerComments struct { type ControllerComments struct {
Method string Method string
Router string Router string
@ -64,7 +65,7 @@ type Controller struct {
Layout string Layout string
LayoutSections map[string]string // the key is the section name and the value is the template name LayoutSections map[string]string // the key is the section name and the value is the template name
TplExt string TplExt string
_xsrf_token string _xsrfToken string
gotofunc string gotofunc string
CruSession session.SessionStore CruSession session.SessionStore
XSRFExpire int XSRFExpire int
@ -153,20 +154,20 @@ func (c *Controller) Options() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
} }
// call function fn // HandlerFunc call function with the name
func (c *Controller) HandlerFunc(fnname string) bool { func (c *Controller) HandlerFunc(fnname string) bool {
if v, ok := c.methodMapping[fnname]; ok { if v, ok := c.methodMapping[fnname]; ok {
v() v()
return true return true
} else {
return false
} }
return false
} }
// URLMapping register the internal Controller router. // URLMapping register the internal Controller router.
func (c *Controller) URLMapping() { func (c *Controller) URLMapping() {
} }
// Mapping the method to function
func (c *Controller) Mapping(method string, fn func()) { func (c *Controller) Mapping(method string, fn func()) {
c.methodMapping[method] = fn c.methodMapping[method] = fn
} }
@ -177,13 +178,11 @@ func (c *Controller) Render() error {
return nil return nil
} }
rb, err := c.RenderBytes() rb, err := c.RenderBytes()
if err != nil { if err != nil {
return err return err
} else { }
c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8") c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
c.Ctx.Output.Body(rb) c.Ctx.Output.Body(rb)
}
return nil return nil
} }
@ -252,7 +251,8 @@ func (c *Controller) RenderBytes() ([]byte, error) {
} }
icontent, _ := ioutil.ReadAll(ibytes) icontent, _ := ioutil.ReadAll(ibytes)
return icontent, nil return icontent, nil
} else { }
if c.TplNames == "" { if c.TplNames == "" {
c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt
} }
@ -271,14 +271,13 @@ func (c *Controller) RenderBytes() ([]byte, error) {
icontent, _ := ioutil.ReadAll(ibytes) icontent, _ := ioutil.ReadAll(ibytes)
return icontent, nil return icontent, nil
} }
}
// Redirect sends the redirection response to url with status code. // Redirect sends the redirection response to url with status code.
func (c *Controller) Redirect(url string, code int) { func (c *Controller) Redirect(url string, code int) {
c.Ctx.Redirect(code, url) c.Ctx.Redirect(code, url)
} }
// Aborts stops controller handler and show the error data if code is defined in ErrorMap or code string. // Abort stops controller handler and show the error data if code is defined in ErrorMap or code string.
func (c *Controller) Abort(code string) { func (c *Controller) Abort(code string) {
status, err := strconv.Atoi(code) status, err := strconv.Atoi(code)
if err != nil { if err != nil {
@ -296,29 +295,28 @@ func (c *Controller) CustomAbort(status int, body string) {
} }
// last panic user string // last panic user string
c.Ctx.ResponseWriter.Write([]byte(body)) c.Ctx.ResponseWriter.Write([]byte(body))
panic(USERSTOPRUN) panic(ErrAbort)
} }
// StopRun makes panic of USERSTOPRUN error and go to recover function if defined. // StopRun makes panic of USERSTOPRUN error and go to recover function if defined.
func (c *Controller) StopRun() { func (c *Controller) StopRun() {
panic(USERSTOPRUN) panic(ErrAbort)
} }
// UrlFor does another controller handler in this request function. // URLFor does another controller handler in this request function.
// it goes to this controller method if endpoint is not clear. // it goes to this controller method if endpoint is not clear.
func (c *Controller) UrlFor(endpoint string, values ...interface{}) string { func (c *Controller) URLFor(endpoint string, values ...interface{}) string {
if len(endpoint) <= 0 { if len(endpoint) <= 0 {
return "" return ""
} }
if endpoint[0] == '.' { if endpoint[0] == '.' {
return UrlFor(reflect.Indirect(reflect.ValueOf(c.AppController)).Type().Name()+endpoint, values...) return URLFor(reflect.Indirect(reflect.ValueOf(c.AppController)).Type().Name()+endpoint, values...)
} else {
return UrlFor(endpoint, values...)
} }
return URLFor(endpoint, values...)
} }
// ServeJson sends a json response with encoding charset. // ServeJSON sends a json response with encoding charset.
func (c *Controller) ServeJson(encoding ...bool) { func (c *Controller) ServeJSON(encoding ...bool) {
var hasIndent bool var hasIndent bool
var hasencoding bool var hasencoding bool
if RunMode == "prod" { if RunMode == "prod" {
@ -332,8 +330,8 @@ func (c *Controller) ServeJson(encoding ...bool) {
c.Ctx.Output.Json(c.Data["json"], hasIndent, hasencoding) c.Ctx.Output.Json(c.Data["json"], hasIndent, hasencoding)
} }
// ServeJsonp sends a jsonp response. // ServeJSONP sends a jsonp response.
func (c *Controller) ServeJsonp() { func (c *Controller) ServeJSONP() {
var hasIndent bool var hasIndent bool
if RunMode == "prod" { if RunMode == "prod" {
hasIndent = false hasIndent = false
@ -343,8 +341,8 @@ func (c *Controller) ServeJsonp() {
c.Ctx.Output.Jsonp(c.Data["jsonp"], hasIndent) c.Ctx.Output.Jsonp(c.Data["jsonp"], hasIndent)
} }
// ServeXml sends xml response. // ServeXML sends xml response.
func (c *Controller) ServeXml() { func (c *Controller) ServeXML() {
var hasIndent bool var hasIndent bool
if RunMode == "prod" { if RunMode == "prod" {
hasIndent = false hasIndent = false
@ -358,12 +356,12 @@ func (c *Controller) ServeXml() {
func (c *Controller) ServeFormatted() { func (c *Controller) ServeFormatted() {
accept := c.Ctx.Input.Header("Accept") accept := c.Ctx.Input.Header("Accept")
switch accept { switch accept {
case applicationJson: case applicationJSON:
c.ServeJson() c.ServeJSON()
case applicationXml, textXml: case applicationXML, textXML:
c.ServeXml() c.ServeXML()
default: default:
c.ServeJson() c.ServeJSON()
} }
} }
@ -389,9 +387,8 @@ func (c *Controller) GetString(key string, def ...string) string {
if v := c.Ctx.Input.Query(key); v != "" { if v := c.Ctx.Input.Query(key); v != "" {
return v return v
} else {
return defv
} }
return defv
} }
// GetStrings returns the input string slice by key string or the default value while it's present and input is blank // GetStrings returns the input string slice by key string or the default value while it's present and input is blank
@ -410,9 +407,8 @@ func (c *Controller) GetStrings(key string, def ...[]string) []string {
vs := f[key] vs := f[key]
if len(vs) > 0 { if len(vs) > 0 {
return vs return vs
} else {
return defv
} }
return defv
} }
// GetInt returns input as an int or the default value while it's present and input is blank // GetInt returns input as an int or the default value while it's present and input is blank
@ -586,7 +582,7 @@ func (c *Controller) GetSession(name interface{}) interface{} {
return c.CruSession.Get(name) return c.CruSession.Get(name)
} }
// SetSession removes value from session. // DelSession removes value from session.
func (c *Controller) DelSession(name interface{}) { func (c *Controller) DelSession(name interface{}) {
if c.CruSession == nil { if c.CruSession == nil {
c.StartSession() c.StartSession()
@ -625,34 +621,34 @@ func (c *Controller) SetSecureCookie(Secret, name, value string, others ...inter
c.Ctx.SetSecureCookie(Secret, name, value, others...) c.Ctx.SetSecureCookie(Secret, name, value, others...)
} }
// XsrfToken creates a xsrf token string and returns. // XSRFToken creates a CSRF token string and returns.
func (c *Controller) XsrfToken() string { func (c *Controller) XSRFToken() string {
if c._xsrf_token == "" { if c._xsrfToken == "" {
var expire int64 var expire int64
if c.XSRFExpire > 0 { if c.XSRFExpire > 0 {
expire = int64(c.XSRFExpire) expire = int64(c.XSRFExpire)
} else { } else {
expire = int64(XSRFExpire) expire = int64(XSRFExpire)
} }
c._xsrf_token = c.Ctx.XsrfToken(XSRFKEY, expire) c._xsrfToken = c.Ctx.XsrfToken(XSRFKEY, expire)
} }
return c._xsrf_token return c._xsrfToken
} }
// CheckXsrfCookie checks xsrf token in this request is valid or not. // CheckXSRFCookie checks xsrf token in this request is valid or not.
// the token can provided in request header "X-Xsrftoken" and "X-CsrfToken" // the token can provided in request header "X-Xsrftoken" and "X-CsrfToken"
// or in form field value named as "_xsrf". // or in form field value named as "_xsrf".
func (c *Controller) CheckXsrfCookie() bool { func (c *Controller) CheckXSRFCookie() bool {
if !c.EnableXSRF { if !c.EnableXSRF {
return true return true
} }
return c.Ctx.CheckXsrfCookie() return c.Ctx.CheckXsrfCookie()
} }
// XsrfFormHtml writes an input field contains xsrf token value. // XSRFFormHTML writes an input field contains xsrf token value.
func (c *Controller) XsrfFormHtml() string { func (c *Controller) XSRFFormHTML() string {
return "<input type=\"hidden\" name=\"_xsrf\" value=\"" + return "<input type=\"hidden\" name=\"_xsrf\" value=\"" +
c._xsrf_token + "\"/>" c._xsrfToken + "\"/>"
} }
// GetControllerAndAction gets the executing controller name and action name. // GetControllerAndAction gets the executing controller name and action name.

View File

@ -863,7 +863,7 @@ Admin:
func (p *ControllerRegister) recoverPanic(context *beecontext.Context) { func (p *ControllerRegister) recoverPanic(context *beecontext.Context) {
if err := recover(); err != nil { if err := recover(); err != nil {
if err == USERSTOPRUN { if err == ErrAbort {
return return
} }
if !RecoverPanic { if !RecoverPanic {

View File

@ -65,7 +65,7 @@ func init() {
beegoTplFuncMap["lt"] = lt // < beegoTplFuncMap["lt"] = lt // <
beegoTplFuncMap["ne"] = ne // != beegoTplFuncMap["ne"] = ne // !=
beegoTplFuncMap["urlfor"] = UrlFor // != beegoTplFuncMap["urlfor"] = URLFor // !=
} }
// AddFuncMap let user to register a func in the template. // AddFuncMap let user to register a func in the template.

View File

@ -237,14 +237,14 @@ func Htmlunquote(src string) string {
return strings.TrimSpace(text) return strings.TrimSpace(text)
} }
// UrlFor returns url string with another registered controller handler with params. // URLFor returns url string with another registered controller handler with params.
// usage: // usage:
// //
// UrlFor(".index") // URLFor(".index")
// print UrlFor("index") // print URLFor("index")
// router /login // router /login
// print UrlFor("login") // print URLFor("login")
// print UrlFor("login", "next","/"") // print URLFor("login", "next","/"")
// router /profile/:username // router /profile/:username
// print UrlFor("profile", ":username","John Doe") // print UrlFor("profile", ":username","John Doe")
// result: // result:
@ -254,7 +254,7 @@ func Htmlunquote(src string) string {
// /user/John%20Doe // /user/John%20Doe
// //
// more detail http://beego.me/docs/mvc/controller/urlbuilding.md // more detail http://beego.me/docs/mvc/controller/urlbuilding.md
func UrlFor(endpoint string, values ...interface{}) string { func URLFor(endpoint string, values ...interface{}) string {
return BeeApp.Handlers.UrlFor(endpoint, values...) return BeeApp.Handlers.UrlFor(endpoint, values...)
} }
@ -698,7 +698,6 @@ func MapGet(arg1 interface{}, arg2 ...interface{}) (interface{}, error) {
storedVal := arg1Val.MapIndex(arg2Val) storedVal := arg1Val.MapIndex(arg2Val)
if storedVal.IsValid() { if storedVal.IsValid() {
var result interface{} var result interface{}
@ -727,7 +726,6 @@ func MapGet(arg1 interface{}, arg2 ...interface{}) (interface{}, error) {
return nil, nil return nil, nil
} }
} else { } else {
return nil, nil return nil, nil
} }