Skip to content

Commit

Permalink
Add support for queries in URL reversing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Ivanov authored and kisielk committed Jun 2, 2017
1 parent 043ee65 commit 9c9af15
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
15 changes: 12 additions & 3 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 9c9af15

Please sign in to comment.