summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-01-25 18:13:29 +0700
committerShulhan <ms@kilabit.info>2024-01-25 18:13:29 +0700
commitf9b7b2734bb52da14bf908a89a1c75b71d51471d (patch)
tree37a8e1518822169e2f3ca5f5023a521b42bb4e10
parent90216377b90e74698a04ff8218482d91871d212e (diff)
downloadpakakeh.go-f9b7b2734bb52da14bf908a89a1c75b71d51471d.tar.xz
lib/path: add method Keys and NKey, and export IsKeyExists
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.
-rw-r--r--lib/path/route.go27
-rw-r--r--lib/path/route_example_test.go52
2 files changed, 75 insertions, 4 deletions
diff --git a/lib/path/route.go b/lib/path/route.go
index 1cf9d419..f60ad310 100644
--- a/lib/path/route.go
+++ b/lib/path/route.go
@@ -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
}
@@ -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 {
@@ -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) {
diff --git a/lib/path/route_example_test.go b/lib/path/route_example_test.go
index 6d7c6380..37b1168e 100644
--- a/lib/path/route_example_test.go
+++ b/lib/path/route_example_test.go
@@ -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