Skip to content

Commit

Permalink
Ditched RouteInfo in favor of Route interface
Browse files Browse the repository at this point in the history
  • Loading branch information
José Miguel Molina Arboledas committed Apr 25, 2014
1 parent e1b8dea commit 8155718
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
33 changes: 23 additions & 10 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ func (r *router) findRoute(name string) *route {
type Route interface {
// URLWith returns a rendering of the Route's url with the given string params.
URLWith([]string) string
// Name sets a name for the route.
Name(string)
// GetName returns the name of the route.
GetName() string
// Pattern returns the pattern of the route.
Pattern() string
// Method returns the method of the route.
Method() string
}

type route struct {
Expand Down Expand Up @@ -247,14 +254,26 @@ func (r *route) Name(name string) {
r.name = name
}

func (r *route) GetName() string {
return r.name
}

func (r *route) Pattern() string {
return r.pattern
}

func (r *route) Method() string {
return r.method
}

// Routes is a helper service for Martini's routing layer.
type Routes interface {
// URLFor returns a rendered URL for the given route. Optional params can be passed to fulfill named parameters in the route.
URLFor(name string, params ...interface{}) string
// MethodsFor returns an array of methods available for the path
MethodsFor(path string) []string
// GetAllRoutes returns an array with all the routes in the router.
GetAllRoutes() []RouteInfo
All() []Route
}

// URLFor returns the url for the given route name.
Expand Down Expand Up @@ -282,17 +301,11 @@ func (r *router) URLFor(name string, params ...interface{}) string {
return route.URLWith(args)
}

// RouteInfo contains information about a route
type RouteInfo struct {
Pattern string
Method string
}

func (r *router) GetAllRoutes() []RouteInfo {
var ri = make([]RouteInfo, len(r.routes))
func (r *router) All() []Route {
var ri = make([]Route, len(r.routes))

for i, route := range r.routes {
ri[i] = RouteInfo{route.pattern, route.method}
ri[i] = Route(route)
}

return ri
Expand Down
16 changes: 9 additions & 7 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,20 @@ func Test_URLFor(t *testing.T) {
router.Handle(recorder, req, context)
}

func Test_GetAllRoutes(t *testing.T) {
func Test_AllRoutes(t *testing.T) {
router := NewRouter()

patterns := []string{"/foo", "/fee", "/fii"}
methods := []string{"GET", "POST", "DELETE"}
names := []string{"foo", "fee", "fii"}

router.Get("/foo", func() {})
router.Post("/fee", func() {})
router.Delete("/fii", func() {})
router.Get("/foo", func() {}).Name("foo")
router.Post("/fee", func() {}).Name("fee")
router.Delete("/fii", func() {}).Name("fii")

for i, r := range router.GetAllRoutes() {
expect(t, r.Pattern, patterns[i])
expect(t, r.Method, methods[i])
for i, r := range router.All() {
expect(t, r.Pattern(), patterns[i])
expect(t, r.Method(), methods[i])
expect(t, r.GetName(), names[i])
}
}

0 comments on commit 8155718

Please sign in to comment.