package path

import "github.com/shuLhan/share/lib/path"

Package path implements utility routines for manipulating slash-separated paths.

Index

Examples

Variables

var ErrPathKeyDuplicate = errors.New(`duplicate key in path`)

ErrPathKeyDuplicate define an error when registering path with the same keys, for example "/:x/:x".

var ErrPathKeyEmpty = errors.New(`empty path key`)

ErrPathKeyEmpty define an error when path contains an empty key, for example "/:/y".

Types

type Route

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

Route represent a parsed path. A path can have a key, or binding, that can be replaced with string value. For example, "/org/:user/:repo" have two keys "user" and "repo".

Route handle the path in case-insensitive manner.

func NewRoute

func NewRoute(rpath string) (rute *Route, err error)

NewRoute create new Route from path. It will store the key(s) in path if available.

The key is sub-path that start with colon ":". For example, the following path "/:user/:repo" contains two sub-paths with both are keys. If path is invalid, for example, "/:user/:" or "/:user/:user" (key with duplicate names), it will return nil with an error.

func (*Route) IsKeyExists

func (rute *Route) IsKeyExists(key string) bool

IsKeyExists will return true if the key exist in Route; otherwise it will return false. Remember that the key is stored in lower case, so it will be matched after the parameter key is converted to lower case.

Example

Code:play 

package main

import (
	"fmt"
	"log"

	libpath "github.com/shuLhan/share/lib/path"
)

func main() {
	var (
		rute *libpath.Route
		err  error
	)

	rute, err = libpath.NewRoute(`/book/:title/:page`)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(rute.IsKeyExists(`book`))
	fmt.Println(rute.IsKeyExists(`title`))
	fmt.Println(rute.IsKeyExists(`TITLE`))
}

Output:

false
true
true

func (*Route) Keys

func (rute *Route) Keys() (keys []string)

Keys return list of key in path.

Example

Code:play 

package main

import (
	"fmt"
	"log"

	libpath "github.com/shuLhan/share/lib/path"
)

func main() {
	var (
		rute *libpath.Route
		err  error
	)

	rute, err = libpath.NewRoute(`/book/:title/:page`)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(rute.Keys())
}

Output:

[title page]

func (*Route) NKey

func (rute *Route) NKey() (n int)

NKey return the number of key in path.

Example

Code:play 

package main

import (
	"fmt"
	"log"

	libpath "github.com/shuLhan/share/lib/path"
)

func main() {
	var (
		rute *libpath.Route
		err  error
	)

	rute, err = libpath.NewRoute(`/book/:title/:page`)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(rute.NKey())
}

Output:

2

func (*Route) Parse

func (rute *Route) Parse(rpath string) (vals map[string]string, ok bool)

Parse the path and return the key-value association and true if path is matched with current Route; otherwise it will return nil and false.

Example

Code:play 

package main

import (
	"fmt"
	"log"

	libpath "github.com/shuLhan/share/lib/path"
)

func main() {
	var (
		rute *libpath.Route
		err  error
	)

	rute, err = libpath.NewRoute(`/book/:title/:page`)
	if err != nil {
		log.Fatal(err)
	}

	var (
		vals map[string]string
		ok   bool
	)

	vals, ok = rute.Parse(`/book/Hitchiker to Galaxy/42`)
	fmt.Println(ok, vals)

	vals, ok = rute.Parse(`/book/Hitchiker to Galaxy`)
	fmt.Println(ok, vals)

	vals, ok = rute.Parse(`/book/Hitchiker to Galaxy/42/order`)
	fmt.Println(ok, vals)

}

Output:

true map[page:42 title:hitchiker to galaxy]
false map[]
false map[]

func (*Route) Set

func (rute *Route) Set(key, val string) bool

Set or replace the key's value in path with parameter val. If the key exist it will return true; otherwise it will return false.

Example

Code:play 

package main

import (
	"fmt"
	"log"

	libpath "github.com/shuLhan/share/lib/path"
)

func main() {
	var (
		rute *libpath.Route
		err  error
	)
	rute, err = libpath.NewRoute(`/:user/:repo`)
	if err != nil {
		log.Fatal(err)
	}

	rute.Set(`user`, `shuLhan`)
	fmt.Println(rute)

	rute.Set(`repo`, `share`)
	fmt.Println(rute)
}

Output:

/shuLhan/:repo
/shuLhan/share

func (*Route) String

func (rute *Route) String() (path string)

String generate a clean path without any white spaces and single "/" between sub-path. If the key has been Route.Set, the sub-path will be replaced with its value, otherwise it will returned as ":<key>".

Source Files

path.go route.go route_node.go

Version
v0.53.1 (latest)
Published
Mar 2, 2024
Platform
linux/amd64
Imports
2 packages
Last checked
5 days ago

Tools for package owners.