Add BuildVarsFunc to allow modifying route variables before generating a route's URL

This commit is contained in:
Quinn Slack
2014-04-17 01:17:15 -07:00
parent 270c42505a
commit cef3b0cd6d
4 changed files with 70 additions and 10 deletions

View File

@@ -31,6 +31,8 @@ type Route struct {
name string
// Error resulted from building a route.
err error
buildVarsFunc BuildVarsFunc
}
// Match matches the route against the request.
@@ -336,6 +338,19 @@ func (r *Route) Schemes(schemes ...string) *Route {
return r.addMatcher(schemeMatcher(schemes))
}
// BuildVarsFunc --------------------------------------------------------------
// BuildVarsFunc is the function signature used by custom build variable
// functions (which can modify route variables before a route's URL is built).
type BuildVarsFunc func(map[string]string) map[string]string
// BuildVarsFunc adds a custom function to be used to modify build variables
// before a route's URL is built.
func (r *Route) BuildVarsFunc(f BuildVarsFunc) *Route {
r.buildVarsFunc = f
return r
}
// Subrouter ------------------------------------------------------------------
// Subrouter creates a subrouter for the route.
@@ -398,17 +413,20 @@ func (r *Route) URL(pairs ...string) (*url.URL, error) {
if r.regexp == nil {
return nil, errors.New("mux: route doesn't have a host or path")
}
values, err := r.buildVars(pairs...)
if err != nil {
return nil, err
}
var scheme, host, path string
var err error
if r.regexp.host != nil {
// Set a default scheme.
scheme = "http"
if host, err = r.regexp.host.url(pairs...); err != nil {
if host, err = r.regexp.host.url(values); err != nil {
return nil, err
}
}
if r.regexp.path != nil {
if path, err = r.regexp.path.url(pairs...); err != nil {
if path, err = r.regexp.path.url(values); err != nil {
return nil, err
}
}
@@ -429,7 +447,11 @@ func (r *Route) URLHost(pairs ...string) (*url.URL, error) {
if r.regexp == nil || r.regexp.host == nil {
return nil, errors.New("mux: route doesn't have a host")
}
host, err := r.regexp.host.url(pairs...)
values, err := r.buildVars(pairs...)
if err != nil {
return nil, err
}
host, err := r.regexp.host.url(values)
if err != nil {
return nil, err
}
@@ -449,7 +471,11 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
if r.regexp == nil || r.regexp.path == nil {
return nil, errors.New("mux: route doesn't have a path")
}
path, err := r.regexp.path.url(pairs...)
values, err := r.buildVars(pairs...)
if err != nil {
return nil, err
}
path, err := r.regexp.path.url(values)
if err != nil {
return nil, err
}
@@ -458,6 +484,19 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
}, nil
}
// buildVars converts the route variable pairs into a map. If the route has a
// BuildVarsFunc, it is invoked.
func (r *Route) buildVars(pairs ...string) (map[string]string, error) {
m, err := mapFromPairs(pairs...)
if err != nil {
return nil, err
}
if r.buildVarsFunc != nil {
m = r.buildVarsFunc(m)
}
return m, nil
}
// ----------------------------------------------------------------------------
// parentRoute
// ----------------------------------------------------------------------------