Documentation ¶
Overview ¶
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.
Index ¶
- type All
- type Builder
- type Extractor
- type Func
- type GorillaHost
- type GorillaPath
- type GorillaPathPrefix
- type Header
- type Host
- type Matcher
- type Method
- type None
- type One
- type Path
- type PathPrefix
- type PathRedirect
- type Query
- type Regexp
- func (r *Regexp) Compiled() *regexp.Regexp
- func (r *Regexp) Groups() []string
- func (r *Regexp) Indices() []int
- func (r *Regexp) MatchString(s string) bool
- func (r *Regexp) Revert(values url.Values) (string, error)
- func (r *Regexp) RevertValid(values url.Values) (string, error)
- func (r *Regexp) Template() string
- func (r *Regexp) Values(s string) url.Values
- type RegexpHost
- type RegexpPath
- type Result
- type Scheme
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type All ¶
type All []Matcher
All is a set of matchers, and all of them must match.
type GorillaHost ¶
type GorillaHost struct {
Regexp
}
GorillaHost matches a URL host using Gorilla's special syntax for named groups: `{name:regexp}`.
func NewGorillaHost ¶
func NewGorillaHost(pattern string) (*GorillaHost, error)
func (*GorillaHost) Build ¶
Build builds the URL host using the given positional and named variables, and writes it to the given URL.
type GorillaPath ¶
type GorillaPath struct { Regexp // contains filtered or unexported fields }
GorillaPath matches a URL path using Gorilla's special syntax for named groups: `{name:regexp}`.
func NewGorillaPath ¶
func NewGorillaPath(pattern string, strictSlash bool) (*GorillaPath, error)
func (*GorillaPath) Build ¶
Build builds the URL path using the given positional and named variables, and writes it to the given URL.
type GorillaPathPrefix ¶
type GorillaPathPrefix struct {
Regexp
}
GorillaPathPrefix matches a URL path prefix using Gorilla's special syntax for named groups: `{name:regexp}`.
func NewGorillaPathPrefix ¶
func NewGorillaPathPrefix(pattern string) (*GorillaPathPrefix, error)
func (*GorillaPathPrefix) Build ¶
Build builds the URL path using the given positional and named variables, and writes it to the given URL.
type Header ¶
Header matches request headers. All values, if non-empty, must match. Empty values only check if the header is present.
type Method ¶
type Method []string
Method matches the request method. One of the values must match.
type One ¶
type One []Matcher
One is a set of matchers, and at least one of them must match.
type PathPrefix ¶
type PathPrefix string
PathPrefix matches a static URL path prefix.
func NewPathPrefix ¶
func NewPathPrefix(prefix string) PathPrefix
NewPathPrefix returns a static URL path prefix matcher.
type PathRedirect ¶
type PathRedirect string
PathRedirect matches a static URL path and redirects to the trailing-slash or non-trailing-slash version if it differs.
func NewPathRedirect ¶
func NewPathRedirect(path string) PathRedirect
NewPathRedirect returns a static URL path matcher that redirects if the trailing slash differs.
type Query ¶
Query matches URL queries. All values, if non-empty, must match. Empty values only check if the query is present.
type Regexp ¶
type Regexp struct {
// contains filtered or unexported fields
}
Regexp stores a regular expression that can be "reverted" or "built": outermost capturing groups become placeholders to be filled by variables.
func CompileRegexp ¶
CompileRegexp compiles a regular expression pattern and creates a template to revert it.
func (*Regexp) Groups ¶
Groups returns an ordered list of the outermost capturing groups found in the regexp.
Positional groups are listed as an empty string and named groups use the group name.
func (*Regexp) Indices ¶
Indices returns the indices of the outermost capturing groups found in the regexp.
Not all indices may be present because nested capturing groups are ignored.
func (*Regexp) MatchString ¶
Match returns whether the regexp matches the given string.
func (*Regexp) Revert ¶
Revert builds a string for this regexp using the given values. Positional values use an empty string as key.
The values are modified in place, and only the unused ones are left.
func (*Regexp) RevertValid ¶
RevertValid is the same as Revert but it also validates the resulting string matching it against the compiled regexp.
The values are modified in place, and only the unused ones are left.
type RegexpHost ¶
type RegexpHost struct {
Regexp
}
RegexpHost matches the URL host against a regular expression. The outermost capturing groups are extracted and the host can be reverted.
func NewRegexpHost ¶
func NewRegexpHost(pattern string) (*RegexpHost, error)
NewRegexpHost returns a regexp matcher for the given URL host pattern.
func (*RegexpHost) Build ¶
Build builds the URL host using the given positional and named variables, and writes it to the given URL.
type RegexpPath ¶
type RegexpPath struct {
Regexp
}
RegexpPath matches the URL path against a regular expression. The outermost capturing groups are extracted and the path can be reverted.
func NewRegexpPath ¶
func NewRegexpPath(pattern string) (*RegexpPath, error)
NewRegexpPath returns a regexp matcher for the given URL path pattern.
func (*RegexpPath) Build ¶
Build builds the URL path using the given positional and named variables, and writes it to the given URL.