BasePath is not longer an exported field, but a method instead

This commit is contained in:
Manu Mtz-Almeida 2015-07-08 04:27:23 +02:00
parent 0873992f38
commit fc5e355724
4 changed files with 13 additions and 9 deletions

2
gin.go
View File

@ -94,7 +94,7 @@ func New() *Engine {
engine := &Engine{ engine := &Engine{
RouterGroup: RouterGroup{ RouterGroup: RouterGroup{
Handlers: nil, Handlers: nil,
BasePath: "/", basePath: "/",
root: true, root: true,
}, },
RedirectTrailingSlash: true, RedirectTrailingSlash: true,

View File

@ -23,7 +23,7 @@ func init() {
func TestCreateEngine(t *testing.T) { func TestCreateEngine(t *testing.T) {
router := New() router := New()
assert.Equal(t, "/", router.BasePath) assert.Equal(t, "/", router.basePath)
assert.Equal(t, router.engine, router) assert.Equal(t, router.engine, router)
assert.Empty(t, router.Handlers) assert.Empty(t, router.Handlers)
} }

View File

@ -39,7 +39,7 @@ type (
// and an array of handlers (middlewares) // and an array of handlers (middlewares)
RouterGroup struct { RouterGroup struct {
Handlers HandlersChain Handlers HandlersChain
BasePath string basePath string
engine *Engine engine *Engine
root bool root bool
} }
@ -58,11 +58,15 @@ func (group *RouterGroup) Use(middlewares ...HandlerFunc) IRoutes {
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup { func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
return &RouterGroup{ return &RouterGroup{
Handlers: group.combineHandlers(handlers), Handlers: group.combineHandlers(handlers),
BasePath: group.calculateAbsolutePath(relativePath), basePath: group.calculateAbsolutePath(relativePath),
engine: group.engine, engine: group.engine,
} }
} }
func (group *RouterGroup) BasePath() string {
return group.basePath
}
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) IRoutes { func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) IRoutes {
absolutePath := group.calculateAbsolutePath(relativePath) absolutePath := group.calculateAbsolutePath(relativePath)
handlers = group.combineHandlers(handlers) handlers = group.combineHandlers(handlers)
@ -200,7 +204,7 @@ func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain
} }
func (group *RouterGroup) calculateAbsolutePath(relativePath string) string { func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
return joinPaths(group.BasePath, relativePath) return joinPaths(group.basePath, relativePath)
} }
func (group *RouterGroup) returnObj() IRoutes { func (group *RouterGroup) returnObj() IRoutes {

View File

@ -20,14 +20,14 @@ func TestRouterGroupBasic(t *testing.T) {
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")
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)
assert.Equal(t, group2.BasePath, "/hola/manu") assert.Equal(t, group2.BasePath(), "/hola/manu")
assert.Equal(t, group2.engine, router) assert.Equal(t, group2.engine, router)
} }
@ -44,10 +44,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) {})
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) {})
assert.Equal(t, login.BasePath, "/v1/login/") assert.Equal(t, login.BasePath(), "/v1/login/")
handler := func(c *Context) { handler := func(c *Context) {
c.String(400, "the method was %s and index %d", c.Request.Method, c.index) c.String(400, "the method was %s and index %d", c.Request.Method, c.index)