Documentation ¶
Overview ¶
Package path implements utility routines for manipulating slash-separated paths.
Index ¶
- Variables
- type Route
- func (rute *Route) IsKeyExists(key string) bool
- func (rute *Route) Keys() (keys []string)
- func (rute *Route) NKey() (n int)
- func (rute *Route) Parse(rpath string) (vals map[string]string, ok bool)
- func (rute *Route) Path() string
- func (rute *Route) Set(key, val string) bool
- func (rute *Route) String() (path string)
Examples ¶
Constants ¶
This section is empty.
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".
Functions ¶
This section is empty.
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 ¶
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 ¶
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 ¶
package main import ( "fmt" "log" libpath "git.sr.ht/~shulhan/pakakeh.go/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 ¶
Keys return list of key in path.
Example ¶
package main import ( "fmt" "log" libpath "git.sr.ht/~shulhan/pakakeh.go/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 ¶
NKey return the number of key in path.
Example ¶
package main import ( "fmt" "log" libpath "git.sr.ht/~shulhan/pakakeh.go/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 ¶
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 ¶
package main import ( "fmt" "log" libpath "git.sr.ht/~shulhan/pakakeh.go/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) Path ¶ added in v0.55.1
Path return the path with all the keys has been substituted with values, even if its empty. See Route.String for returning path with key name (as in ":name").
Example ¶
package main import ( "fmt" "log" libpath "git.sr.ht/~shulhan/pakakeh.go/lib/path" ) func main() { var ( rute *libpath.Route err error ) rute, err = libpath.NewRoute(`/:user/:repo`) if err != nil { log.Fatal(err) } fmt.Println(rute.Path()) rute.Set(`user`, `shuLhan`) fmt.Println(rute.Path()) rute.Set(`repo`, `pakakeh.go`) fmt.Println(rute.Path()) }
Output: // /shuLhan/ /shuLhan/pakakeh.go
func (*Route) Set ¶
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 ¶
package main import ( "fmt" "log" libpath "git.sr.ht/~shulhan/pakakeh.go/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 ¶
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>".
To return the path with all keys has been substituted, even empty, use Route.Path.