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 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 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.
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.