radix

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitPath

func SplitPath(path string) []string

SplitPath splits a URL path into segments by '/' and ignores empty segments. Catch-all segments (*filepath) stop further splitting.

Types

type Param

type Param struct {
	Key   string
	Value string
}

Param represents a URL parameter captured during tree search (e.g., :id → {Key:"id", Value:"123"}).

type Params

type Params []Param

Params is a slice of URL parameters.

func (Params) Get

func (ps Params) Get(name string) string

Get returns the value of the parameter with the given key.

type Tree

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

Tree is a generic radix tree (trie) for storing values keyed by URL paths. T is the type of value stored at each route endpoint.

func New

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

New creates a new empty Tree.

func (*Tree[T]) Collect

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

Collect returns all stored values by DFS traversal of the tree.

func (*Tree[T]) Insert

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

Insert sets the value at the given path, overwriting any existing value.

func (*Tree[T]) Search

func (t *Tree[T]) Search(path string, params *Params) (*T, bool)

Search finds a value at the given path, populating params for :param and *catch-all segments. Returns a pointer to the value and true if found, nil and false otherwise.

func (*Tree[T]) Upsert

func (t *Tree[T]) Upsert(path string, value T, merge func(existing *T, incoming T))

Upsert inserts a new value or updates an existing one using the merge function. If the path already has a value, merge(existing, incoming) is called instead of overwriting.

Jump to

Keyboard shortcuts

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