Documentation
¶
Overview ¶
Package pattern owns Credo's route-pattern grammar: {name} matches one path segment, {name:regex} constrains it, and {name...} captures the rest of the path. The radix tree, route URL building, host/route parameter checks, and the Rewrite middleware all parse patterns through this package so the grammar has exactly one implementation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckNames ¶ added in v0.23.0
CheckNames reports an error when pattern uses a parameter name more than once. A name is how a capture is read; with a name used twice, one capture is unreadable and lookups that scan from different ends answer with different captures. Parsing stops at the first malformed parameter, which NextSegment reports.
func FindMatchingBrace ¶
FindMatchingBrace returns the index of the closing '}' that matches the opening '{' at openIdx, accounting for nested braces in regex quantifiers like {id:[0-9]{2,4}}, escaped braces (\{, \}), and character classes ([...]). It returns -1 when no matching brace exists.
func ParamName ¶
ParamName returns the parameter name of the text between one pair of braces: "id" for "id", "id:[0-9]+" and "id...".
func ParamNames ¶
ParamNames returns every parameter name in pattern in order. Parsing stops at the first malformed parameter, mirroring how registration reports the error separately; the names collected so far are returned.
Types ¶
type Kind ¶
type Kind uint8
Kind classifies a parsed pattern segment. The numeric order is the routing priority the radix tree relies on (static before regexp before param before catch-all), so the values must not be reordered.
type Matcher ¶ added in v0.26.0
type Matcher struct {
// contains filtered or unexported fields
}
Matcher matches a canonical wire path (see wirepath.Canonical) against one pattern the way the router matches a route: literal text in canonical form (see wirepath.Static); a {name} or {name:regex} candidate that ends at the parameter's tail byte or the next slash, whichever comes first, with every escape one unit (see wirepath.CandidateEnd), so it never spans a raw slash and an escaped slash ("%2F") is data; a constraint checked on the whole decoded value; and a {name...} catch-all that takes the rest of the path, an empty rest included. A capture that decodes to invalid UTF-8 is no match. Each capture is reported in canonical wire form, which decodes once to the value a route parameter would have.
func NewMatcher ¶ added in v0.26.0
NewMatcher parses pattern into a Matcher. A malformed parameter, a repeated parameter name (see CheckNames) and literal text after a catch-all, which takes the rest of the path, are errors.
func (*Matcher) Match ¶ added in v0.26.0
Match reports whether the canonical wire path matches the pattern and returns one canonical capture per parameter.
func (*Matcher) Names ¶ added in v0.26.0
Names returns the parameter names in pattern order, aligned with the captures Matcher.Match returns.
type Segment ¶
type Segment struct {
// Kind is the segment kind.
Kind Kind
// Prefix is the literal text before the parameter (the whole pattern for
// Static).
Prefix string
// Name is the parameter name without braces, "..." or the constraint.
Name string
// Regexp is the constraint compiled as "^(?:regex)$" for Regexp segments:
// a constraint always applies to a whole decoded parameter value, never to
// a prefix of it.
Regexp *regexp.Regexp
// RegexpSource is the raw constraint text for Regexp segments.
RegexpSource string
// Suffix is the remaining pattern after the closing brace.
Suffix string
// TailByte is the first byte of Suffix (0 when Suffix is empty); the tree
// uses it to bound parameter scanning.
TailByte byte
}
Segment is the parsed result of the next parameter in a pattern.
func NextSegment ¶
NextSegment parses the next segment of pattern. A pattern without an opening brace, or with a trailing "{" at its very end, is one Static segment. Malformed parameters (unclosed brace, empty name, empty or invalid regex) return an error.