Browse Source

Call parent BuildVarsFuncs

Quinn Slack 12 years ago
parent
commit
a883d5a9b9
3 changed files with 35 additions and 6 deletions
  1. +7
    -0
      mux.go
  2. +14
    -0
      mux_test.go
  3. +14
    -6
      route.go

+ 7
- 0
mux.go View File

@ -146,6 +146,13 @@ func (r *Router) getRegexpGroup() *routeRegexpGroup {
return nil return nil
} }
func (r *Router) buildVars(m map[string]string) map[string]string {
if r.parent != nil {
m = r.parent.buildVars(m)
}
return m
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Route factories // Route factories
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------


+ 14
- 0
mux_test.go View File

@ -514,6 +514,7 @@ func TestMatcherFunc(t *testing.T) {
func TestBuildVarsFunc(t *testing.T) { func TestBuildVarsFunc(t *testing.T) {
tests := []routeTest{ tests := []routeTest{
{ {
title: "BuildVarsFunc set on route",
route: new(Route).Path(`/111/{v1:\d}{v2:.*}`).BuildVarsFunc(func(vars map[string]string) map[string]string { route: new(Route).Path(`/111/{v1:\d}{v2:.*}`).BuildVarsFunc(func(vars map[string]string) map[string]string {
vars["v1"] = "3" vars["v1"] = "3"
vars["v2"] = "a" vars["v2"] = "a"
@ -523,6 +524,19 @@ func TestBuildVarsFunc(t *testing.T) {
path: "/111/3a", path: "/111/3a",
shouldMatch: true, shouldMatch: true,
}, },
{
title: "BuildVarsFunc set on route and parent route",
route: new(Route).PathPrefix(`/{v1:\d}`).BuildVarsFunc(func(vars map[string]string) map[string]string {
vars["v1"] = "2"
return vars
}).Subrouter().Path(`/{v2:\w}`).BuildVarsFunc(func(vars map[string]string) map[string]string {
vars["v2"] = "b"
return vars
}),
request: newRequest("GET", "http://localhost/1/a"),
path: "/2/b",
shouldMatch: true,
},
} }
for _, test := range tests { for _, test := range tests {


+ 14
- 6
route.go View File

@ -413,7 +413,7 @@ func (r *Route) URL(pairs ...string) (*url.URL, error) {
if r.regexp == nil { if r.regexp == nil {
return nil, errors.New("mux: route doesn't have a host or path") return nil, errors.New("mux: route doesn't have a host or path")
} }
values, err := r.buildVars(pairs...)
values, err := r.prepareVars(pairs...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -447,7 +447,7 @@ func (r *Route) URLHost(pairs ...string) (*url.URL, error) {
if r.regexp == nil || r.regexp.host == nil { if r.regexp == nil || r.regexp.host == nil {
return nil, errors.New("mux: route doesn't have a host") return nil, errors.New("mux: route doesn't have a host")
} }
values, err := r.buildVars(pairs...)
values, err := r.prepareVars(pairs...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -471,7 +471,7 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
if r.regexp == nil || r.regexp.path == nil { if r.regexp == nil || r.regexp.path == nil {
return nil, errors.New("mux: route doesn't have a path") return nil, errors.New("mux: route doesn't have a path")
} }
values, err := r.buildVars(pairs...)
values, err := r.prepareVars(pairs...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -484,17 +484,24 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
}, nil }, nil
} }
// buildVars converts the route variable pairs into a map. If the route has a
// prepareVars converts the route variable pairs into a map. If the route has a
// BuildVarsFunc, it is invoked. // BuildVarsFunc, it is invoked.
func (r *Route) buildVars(pairs ...string) (map[string]string, error) {
func (r *Route) prepareVars(pairs ...string) (map[string]string, error) {
m, err := mapFromPairs(pairs...) m, err := mapFromPairs(pairs...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return r.buildVars(m), nil
}
func (r *Route) buildVars(m map[string]string) map[string]string {
if r.parent != nil {
m = r.parent.buildVars(m)
}
if r.buildVarsFunc != nil { if r.buildVarsFunc != nil {
m = r.buildVarsFunc(m) m = r.buildVarsFunc(m)
} }
return m, nil
return m
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -505,6 +512,7 @@ func (r *Route) buildVars(pairs ...string) (map[string]string, error) {
type parentRoute interface { type parentRoute interface {
getNamedRoutes() map[string]*Route getNamedRoutes() map[string]*Route
getRegexpGroup() *routeRegexpGroup getRegexpGroup() *routeRegexpGroup
buildVars(map[string]string) map[string]string
} }
// getNamedRoutes returns the map where named routes are registered. // getNamedRoutes returns the map where named routes are registered.


Loading…
Cancel
Save