From eb3e9293edcf988c66ce5a4fcdc2ebb75a6a8661 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Thu, 7 May 2015 11:30:01 +0200 Subject: [PATCH] Renames []HandleFunc to HandlersChain --- context.go | 2 +- context_test.go | 2 +- debug.go | 2 +- debug_test.go | 2 +- gin.go | 13 +++++++------ gin_test.go | 6 +++--- githubapi_test.go | 2 +- routergroup.go | 8 ++++---- routes_test.go | 6 +++--- tree.go | 8 ++++---- tree_test.go | 4 ++-- 11 files changed, 28 insertions(+), 27 deletions(-) diff --git a/context.go b/context.go index 65eef13..b99e54c 100644 --- a/context.go +++ b/context.go @@ -59,7 +59,7 @@ type Context struct { Writer ResponseWriter Params Params - handlers []HandlerFunc + handlers HandlersChain index int8 Engine *Engine diff --git a/context_test.go b/context_test.go index 54c3581..2c48abc 100644 --- a/context_test.go +++ b/context_test.go @@ -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") diff --git a/debug.go b/debug.go index 6c04aa0..b52356a 100644 --- a/debug.go +++ b/debug.go @@ -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]) diff --git a/debug_test.go b/debug_test.go index 12a931e..e960568 100644 --- a/debug_test.go +++ b/debug_test.go @@ -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) { diff --git a/gin.go b/gin.go index 4151cae..0cdd10f 100644 --- a/gin.go +++ b/gin.go @@ -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 '/'") } diff --git a/gin_test.go b/gin_test.go index ec0ad6b..5efb79f 100644 --- a/gin_test.go +++ b/gin_test.go @@ -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) { diff --git a/githubapi_test.go b/githubapi_test.go index 4ce33c1..f60d540 100644 --- a/githubapi_test.go +++ b/githubapi_test.go @@ -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 diff --git a/routergroup.go b/routergroup.go index 760bae4..a231643 100644 --- a/routergroup.go +++ b/routergroup.go @@ -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 diff --git a/routes_test.go b/routes_test.go index 2c34c92..aac1002 100644 --- a/routes_test.go +++ b/routes_test.go @@ -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 }}) diff --git a/tree.go b/tree.go index 8cd67e7..169e5f1 100644 --- a/tree.go +++ b/tree.go @@ -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 { diff --git a/tree_test.go b/tree_test.go index 800e751..4e2cb7f 100644 --- a/tree_test.go +++ b/tree_test.go @@ -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 }} }