pattern

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 3 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 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.

func ToRegexp

func ToRegexp(pattern string) (*regexp.Regexp, []string, error)

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.

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