diff options
author | Franck Cuny <franck@lumberjaph.net> | 2013-04-21 16:37:36 -0700 |
---|---|---|
committer | Franck Cuny <franck@lumberjaph.net> | 2013-04-21 16:37:36 -0700 |
commit | 11d7b0a2a9b6cd2c26b9e3460d65685583a74dfa (patch) | |
tree | bc58756649feadef4d3d13a12db82bbbf34c346d | |
parent | Apply gofmt (diff) | |
download | path-router-11d7b0a2a9b6cd2c26b9e3460d65685583a74dfa.tar.gz |
Normalize the name of captured components.
In the case of an optional components, the name is ":something". We need to normalize that name in order to be able to call directly "something" when needed.
-rw-r--r-- | route.go | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/route.go b/route.go index 38ff036..e7994f7 100644 --- a/route.go +++ b/route.go @@ -27,8 +27,15 @@ type Match struct { } var componentIsVariable = regexp.MustCompile("^:") -var componentsIsOptional = regexp.MustCompile("^?:") +var componentsIsOptional = regexp.MustCompile("^\\?:") var namedComponentsRegex = regexp.MustCompile("^:(.*)$") +var convertComponent = regexp.MustCompile("^\\??:(.*)$") + + +func (self *Route) convertComponentName(name string) string { + newName := convertComponent.FindStringSubmatch(name) + return newName[1] +} func (self *Route) Match(request *http.Request) *Match { methodMatch := false @@ -55,7 +62,7 @@ func (self *Route) Match(request *http.Request) *Match { p := components[i] if componentIsVariable.MatchString(c) == true { - mapping[c] = p + mapping[self.convertComponentName(c)] = p } else { if p != c { return nil |