diff options
author | Franck Cuny <franck@lumberjaph.net> | 2013-04-27 10:20:05 -0700 |
---|---|---|
committer | Franck Cuny <franck@lumberjaph.net> | 2013-04-27 10:20:05 -0700 |
commit | 57b12db9e9352f45b9f365a5e96938aacdf25176 (patch) | |
tree | 7c8fec8cc37b579bd6b7ec30ef144f8dec81f159 /route.go | |
parent | Add exemple for validations in the README. (diff) | |
download | path-router-57b12db9e9352f45b9f365a5e96938aacdf25176.tar.gz |
Support default value for placeholders in route's path.
If a route is created with a Default: &Route{ Path: "/user/:id", Defaults: map[string]string{ "id": "1", }, ... } a request for /user/ will match the route, and set the value for "id" to 1. However, the route /user will not match.
Diffstat (limited to 'route.go')
-rw-r--r-- | route.go | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/route.go b/route.go index 9eab0ab..a33a78f 100644 --- a/route.go +++ b/route.go @@ -12,12 +12,13 @@ type Route struct { Method string Path string Code fn + Validations map[string]*regexp.Regexp + Defaults map[string]string components []string requiredNamedComponents map[string]bool optionalNamedComponents map[string]bool length int lengthWithoutOptional int - Validations map[string]*regexp.Regexp } type Match struct { @@ -55,7 +56,13 @@ func (self *Route) Match(method string, components []string) *Match { return nil } - mapping := map[string]string{} + var mapping map[string]string + + if self.Defaults != nil { + mapping = self.Defaults + }else{ + mapping = map[string]string{} + } currentComponentsLength := len(components) |