Renames []HandleFunc to HandlersChain

This commit is contained in:
Manu Mtz-Almeida 2015-05-07 11:30:01 +02:00
parent 79131ac84d
commit eb3e9293ed
11 changed files with 28 additions and 27 deletions

View File

@ -59,7 +59,7 @@ type Context struct {
Writer ResponseWriter
Params Params
handlers []HandlerFunc
handlers HandlersChain
index int8
Engine *Engine

View File

@ -73,7 +73,7 @@ func TestContextCopy(t *testing.T) {
c, _, _ := createTestContext()
c.index = 2
c.Request, _ = http.NewRequest("POST", "/hola", nil)
c.handlers = []HandlerFunc{func(c *Context) {}}
c.handlers = HandlersChain{func(c *Context) {}}
c.Params = Params{Param{Key: "foo", Value: "bar"}}
c.Set("foo", "bar")

View File

@ -15,7 +15,7 @@ func IsDebugging() bool {
return ginMode == debugCode
}
func debugRoute(httpMethod, absolutePath string, handlers []HandlerFunc) {
func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
if IsDebugging() {
nuHandlers := len(handlers)
handlerName := nameOfFunction(handlers[nuHandlers-1])

View File

@ -11,7 +11,7 @@ import (
)
// TODO
// func debugRoute(httpMethod, absolutePath string, handlers []HandlerFunc) {
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
// func debugPrint(format string, values ...interface{}) {
func TestIsDebugging(t *testing.T) {

13
gin.go
View File

@ -17,17 +17,18 @@ var default404Body = []byte("404 page not found")
var default405Body = []byte("405 method not allowed")
type (
HandlerFunc func(*Context)
HandlerFunc func(*Context)
HandlersChain []HandlerFunc
// Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares.
Engine struct {
RouterGroup
HTMLRender render.Render
pool sync.Pool
allNoRoute []HandlerFunc
allNoMethod []HandlerFunc
noRoute []HandlerFunc
noMethod []HandlerFunc
allNoRoute HandlersChain
allNoMethod HandlersChain
noRoute HandlersChain
noMethod HandlersChain
trees map[string]*node
// Enables automatic redirection if the current route can't be matched but a
@ -136,7 +137,7 @@ func (engine *Engine) rebuild405Handlers() {
engine.allNoMethod = engine.combineHandlers(engine.noMethod)
}
func (engine *Engine) handle(method, path string, handlers []HandlerFunc) {
func (engine *Engine) handle(method, path string, handlers HandlersChain) {
if path[0] != '/' {
panic("path must begin with '/'")
}

View File

@ -29,9 +29,9 @@ func TestCreateEngine(t *testing.T) {
assert.True(t, router.RedirectFixedPath)
assert.True(t, router.HandleMethodNotAllowed)
assert.Panics(t, func() { router.handle("", "/", []HandlerFunc{func(_ *Context) {}}) })
assert.Panics(t, func() { router.handle("GET", "", []HandlerFunc{func(_ *Context) {}}) })
assert.Panics(t, func() { router.handle("GET", "/", []HandlerFunc{}) })
assert.Panics(t, func() { router.handle("", "/", HandlersChain{func(_ *Context) {}}) })
assert.Panics(t, func() { router.handle("GET", "", HandlersChain{func(_ *Context) {}}) })
assert.Panics(t, func() { router.handle("GET", "/", HandlersChain{}) })
}
func TestCreateDefaultRouter(t *testing.T) {

View File

@ -286,7 +286,7 @@ func TestGithubAPI(t *testing.T) {
router := New()
for _, route := range githubAPI {
router.Handle(route.method, route.path, []HandlerFunc{func(c *Context) {
router.Handle(route.method, route.path, HandlersChain{func(c *Context) {
output := H{"status": "good"}
for _, param := range c.Params {
output[param.Key] = param.Value

View File

@ -12,7 +12,7 @@ import (
// Used internally to configure router, a RouterGroup is associated with a prefix
// and an array of handlers (middlewares)
type RouterGroup struct {
Handlers []HandlerFunc
Handlers HandlersChain
absolutePath string
engine *Engine
}
@ -42,7 +42,7 @@ func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *R
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers []HandlerFunc) {
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers HandlersChain) {
absolutePath := group.calculateAbsolutePath(relativePath)
handlers = group.combineHandlers(handlers)
debugRoute(httpMethod, absolutePath, handlers)
@ -117,9 +117,9 @@ func (group *RouterGroup) createStaticHandler(absolutePath, root string) func(*C
}
}
func (group *RouterGroup) combineHandlers(handlers []HandlerFunc) []HandlerFunc {
func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
finalSize := len(group.Handlers) + len(handlers)
mergedHandlers := make([]HandlerFunc, finalSize)
mergedHandlers := make(HandlersChain, finalSize)
copy(mergedHandlers, group.Handlers)
copy(mergedHandlers[len(group.Handlers):], handlers)
return mergedHandlers

View File

@ -27,7 +27,7 @@ func testRouteOK(method string, t *testing.T) {
// SETUP
passed := false
r := New()
r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
r.Handle(method, "/test", HandlersChain{func(c *Context) {
passed = true
}})
// RUN
@ -43,7 +43,7 @@ func testRouteNotOK(method string, t *testing.T) {
// SETUP
passed := false
router := New()
router.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
router.Handle(method, "/test_2", HandlersChain{func(c *Context) {
passed = true
}})
@ -66,7 +66,7 @@ func testRouteNotOK2(method string, t *testing.T) {
} else {
methodRoute = "POST"
}
router.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
router.Handle(methodRoute, "/test", HandlersChain{func(c *Context) {
passed = true
}})

View File

@ -45,7 +45,7 @@ type node struct {
maxParams uint8
indices string
children []*node
handlers []HandlerFunc
handlers HandlersChain
priority uint32
}
@ -77,7 +77,7 @@ func (n *node) incrementChildPrio(pos int) int {
// addRoute adds a node with the given handle to the path.
// Not concurrency-safe!
func (n *node) addRoute(path string, handlers []HandlerFunc) {
func (n *node) addRoute(path string, handlers HandlersChain) {
fullPath := path
n.priority++
numParams := countParams(path)
@ -198,7 +198,7 @@ func (n *node) addRoute(path string, handlers []HandlerFunc) {
}
}
func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers []HandlerFunc) {
func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers HandlersChain) {
var offset int // already handled bytes of the path
// find prefix until first wildcard (beginning with ':'' or '*'')
@ -316,7 +316,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
// made if a handle exists with an extra (without the) trailing slash for the
// given path.
func (n *node) getValue(path string, po Params) (handlers []HandlerFunc, p Params, tsr bool) {
func (n *node) getValue(path string, po Params) (handlers HandlersChain, p Params, tsr bool) {
p = po
walk: // Outer loop for walking the tree
for {

View File

@ -24,8 +24,8 @@ func printChildren(n *node, prefix string) {
// Used as a workaround since we can't compare functions or their adresses
var fakeHandlerValue string
func fakeHandler(val string) []HandlerFunc {
return []HandlerFunc{func(c *Context) {
func fakeHandler(val string) HandlersChain {
return HandlersChain{func(c *Context) {
fakeHandlerValue = val
}}
}