Skip to content
Permalink
Browse files

Add support for queries in URL reversing.

  • Loading branch information
Pavel Ivanov authored and kisielk committed Feb 19, 2017
1 parent 043ee65 commit 9c9af153a18415409f478fe09658a3af59366515
Showing with 15 additions and 3 deletions.
  1. +3 −0 regexp.go
  2. +12 −3 route.go
@@ -178,6 +178,9 @@ func (r *routeRegexp) url(values map[string]string) (string, error) {
if !ok {
return "", fmt.Errorf("mux: missing route variable %q", v)
}
if r.matchQuery {
value = url.QueryEscape(value)
}
urlValues[k] = value
}
rv := fmt.Sprintf(r.reverse, urlValues...)
@@ -482,6 +482,7 @@ func (r *Route) URL(pairs ...string) (*url.URL, error) {
return nil, err
}
var scheme, host, path string
queries := make([]string, 0, len(r.regexp.queries))
if r.regexp.host != nil {
if host, err = r.regexp.host.url(values); err != nil {
return nil, err
@@ -496,10 +497,18 @@ func (r *Route) URL(pairs ...string) (*url.URL, error) {
return nil, err
}
}
for _, q := range r.regexp.queries {
var query string
if query, err = q.url(values); err != nil {
return nil, err
}
queries = append(queries, query)
}
return &url.URL{
Scheme: scheme,
Host: host,
Path: path,
Scheme: scheme,
Host: host,
Path: path,
RawQuery: strings.Join(queries, "&"),
}, nil
}

0 comments on commit 9c9af15

Please sign in to comment.
You can’t perform that action at this time.