diff options
author | Franck Cuny <franck@lumberjaph.net> | 2013-04-21 14:18:38 -0700 |
---|---|---|
committer | Franck Cuny <franck@lumberjaph.net> | 2013-04-21 14:18:38 -0700 |
commit | a0f9885b944e51a4aa33ff9e5860185f4a0abea0 (patch) | |
tree | 942ce362f2cab175f857c52f73da65f48b4db8e6 /route.go | |
parent | Add support for optional parameter in a path. (diff) | |
download | path-router-a0f9885b944e51a4aa33ff9e5860185f4a0abea0.tar.gz |
Disambiguate routes when more than one match.
For some path, more than one route can match. We try hard to disambiguate them, and if we can't, we return an error with the list of routes matching the request. In order to do that, the Match function needs to be able to also return an error in addition to a route.
Diffstat (limited to 'route.go')
-rw-r--r-- | route.go | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/route.go b/route.go index d84d026..b26ad5a 100644 --- a/route.go +++ b/route.go @@ -14,12 +14,12 @@ type Route struct { RequiredNamedComponents map[string]bool OptionalNamedComponents map[string]bool Length int - LengthWithoutOptional int + LengthWithoutOptional int } type Match struct { Path string - Route *Route + Route Route Mapping map[string]string Method string } @@ -29,7 +29,6 @@ var componentsIsOptional = regexp.MustCompile("^?:") var namedComponentsRegex = regexp.MustCompile("^:(.*)$") func (self *Route) Match(request Request) *Match { - methodMatch := false for m, _ := range self.Executors { if m == request.Method { @@ -42,6 +41,7 @@ func (self *Route) Match(request Request) *Match { components := strings.Split(request.Request.URL.Path, "/") + //fmt.Println(fmt.Sprintf("components are %s and %s, the lenght is %d, the length without opt is %d and total is %d", components, self.Components, len(components), self.LengthWithoutOptional, self.Length)) if len(components) < self.LengthWithoutOptional || len(components) > self.Length { return nil } @@ -63,14 +63,12 @@ func (self *Route) Match(request Request) *Match { } } - match := &Match{ + return &Match{ Path: request.Request.URL.Path, - Route: self, - Mapping: mapping, + Route: *self, Method: request.Method, + Mapping: mapping, } - - return match } func (self *Route) Execute(request *Request) (Response, error) { |