mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 14:12:16 +08:00
Make Group return IRouter
This commit is contained in:
parent
eadf4da8f6
commit
e539c8c828
@ -46,7 +46,7 @@ func NoMethod(handlers ...HandlerFunc) {
|
|||||||
|
|
||||||
// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
||||||
// For example, all the routes that use a common middlware for authorization could be grouped.
|
// For example, all the routes that use a common middlware for authorization could be grouped.
|
||||||
func Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
|
func Group(relativePath string, handlers ...HandlerFunc) IRouter {
|
||||||
return engine().Group(relativePath, handlers...)
|
return engine().Group(relativePath, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
type (
|
type (
|
||||||
IRouter interface {
|
IRouter interface {
|
||||||
IRoutes
|
IRoutes
|
||||||
Group(string, ...HandlerFunc) *RouterGroup
|
Group(string, ...HandlerFunc) IRouter
|
||||||
}
|
}
|
||||||
|
|
||||||
IRoutes interface {
|
IRoutes interface {
|
||||||
@ -55,7 +55,7 @@ func (group *RouterGroup) Use(middleware ...HandlerFunc) IRoutes {
|
|||||||
|
|
||||||
// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
||||||
// For example, all the routes that use a common middlware for authorization could be grouped.
|
// For example, all the routes that use a common middlware for authorization could be grouped.
|
||||||
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
|
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) IRouter {
|
||||||
return &RouterGroup{
|
return &RouterGroup{
|
||||||
Handlers: group.combineHandlers(handlers),
|
Handlers: group.combineHandlers(handlers),
|
||||||
basePath: group.calculateAbsolutePath(relativePath),
|
basePath: group.calculateAbsolutePath(relativePath),
|
||||||
|
@ -16,14 +16,14 @@ func init() {
|
|||||||
|
|
||||||
func TestRouterGroupBasic(t *testing.T) {
|
func TestRouterGroupBasic(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
group := router.Group("/hola", func(c *Context) {})
|
group := router.Group("/hola", func(c *Context) {}).(*RouterGroup)
|
||||||
group.Use(func(c *Context) {})
|
group.Use(func(c *Context) {})
|
||||||
|
|
||||||
assert.Len(t, group.Handlers, 2)
|
assert.Len(t, group.Handlers, 2)
|
||||||
assert.Equal(t, group.BasePath(), "/hola")
|
assert.Equal(t, group.BasePath(), "/hola")
|
||||||
assert.Equal(t, group.engine, router)
|
assert.Equal(t, group.engine, router)
|
||||||
|
|
||||||
group2 := group.Group("manu")
|
group2 := group.Group("manu").(*RouterGroup)
|
||||||
group2.Use(func(c *Context) {}, func(c *Context) {})
|
group2.Use(func(c *Context) {}, func(c *Context) {})
|
||||||
|
|
||||||
assert.Len(t, group2.Handlers, 4)
|
assert.Len(t, group2.Handlers, 4)
|
||||||
@ -43,10 +43,10 @@ func TestRouterGroupBasicHandle(t *testing.T) {
|
|||||||
|
|
||||||
func performRequestInGroup(t *testing.T, method string) {
|
func performRequestInGroup(t *testing.T, method string) {
|
||||||
router := New()
|
router := New()
|
||||||
v1 := router.Group("v1", func(c *Context) {})
|
v1 := router.Group("v1", func(c *Context) {}).(*RouterGroup)
|
||||||
assert.Equal(t, v1.BasePath(), "/v1")
|
assert.Equal(t, v1.BasePath(), "/v1")
|
||||||
|
|
||||||
login := v1.Group("/login/", func(c *Context) {}, func(c *Context) {})
|
login := v1.Group("/login/", func(c *Context) {}, func(c *Context) {}).(*RouterGroup)
|
||||||
assert.Equal(t, login.BasePath(), "/v1/login/")
|
assert.Equal(t, login.BasePath(), "/v1/login/")
|
||||||
|
|
||||||
handler := func(c *Context) {
|
handler := func(c *Context) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user