router

package
v1.13.9 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package router matches request paths against an ordered table of starlette-style path templates. The first route declared that matches wins: declaration order is the entire priority rule.

Index

Constants

View Source
const MaxRoutes = 1 << 16

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder[T any] struct {
	// contains filtered or unexported fields
}

Builder accumulates a route table. A route's index is its position in the order added.

func NewBuilder

func NewBuilder[T any]() *Builder[T]

func (*Builder[T]) Add

func (b *Builder[T]) Add(t *Template, m Mask, v T) error

Add appends a route. Routes must be added in declaration order: the first one added that matches a path wins.

func (*Builder[T]) Build

func (b *Builder[T]) Build() *Router[T]

Build freezes the table, compacting its nodes into one arena.

type Mask

type Mask uint32

Mask is an opaque per-route tag. A route fully matches when its mask intersects the query mask, and partially matches when only its template does.

type Result

type Result uint8
const (
	ResultNone Result = iota
	// ResultPartial means a template matched but no route carrying the queried
	// mask did.
	ResultPartial
	ResultFull
	// ResultOverBudget means the search exceeded maxSteps and was abandoned, so
	// no route was decided.
	ResultOverBudget
)

func (Result) String

func (r Result) String() string

type Router

type Router[T any] struct {
	// contains filtered or unexported fields
}

Router matches paths against a route table. It is built once and never mutated, so readers need no lock and a new table is published by a single atomic.Pointer store, which synchronises everything a reader reaches.

func (*Router[T]) Ambiguous

func (r *Router[T]) Ambiguous() []string

Ambiguous returns the templates whose shape forces the matcher to backtrack - a param that a following literal can extend, adjacent params, or a non-final path convertor. The result is read-only.

func (*Router[T]) MarshalLogObject

func (r *Router[T]) MarshalLogObject(e zapcore.ObjectEncoder) error

MarshalLogObject describes the table's shape. A Router holds no per-match state.

func (*Router[T]) Match

func (r *Router[T]) Match(path string, q Mask) (T, Result)

Match returns the value of the first route in declaration order whose template matches path and whose mask intersects q. It returns ResultPartial when a template matched but no such route carried the mask, and ResultOverBudget when the search exceeded its step budget.

Match allocates nothing.

type Template

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

Template is a parsed starlette-style path template. Parsing mirrors starlette's compile_path exactly.

func ParseTemplate

func ParseTemplate(path string) (*Template, error)

ParseTemplate parses a starlette path template. Custom convertors are rejected: only the five built-ins may travel over the wire.

func (*Template) Ambiguous

func (t *Template) Ambiguous() bool

Ambiguous reports whether the template's own shape can force the matcher to backtrack - a param a following literal can extend, adjacent params, or a non-final path convertor - independent of what other templates put in the tree.

func (*Template) Canonical

func (t *Template) Canonical() string

Canonical returns a key identifying what the template matches: two templates with the same canonical form accept exactly the same paths and differ only in param names. Literals are length-prefixed so that a literal brace cannot forge a convertor - "/x/{:str}" is literal text, and must not collide with "/x/{a}".

func (*Template) String

func (t *Template) String() string

String returns the template as declared.

Jump to

Keyboard shortcuts

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