Sfoglia il codice sorgente

Renames []HandleFunc to HandlersChain

Manu Mtz-Almeida 10 anni fa
parent
commit
eb3e9293ed
11 ha cambiato i file con 28 aggiunte e 27 eliminazioni
  1. 1 1
      context.go
  2. 1 1
      context_test.go
  3. 1 1
      debug.go
  4. 1 1
      debug_test.go
  5. 7 6
      gin.go
  6. 3 3
      gin_test.go
  7. 1 1
      githubapi_test.go
  8. 4 4
      routergroup.go
  9. 3 3
      routes_test.go
  10. 4 4
      tree.go
  11. 2 2
      tree_test.go

+ 1 - 1
context.go

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

+ 1 - 1
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")
 

+ 1 - 1
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])

+ 1 - 1
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) {

+ 7 - 6
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 '/'")
 	}

+ 3 - 3
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) {

+ 1 - 1
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

+ 4 - 4
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

+ 3 - 3
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
 	}})
 

+ 4 - 4
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 {

+ 2 - 2
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
 	}}
 }