package path
import "github.com/shuLhan/share/lib/path"
Package path implements utility routines for manipulating slash-separated paths.
Index ¶
- Variables
- type Route
- func NewRoute(rpath string) (rute *Route, err error)
- 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) Set(key, val string) bool
- func (rute *Route) String() (path string)
Examples ¶
Variables ¶
ErrPathKeyDuplicate define an error when registering path with the same keys, for example "/:x/:x".
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 ¶
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.
Code:play
Output:Example¶
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`))
}
false
true
true
func (*Route) Keys ¶
Keys return list of key in path.
func (*Route) NKey ¶
NKey return the number of key in path.
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.
Code:play
Output:Example¶
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)
}
true map[page:42 title:hitchiker to galaxy]
false map[]
false map[]
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.
Code:play
Output:Example¶
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)
}
/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>".
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.