Skip to content

Commit

Permalink
break up group function
Browse files Browse the repository at this point in the history
Group handled 3 separate pieces of logic, which have now been broken out for clarity and ease of use into:
Group - groups retaining existing middleware
GroupWithMore - groups retaining middleware and adding additional
GroupWithNone - groups but retains no middleware
  • Loading branch information
Dean Karn committed Jun 25, 2017
1 parent 5853c10 commit b0c58b5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## LARS
<img align="right" src="https://raw.githubusercontent.com/go-playground/lars/master/examples/README/test.gif">![Project status](https://img.shields.io/badge/version-3.7.0-green.svg)
<img align="right" src="https://raw.githubusercontent.com/go-playground/lars/master/examples/README/test.gif">![Project status](https://img.shields.io/badge/version-4.0.0-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/projects/4351aa2d-2f94-40be-a6ef-85c248490378/679708/badge.svg)](https://semaphoreci.com/joeybloggs/lars)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/lars/badge.svg?branch=master)](https://coveralls.io/github/go-playground/lars?branch=master)
[![Go Report Card](https://goreportcard.com/badge/go-playground/lars)](https://goreportcard.com/report/go-playground/lars)
Expand Down
47 changes: 26 additions & 21 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
// IRouteGroup interface for router group
type IRouteGroup interface {
IRoutes
Group(prefix string, middleware ...Handler) IRouteGroup
GroupWithNone(prefix string) IRouteGroup
GroupWithMore(prefix string, middleware ...Handler) IRouteGroup
Group(prefix string) IRouteGroup
}

// IRoutes interface for routes
Expand Down Expand Up @@ -176,31 +178,34 @@ func (g *routeGroup) WebSocket(upgrader websocket.Upgrader, path string, h Handl
}, handler)
}

// Group creates a new sub router with prefix. It inherits all properties from
// the parent. Passing middleware overrides parent middleware but still keeps
// the root level middleware intact.
func (g *routeGroup) Group(prefix string, middleware ...Handler) IRouteGroup {

rg := &routeGroup{
prefix: g.prefix + prefix,
lars: g.lars,
}

if len(middleware) == 0 {
rg.middleware = make(HandlersChain, len(g.middleware))
copy(rg.middleware, g.middleware)

return rg
// GroupWithNone creates a new sub router with specified prefix and no middleware attached.
func (g *routeGroup) GroupWithNone(prefix string) IRouteGroup {
return &routeGroup{
prefix: g.prefix + prefix,
lars: g.lars,
middleware: make(HandlersChain, 0),
}
}

if middleware[0] == nil {
rg.middleware = make(HandlersChain, 0)
return rg
// GroupWithMore creates a new sub router with specified prefix, retains existing middleware and adds new middleware.
func (g *routeGroup) GroupWithMore(prefix string, middleware ...Handler) IRouteGroup {
rg := &routeGroup{
prefix: g.prefix + prefix,
lars: g.lars,
middleware: make(HandlersChain, len(g.middleware)),
}

rg.middleware = make(HandlersChain, len(g.middleware))
copy(rg.middleware, g.middleware)
rg.Use(middleware...)
return rg
}

// Group creates a new sub router with specified prefix and retains existing middleware.
func (g *routeGroup) Group(prefix string) IRouteGroup {
rg := &routeGroup{
prefix: g.prefix + prefix,
lars: g.lars,
middleware: make(HandlersChain, len(g.middleware)),
}
copy(rg.middleware, g.middleware)
return rg
}
6 changes: 3 additions & 3 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ func TestGrouplogic(t *testing.T) {
c.Next()
})

a := l.Group("/a", aM)
a := l.GroupWithMore("/a", aM)
a.Get("/test", func(c Context) {
c.JSON(http.StatusOK, "a-ok")
})

b := a.Group("/b", bM)
b := a.GroupWithMore("/b", bM)
b.Get("/test", func(c Context) {
c.JSON(http.StatusOK, "b-ok")
})

c := b.Group("/c", cM)
c := b.GroupWithMore("/c", cM)
c.Get("/test", func(c Context) {
c.JSON(http.StatusOK, "c-ok")
})
Expand Down
4 changes: 2 additions & 2 deletions lars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func TestUseAndGroup(t *testing.T) {
c.Next()
}

sh := l.Group("/superheros", logger2)
sh := l.GroupWithMore("/superheros", logger2)
sh.Get("/", fn)
sh.Get("/list/", fn)

Expand Down Expand Up @@ -381,7 +381,7 @@ func TestUseAndGroup(t *testing.T) {

log = ""

g2 := l.Group("/admins", nil)
g2 := l.GroupWithNone("/admins")
g2.Get("/", fn)
g2.Get("/list/", fn)

Expand Down

0 comments on commit b0c58b5

Please sign in to comment.