Skip to content

Commit

Permalink
lib/path: add method Keys and NKey, and export IsKeyExists
Browse files Browse the repository at this point in the history
The Keys method return list of key in path.
The NKey method return the number of key in path.
The IsKeyExists return true if the key exist in Route; otherwise it
will return false.
  • Loading branch information
shuLhan committed Jan 25, 2024
1 parent 9021637 commit f9b7b27
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/path/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewRoute(rpath string) (rute *Route, err error) {
return nil, ErrPathKeyEmpty
}

if rute.isKeyExist(node.name) {
if rute.IsKeyExists(node.name) {
return nil, ErrPathKeyDuplicate
}

Expand All @@ -79,9 +79,12 @@ func NewRoute(rpath string) (rute *Route, err error) {
return rute, nil
}

// isKeyExist will return true if the key already exist in nodes; otherwise
// it will return false.
func (rute *Route) isKeyExist(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.
func (rute *Route) IsKeyExists(key string) bool {
key = strings.ToLower(key)
var node *routeNode
for _, node = range rute.nodes {
if !node.isKey {
Expand All @@ -94,6 +97,22 @@ func (rute *Route) isKeyExist(key string) bool {
return false
}

// Keys return list of key in path.
func (rute *Route) Keys() (keys []string) {
var node *routeNode
for _, node = range rute.nodes {
if node.isKey {
keys = append(keys, node.name)
}
}
return keys
}

// NKey return the number of key in path.
func (rute *Route) NKey() (n int) {
return rute.nkey
}

// 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.
func (rute *Route) Parse(rpath string) (vals map[string]string, ok bool) {
Expand Down
52 changes: 52 additions & 0 deletions lib/path/route_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,58 @@ import (
libpath "github.com/shuLhan/share/lib/path"
)

func ExampleRoute_IsKeyExists() {
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 ExampleRoute_Keys() {
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 ExampleRoute_NKey() {
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 ExampleRoute_Parse() {
var (
rute *libpath.Route
Expand Down

0 comments on commit f9b7b27

Please sign in to comment.