pattern

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func CheckNames(pattern string) error

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

func FindMatchingBrace(pattern string, openIdx int) int

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

func ParamName(inner string) string

ParamName returns the parameter name of the text between one pair of braces: "id" for "id", "id:[0-9]+" and "id...".

func ParamNames

func ParamNames(pattern string) []string

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.

const (
	Static   Kind = iota // literal path text (e.g. "/users")
	Regexp               // regex-constrained parameter (e.g. "{id:[0-9]+}")
	Param                // named parameter (e.g. "{id}")
	CatchAll             // catch-all parameter (e.g. "{path...}")
)

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

func NewMatcher(pattern string) (*Matcher, error)

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

func (m *Matcher) Match(path string) ([]string, bool)

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

func (m *Matcher) Names() []string

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

func NextSegment(pattern string) (Segment, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL