routevars

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: GPL-2.0 Imports: 4 Imported by: 6

README

RouteVars

Easily get variables from any url.

Example

	var path = "/users/1234/john"
	var ok, vars = routevars.Match("/users/<<id:int>>/<<name:string>>", path)
	if ok {
		fmt.Println("Match!")
		for k, v := range vars {
			fmt.Printf("%s: %s\n", k, v)
		}
	} else {
		fmt.Println("No match")
	}

Installation

go get github.com/Nigel2392/routevars

Documentation

Index

Constants

View Source
const (
	RT_PATH_VAR_PREFIX = "<<"
	RT_PATH_VAR_SUFFIX = ">>"
	RT_PATH_VAR_DELIM  = ":"
)

Router regex delimiters

View Source
const (
	NameInt      = "int"
	NameString   = "string"
	NameSlug     = "slug"
	NameUUID     = "uuid"
	NameAny      = "any"
	NameHex      = "hex"
	NameAlphaNum = "alphanum"
)

Router regex types.

View Source
const (
	// Match any character
	RT_PATH_REGEX_ANY = ".+"
	// Match any number
	RT_PATH_REGEX_NUM = "[0-9]+"
	// Match any string
	RT_PATH_REGEX_STR = "[a-zA-Z]+"
	// Match any hex number
	RT_PATH_REGEX_HEX = "[0-9a-fA-F]+"
	// Match any UUID
	RT_PATH_REGEX_UUID = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
	// Match any alphanumeric string
	RT_PATH_REGEX_ALPHANUMERIC = "[0-9a-zA-Z_-]+"
	// Match any slug
	RT_PATH_REGEX_SLUG = "[0-9a-zA-Z_-]+"
)

Router regex patterns

Variables

This section is empty.

Functions

func FormatURL added in v1.0.7

func FormatURL(path string, v ...any) string

func FormatURLSafe added in v1.0.8

func FormatURLSafe(path string, v ...any) (string, error)

func Match

func Match(path, pathToMatch string) (bool, map[string]string)

Types

type URLFormatter added in v1.0.6

type URLFormatter string

func (URLFormatter) Format added in v1.0.6

func (uf URLFormatter) Format(v ...any) string

Format the URL with the given arguments.

func (URLFormatter) FormatSafe added in v1.0.8

func (uf URLFormatter) FormatSafe(v ...any) (string, error)

Format the URL with the given arguments.

func (URLFormatter) Match added in v1.0.7

func (uf URLFormatter) Match(other string) (bool, map[string]string)

Match the URL with another URL.

Jump to

Keyboard shortcuts

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