trie

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Modify from https://github.com/teambition/trie-mux

Index

Constants

View Source
const Version = "1.6.0"

Version is trie-mux version

Variables

This section is empty.

Functions

This section is empty.

Types

type Matched

type Matched struct {
	// Either a Node pointer when matched or nil
	Node *Node

	// Either a map contained matched values or empty map.
	Params map[string]interface{}

	// If FixedPathRedirect enabled, it may returns a redirect path,
	// otherwise a empty string.
	FPR string

	// If TrailingSlashRedirect enabled, it may returns a redirect path,
	// otherwise a empty string.
	TSR string
}

Matched is a result returned by Trie.Match.

type Node

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

Node represents a node on defined patterns that can be matched.

func (*Node) GetAllow

func (n *Node) GetAllow() string

GetAllow returns allow methods defined on the node

trie := New()
trie.Define("/").Handle("GET", handler1)
trie.Define("/").Handle("PUT", handler2)

// trie.Match("/").Node.GetAllow() == "GET, PUT"

func (*Node) GetDescendants

func (n *Node) GetDescendants() []*Node

GetDescendants returns all descendants nodes.

func (*Node) GetHandler

func (n *Node) GetHandler(method string) interface{}

GetHandler ... GetHandler returns handler by method that defined on the node

trie := New()
trie.Define("/api").Handle("GET", func handler1() {})
trie.Define("/api").Handle("PUT", func handler2() {})

trie.Match("/api").Node.GetHandler("GET").(func()) == handler1
trie.Match("/api").Node.GetHandler("PUT").(func()) == handler2

func (*Node) GetMethods

func (n *Node) GetMethods() []string

GetMethods returns methods defined on the node

func (*Node) GetPattern

func (n *Node) GetPattern() string

GetPattern returns pattern defined on the node

func (*Node) Handle

func (n *Node) Handle(method string, handler interface{})

Handle is used to mount a handler with a method name to the node.

t := New()
node := t.Define("/a/b")
node.Handle("GET", handler1)
node.Handle("POST", handler1)

type Options

type Options struct {
	// Ignore case when matching URL path.
	IgnoreCase bool

	// If enabled, the trie will detect if the current path can't be matched but
	// a handler for the fixed path exists.
	// Matched.FPR will returns either a fixed redirect path or an empty string.
	// For example when "/api/foo" defined and matching "/api//foo",
	// The result Matched.FPR is "/api/foo".
	FixedPathRedirect bool

	// If enabled, the trie will detect if the current path can't be matched but
	// a handler for the path with (without) the trailing slash exists.
	// Matched.TSR will returns either a redirect path or an empty string.
	// For example if /foo/ is requested but a route only exists for /foo, the
	// client is redirected to /foo
	// For example when "/api/foo" defined and matching "/api/foo/",
	// The result Matched.TSR is "/api/foo".
	TrailingSlashRedirect bool
}

Options is options for Trie.

type Trie

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

Trie represents a trie that defining patterns and matching URL.

func New

func New(args ...Options) *Trie

New returns a trie

trie := New()
// disable IgnoreCase, TrailingSlashRedirect and FixedPathRedirect
trie := New(Options{})

func (*Trie) Define

func (t *Trie) Define(pattern string) *Node

Define define a pattern on the trie and returns the endpoint node for the pattern.

trie := New()
node1 := trie.Define("/a")
node2 := trie.Define("/a/b")
node3 := trie.Define("/a/b")
// node2.parent == node1
// node2 == node3

The defined pattern can contain three types of parameters:

| Syntax | Description | |--------|------| | `:name` | named parameter | | `:name*` | named with catch-all parameter | | `:name(regexp)` | named with regexp parameter | | `::name` | not named parameter, it is literal `:name` |

func (*Trie) GetEndpoints

func (t *Trie) GetEndpoints() []*Node

GetEndpoints returns all endpoint nodes.

func (*Trie) Match

func (t *Trie) Match(path string) *Matched

Match try to match path. It will returns a Matched instance that includes *Node, Params and Tsr flag when matching success, otherwise a nil.

matched := trie.Match("/a/b")

Jump to

Keyboard shortcuts

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