gradix

package module
v0.0.0-...-a577230 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 2 Imported by: 0

README

Gradix

A simple radix like tree specialized for URL path routing with supports for wildcards.

Usage

GOPROXY=direct go get github.com/globoplox/gradix

package main

import (
  "github.com/globoplox/gradix"
  "fmt"
)

func main() {
  radix := gradix.New[string]()
  radix.Add("/users", "List users")
  radix.Add("/users/:id", "Get a user by id")
  radix.Add("/users/self", "Get the current user")
  radix.Add("/users/:user_id/pets/", "...")
  radix.Add("/users/:user_id/pets/:pet_id", "...")
  radix.Add("/users/:user_id/friends/:friend_id", "...")
  radix.Add("/users/:user_id/friends/", "...")
  fmt.Printf("%v\n", radix.Search("/users/toto")) // => [{Get a user by id map[id:toto]}]
  fmt.Printf("%v\n", radix.Search("/users/toto/")) // => [{Get a user by id map[id:toto]}]
  fmt.Printf("%v\n", radix.Search("/users/self")) // => [{Get the current user map[]} {Get a user by id map[id:self]}]
  fmt.Printf("%v\n", radix.Search("/test")) // => []
  fmt.Printf("%v\n", radix.Search("nowhere")) // => []
  fmt.Printf("%v\n", radix.Search("")) // => []
  fmt.Printf("%v\n", radix.Search("/////")) // => []
}

Documentation

Overview

A simple radix like tree specialized for URL path routing with supports for wildcards.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Radix

type Radix[P any] struct {
	// contains filtered or unexported fields
}

func New

func New[P any]() *Radix[P]

Create an empty tree

func (*Radix[P]) Add

func (self *Radix[P]) Add(url string, payload P)

Add an uri path template and the corresponding payload. Path component must be separated by '/'. Wildcards path component use a ':' prefix. Examples:

radix := New[string]()
radix.Add("/users", "List users")
radix.Add("/users/:id", "Get a user by id")
radix.Add("/users/self", "Get the current user")
radix.Add("/users/:user_id/pets/", "...")
radix.Add("/users/:user_id/pets/:pet_id", "...")
radix.Add("/users/:user_id/friends/:friend_id", "...")
radix.Add("/users/:user_id/friends/", "...")

func (*Radix[P]) Search

func (self *Radix[P]) Search(url string) []Result[P]

Search the radix tree with a given path. It returns all the match found. If there are no match, return an empty slice. When a path component match multiple fixeds or wildcards registered paths, all paths are returned bu the fixed paths appear first, then the wildcard path. Example:

radix.Search("/users/toto") // => [{"Get a user by id", [id:toto]}}]
radix.Search("/users/self") // => [{"Get the current user", []}, {"Get a user by id", [id:self]}}]
radix.Search("/test") // => empty
radix.Search("nowhere") // => empty
radix.Search("") // => empty

type Result

type Result[P any] struct {
	// The payload of the added path that matched
	Payload P
	// The values of the wildcard components of the match
	Parameters map[string]string
}

Match result

Jump to

Keyboard shortcuts

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