Skip to content

Commit

Permalink
Renames []HandleFunc to HandlersChain
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed May 7, 2015
1 parent 79131ac commit eb3e929
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Context struct {
Writer ResponseWriter

Params Params
handlers []HandlerFunc
handlers HandlersChain
index int8

Engine *Engine
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 7 additions & 6 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '/'")
}
Expand Down
6 changes: 3 additions & 3 deletions gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion githubapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions routergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}})

Expand All @@ -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
}})

Expand Down
8 changes: 4 additions & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type node struct {
maxParams uint8
indices string
children []*node
handlers []HandlerFunc
handlers HandlersChain
priority uint32
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 '*'')
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}}
}
Expand Down

0 comments on commit eb3e929

Please sign in to comment.