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.
func ToRegexp ¶
ToRegexp converts a whole pattern into an anchored regexp with one capture group per parameter: {name} → ([^/]+), {name...} → (.*), {name:re} → (re). names[0] is "" so the slice aligns with regexp.FindStringSubmatch. A pattern that repeats a parameter name is rejected (see CheckNames).
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 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.