matcher

package
v0.85.0-pre.2 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

README

kit/matcher

github.com/vormadev/vorma/kit/matcher

Path-pattern matcher for router-like workloads.

It supports:

  • static segments (/users)
  • dynamic params (/users/:id)
  • splats (/files/*)
  • single best-match lookup

Import

import "github.com/vormadev/vorma/kit/matcher"

Core Types

  • Matcher: registers patterns and resolves paths.
  • RegisteredPattern: normalized form of a registered pattern.
  • BestMatch: result of FindBestMatch.

For nested/layout match stacks, use github.com/vormadev/vorma/kit/nestedmatcher.

Quick Start

m := matcher.New(nil)
m.RegisterPattern("/")
m.RegisterPattern("/users")
m.RegisterPattern("/users/:id")
m.RegisterPattern("/files/*")

best, ok := m.FindBestMatch("/users/42")
if ok {
	_ = best.NormalizedPattern() // "/users/:id"
	_ = best.Params["id"]        // "42"
}

Options

Defaults:

  • DynamicParamPrefix: ':'
  • SplatSegmentIdentifier: '*'
  • Quiet: false (duplicate-registration warnings enabled)

Match APIs

FindBestMatch

Returns exactly one route using internal precedence/score rules.

  • exact static matches are preferred when available
  • dynamic segments capture params by segment name
  • splats capture the remaining segments
  • returns (*BestMatch, false) when nothing matches

Pattern Normalization Helpers

  • HasLeadingSlash, HasTrailingSlash
  • EnsureLeadingSlash, EnsureTrailingSlash, EnsureLeadingAndTrailingSlash
  • StripLeadingSlash, StripTrailingSlash
  • ParseSegments
  • JoinPatterns

Important Behavior Notes

  • Register all patterns before serving traffic. Matcher is not designed as a concurrent registration/mutation structure.
  • Duplicate registrations overwrite map entries and may log warnings when Quiet is false.
  • Dynamic params match non-empty segments.
  • RegisteredPattern.NormalizedSegments() returns a copy of normalized segment metadata, so mutating that returned slice does not mutate matcher internals.

API Reference

Types
  • type BestMatch
  • type Matcher
  • type Options
  • type Params
  • type RegisteredPattern
  • type Segment
  • type SegmentType
Constructors and functions
  • func New(opts *Options) *Matcher
  • func ParseSegments(path string) []string
  • func JoinPatterns(rp *RegisteredPattern, pattern string) string
  • func HasLeadingSlash(pattern string) bool
  • func HasTrailingSlash(pattern string) bool
  • func EnsureLeadingSlash(pattern string) string
  • func EnsureTrailingSlash(pattern string) string
  • func EnsureLeadingAndTrailingSlash(pattern string) string
  • func StripLeadingSlash(pattern string) string
  • func StripTrailingSlash(pattern string) string
Matcher methods
  • func (m *Matcher) RegisterPattern(originalPattern string) *RegisteredPattern
  • func (m *Matcher) NormalizePattern(originalPattern string) *RegisteredPattern
  • func (m *Matcher) FindBestMatch(realPath string) (*BestMatch, bool)
  • func (m *Matcher) DynamicParamPrefix() rune
  • func (m *Matcher) SplatSegmentIdentifier() rune
RegisteredPattern methods
  • func (rp *RegisteredPattern) OriginalPattern() string
  • func (rp *RegisteredPattern) NormalizedPattern() string
  • func (rp *RegisteredPattern) NormalizedSegments() []Segment

Documentation

Overview

Package matcher provides route pattern matching with best-match semantics.

It is intended for routers that need exactly one resolved route per request path, including support for dynamic params and splats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureLeadingAndTrailingSlash

func EnsureLeadingAndTrailingSlash(pattern string) string

EnsureLeadingAndTrailingSlash ensures both leading and trailing slashes.

func EnsureLeadingSlash

func EnsureLeadingSlash(pattern string) string

EnsureLeadingSlash ensures a leading slash exists.

func EnsureTrailingSlash

func EnsureTrailingSlash(pattern string) string

EnsureTrailingSlash ensures a trailing slash exists.

func HasLeadingSlash

func HasLeadingSlash(pattern string) bool

HasLeadingSlash reports whether pattern starts with '/'.

func HasTrailingSlash

func HasTrailingSlash(pattern string) bool

HasTrailingSlash reports whether pattern ends with '/'.

func JoinPatterns

func JoinPatterns(rp *RegisteredPattern, pattern string) string

JoinPatterns joins a registered pattern with a pattern suffix.

func ParseSegments

func ParseSegments(path string) []string

ParseSegments splits a path into matcher segments.

func StripLeadingSlash

func StripLeadingSlash(pattern string) string

StripLeadingSlash removes a leading slash when present.

func StripTrailingSlash

func StripTrailingSlash(pattern string) string

StripTrailingSlash removes a trailing slash when present.

Types

type BestMatch

type BestMatch = matchercore.BestMatch

BestMatch is the highest-scoring single match for a path.

type Matcher

type Matcher struct {
	// contains filtered or unexported fields
}

Matcher resolves route patterns for one option configuration.

func New

func New(opts *Options) *Matcher

New constructs a matcher with defaulted options.

func (*Matcher) DynamicParamPrefix

func (m *Matcher) DynamicParamPrefix() rune

DynamicParamPrefix returns the dynamic parameter prefix rune.

func (*Matcher) FindBestMatch

func (m *Matcher) FindBestMatch(realPath string) (*BestMatch, bool)

FindBestMatch resolves the highest-scoring single match for realPath.

func (*Matcher) NormalizePattern

func (m *Matcher) NormalizePattern(originalPattern string) *RegisteredPattern

NormalizePattern validates and normalizes one input pattern.

func (*Matcher) RegisterPattern

func (m *Matcher) RegisterPattern(originalPattern string) *RegisteredPattern

RegisterPattern registers one pattern into matcher indices.

func (*Matcher) SplatSegmentIdentifier

func (m *Matcher) SplatSegmentIdentifier() rune

SplatSegmentIdentifier returns the splat segment identifier rune.

type Options

type Options struct {
	// Optional. Defaults to ':'.
	DynamicParamPrefix rune
	// Optional. Defaults to '*'.
	SplatSegmentIdentifier rune
	// Optional. Defaults to false.
	Quiet bool
}

Options configures matcher behavior.

type Params

type Params = matchercore.Params

Params stores dynamic path parameter values extracted from a match.

type RegisteredPattern

type RegisteredPattern = matchercore.RegisteredPattern

RegisteredPattern is normalized metadata for one registered pattern.

type Segment

type Segment = matchercore.Segment

Segment is a normalized pattern segment and its classification.

type SegmentType

type SegmentType = matchercore.SegmentType

SegmentType identifies the kind of one route segment.

Jump to

Keyboard shortcuts

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