From a0f9885b944e51a4aa33ff9e5860185f4a0abea0 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 21 Apr 2013 14:18:38 -0700 Subject: 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. --- route.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'route.go') 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) { -- cgit 1.4.1