radix

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package radix provides a radix tree (prefix tree) implementation optimized for URL path matching with support for parameterized segments.

The tree provides O(k) lookup complexity where k is the number of path segments (typically 3-5 for REST APIs), making it ideal for routing and path matching.

Example usage:

tree := radix.New[*MyHandler]()
tree.Insert("/users/{id}", handler1)
tree.Insert("/users/{id}/posts", handler2)

handler, path, found := tree.Lookup("/users/123/posts")
// handler = handler2, path = "/users/{id}/posts", found = true

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PathLookup

type PathLookup interface {
	// Lookup finds the PathItem for a given URL path.
	// Returns the matched PathItem, the path template (e.g., "/users/{id}"), and whether found.
	Lookup(urlPath string) (pathItem *v3.PathItem, matchedPath string, found bool)
}

PathLookup defines the interface for radix tree path matching implementations. The default implementation provides O(k) lookup where k is the path segment count.

Note: This interface handles URL path matching only. HTTP method validation is performed separately after the PathItem is retrieved, since a single path (e.g., "/users/{id}") can support multiple HTTP methods (GET, POST, PUT, DELETE).

type PathTree

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

PathTree is a radix tree optimized for OpenAPI path matching. It provides O(k) lookup where k is the number of path segments (typically 3-5), with minimal allocations during lookup.

This is a thin wrapper around the generic Tree, specialized for OpenAPI PathItem values. It implements the PathLookup interface.

func BuildPathTree

func BuildPathTree(doc *v3.Document) *PathTree

BuildPathTree creates a PathTree from an OpenAPI document. This should be called once during validator initialization.

func NewPathTree

func NewPathTree() *PathTree

NewPathTree creates a new empty radix tree for path matching.

func (*PathTree) Insert

func (t *PathTree) Insert(path string, pathItem *v3.PathItem)

Insert adds a path and its PathItem to the tree. Path should be in OpenAPI format, e.g., "/users/{id}/posts"

func (*PathTree) Lookup

func (t *PathTree) Lookup(urlPath string) (*v3.PathItem, string, bool)

Lookup finds the PathItem for a given request path. Returns the PathItem, the matched path template, and whether a match was found.

func (*PathTree) Size

func (t *PathTree) Size() int

Size returns the number of paths stored in the tree.

func (*PathTree) Walk

func (t *PathTree) Walk(fn func(path string, pathItem *v3.PathItem) bool)

Walk calls the given function for each path in the tree.

type Tree

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

Tree is a radix tree optimized for URL path matching. It supports both literal path segments and parameterized segments like {id}. T is the type of value stored at leaf nodes.

func New

func New[T any]() *Tree[T]

New creates a new empty radix tree.

func (*Tree[T]) Clear

func (t *Tree[T]) Clear()

Clear removes all entries from the tree.

func (*Tree[T]) Insert

func (t *Tree[T]) Insert(path string, value T) bool

Insert adds a path and its associated value to the tree. The path should use {param} syntax for parameterized segments. Examples: "/users", "/users/{id}", "/users/{userId}/posts/{postId}"

Returns true if a new path was inserted, false if an existing path was updated.

func (*Tree[T]) Lookup

func (t *Tree[T]) Lookup(urlPath string) (value T, matchedPath string, found bool)

Lookup finds the value for a given URL path. Returns the value, the matched path template, and whether a match was found.

Literal matches take precedence over parameter matches per OpenAPI specification. For example, "/users/admin" will match "/users/admin" before "/users/{id}".

func (*Tree[T]) Size

func (t *Tree[T]) Size() int

Size returns the number of paths stored in the tree.

func (*Tree[T]) Walk

func (t *Tree[T]) Walk(fn func(path string, value T) bool)

Walk calls the given function for each path in the tree. The function receives the path template and its associated value. If the function returns false, iteration stops.

Jump to

Keyboard shortcuts

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