url builds a URL part using the given values.
(values map[string]string)
| 195 | |
| 196 | // url builds a URL part using the given values. |
| 197 | func (r *routeRegexp) url(values map[string]string) (string, error) { |
| 198 | urlValues := make([]interface{}, len(r.varsN)) |
| 199 | for k, v := range r.varsN { |
| 200 | value, ok := values[v] |
| 201 | if !ok { |
| 202 | return "", fmt.Errorf("mux: missing route variable %q", v) |
| 203 | } |
| 204 | if r.regexpType == regexpTypeQuery { |
| 205 | value = url.QueryEscape(value) |
| 206 | } |
| 207 | urlValues[k] = value |
| 208 | } |
| 209 | rv := fmt.Sprintf(r.reverse, urlValues...) |
| 210 | if !r.regexp.MatchString(rv) { |
| 211 | // The URL is checked against the full regexp, instead of checking |
| 212 | // individual variables. This is faster but to provide a good error |
| 213 | // message, we check individual regexps if the URL doesn't match. |
| 214 | for k, v := range r.varsN { |
| 215 | if !r.varsR[k].MatchString(values[v]) { |
| 216 | return "", fmt.Errorf( |
| 217 | "mux: variable %q doesn't match, expected %q", values[v], |
| 218 | r.varsR[k].String()) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | return rv, nil |
| 223 | } |
| 224 | |
| 225 | // getURLQuery returns a single query parameter from a request URL. |
| 226 | // For a URL with foo=bar&baz=ding, we return only the relevant key |