From d5af62cc40324e6bf98d0de42f000d3748993da4 Mon Sep 17 00:00:00 2001 From: Marcel Edmund Franke Date: Sat, 7 Jan 2017 11:54:19 +0100 Subject: [PATCH] Added extract vars for regex routes feature --- route.go | 9 +++++++-- router_test.go | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/route.go b/route.go index f4b5a71..e58886c 100644 --- a/route.go +++ b/route.go @@ -181,10 +181,11 @@ func (r *Route) Path(path string) RouteInterface { switch { case containsRegex(path): matcher = newPathRegexMatcher(path) + r.extractVarsIndexies("#", path, "var") r.kind = kindRegexPath case containsVars(path): matcher = newPathWithVarsMatcher(path) - r.extractVarsIndexies(":", path) + r.extractVarsIndexies(":", path, "") r.kind = kindVarsPath default: matcher = pathMatcher(path) @@ -202,7 +203,7 @@ func (r *Route) GetPath() string { return r.path } -func (r *Route) extractVarsIndexies(prefix string, path string) { +func (r *Route) extractVarsIndexies(prefix string, path string, name string) { urlSeg := strings.Split(path, "/") @@ -211,6 +212,10 @@ func (r *Route) extractVarsIndexies(prefix string, path string) { for k, v := range urlSeg { if strings.HasPrefix(v, prefix) { + if name != "" { + v = name + } + if _, found := indexies[v]; !found { indexies[v] = k continue diff --git a/router_test.go b/router_test.go index 61b0189..a5b5c2f 100644 --- a/router_test.go +++ b/router_test.go @@ -162,6 +162,18 @@ func TestPath(t *testing.T) { r.HandleFunc(method, "/api/user/:number", handler) }, }, + { + title: "(GET) Path route with vars", + path: "/article/B00KY1U7GM", + method: http.MethodGet, + statusCode: http.StatusOK, + kind: "HandlerFunc", + vars: map[string]string{"var": "B00KY1U7GM"}, + route: func(r *Router, path string, method string, handler func(w http.ResponseWriter, r *http.Request)) { + r.CaseSensitiveURL = true + r.HandleFunc(method, "/article/#([a-zA-Z0-9]{10,})", handler) + }, + }, { title: "(GET) Path route with vars", path: "/api/user/32/article/golang",