Skip to content

Commit

Permalink
Added extract vars for regex routes feature
Browse files Browse the repository at this point in the history
  • Loading branch information
donutloop committed Jan 7, 2017
1 parent b4aed58 commit d5af62c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, "/")

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d5af62c

Please sign in to comment.