Categorygithub.com/gorilla/reverse
modulepackage
1.0.1
Repository: https://github.com/gorilla/reverse.git
Documentation: pkg.go.dev

# README

reverse

testing codecov godoc sourcegraph

Gorilla Logo

Package gorilla/reverse is a set of utilities to create request routers.

It provides interfaces to match and extract variables from an HTTP request and build URLs for registered routes. It also has a variety of matcher implementations for all kinds of request attributes, among other utilities.

For example, the Regexp type produces reversible regular expressions that can be used to generate URLs for a regexp-based mux. To demonstrate, let's compile a simple regexp:

regexp, err := reverse.CompileRegexp(`/foo/1(\d+)3`)

Now we can call regexp.Revert() passing variables to fill the capturing groups. Because our variable is not named, we use an empty string as key for url.Values, like this:

// url is "/foo/123".
url, err := regexp.Revert(url.Values{"": {"2"}})

Non-capturing groups are ignored, but named capturing groups can be filled normally. Just set the key in url.Values:

regexp, err := reverse.CompileRegexp(`/foo/1(?P<two>\d+)3`)
if err != nil {
    panic(err)
}
// url is "/foo/123".
url, err := re.Revert(url.Values{"two": {"2"}})

There are a few limitations that can't be changed:

  1. Nested capturing groups are ignored; only the outermost groups become a placeholder. So in 1(\d+([a-z]+))3 there is only one placeholder although there are two capturing groups: re.Revert(url.Values{"": {"2", "a"}}) results in "123" and not "12a3".
  2. Literals inside capturing groups are ignored; the whole group becomes a placeholder.

# Functions

CompileRegexp compiles a regular expression pattern and creates a template to revert it.
NewAll returns a group of matchers that succeeds only if all of them match.
No description provided by the author
No description provided by the author
No description provided by the author
NewHeader returns a header matcher, converting keys to the canonical form.
NewHost returns a static URL host matcher.
NewMethod retuns a request method matcher, converting values to upper-case.
NewNone returns a matcher that never matches.
NewOne returns a group of matchers that succeeds if one of them matches.
NewPath returns a static URL path matcher.
NewPathPrefix returns a static URL path prefix matcher.
NewPathRedirect returns a static URL path matcher that redirects if the trailing slash differs.
NewQuery returns a URL query matcher.
NewRegexpHost returns a regexp matcher for the given URL host pattern.
NewRegexpPath returns a regexp matcher for the given URL path pattern.
NewScheme retuns a URL scheme matcher, converting values to lower-case.

# Structs

GorillaHost matches a URL host using Gorilla's special syntax for named groups: `{name:regexp}`.
GorillaPath matches a URL path using Gorilla's special syntax for named groups: `{name:regexp}`.
GorillaPathPrefix matches a URL path prefix using Gorilla's special syntax for named groups: `{name:regexp}`.
Regexp stores a regular expression that can be "reverted" or "built": outermost capturing groups become placeholders to be filled by variables.
RegexpHost matches the URL host against a regular expression.
RegexpPath matches the URL path against a regular expression.
Result stores the results from a match.

# Interfaces

Builder builds a URL based on positional and/or named variables.
Extractor extracts variables from a request.
Matcher matches a request.

# Type aliases

All is a set of matchers, and all of them must match.
Func is a function signature for custom matchers.
Header matches request headers.
Host matches a static URL host.
Method matches the request method.
None never matches.
One is a set of matchers, and at least one of them must match.
Path matches a static URL path.
PathPrefix matches a static URL path prefix.
PathRedirect matches a static URL path and redirects to the trailing-slash or non-trailing-slash version if it differs.
Query matches URL queries.
Scheme matches the URL scheme.