From 6ce15d1c36d9cc4f5ee5c2443964140f72ffcdb1 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sat, 27 Apr 2013 16:11:16 -0700 Subject: Placeholder in URL use the {\w+} form, not :\w+ This is the format supported for URI templates (RFC 6570). We're not using it now but this is something that I consider for a near future. --- eg/base.go | 4 ++-- request_test.go | 2 +- route.go | 8 ++++---- route_test.go | 28 ++++++++++++++-------------- router_benchmark_test.go | 4 ++-- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/eg/base.go b/eg/base.go index 4f4aa35..6485071 100644 --- a/eg/base.go +++ b/eg/base.go @@ -55,14 +55,14 @@ func main() { router.AddRoute( &mooh.Route{ Method: "GET", - Path: "/test/:id", + Path: "/test/{id}", Code: TestId, }, ) router.AddRoute( &mooh.Route{ Method: "GET", - Path: "/test/:id/foo", + Path: "/test/{id}/foo", Code: TestIdFoo, }, ) diff --git a/request_test.go b/request_test.go index 9062e78..d59d9b8 100644 --- a/request_test.go +++ b/request_test.go @@ -16,7 +16,7 @@ func buildRouter() Router { router := BuildRouter() router.AddRoute(&Route{ Method: "GET", - Path: "/foo/:bar", + Path: "/foo/{bar}", Code: testRequestRoute, Validations: map[string]*regexp.Regexp{ "bar": regexp.MustCompile("\\w+"), diff --git a/route.go b/route.go index a33a78f..925bfb5 100644 --- a/route.go +++ b/route.go @@ -28,10 +28,10 @@ type Match struct { Method string } -var componentIsVariable = regexp.MustCompile("^:") -var componentsIsOptional = regexp.MustCompile("^\\?:") -var namedComponentsRegex = regexp.MustCompile("^:(.*)$") -var convertComponent = regexp.MustCompile("^\\??:(.*)$") +var componentIsVariable = regexp.MustCompile("^{[^}]+}$") +var componentsIsOptional = regexp.MustCompile("^\\?{.*}$") +var namedComponentsRegex = regexp.MustCompile("^{(.*)}$") +var convertComponent = regexp.MustCompile("^\\??{(.*)}$") // XXX explain this function func (self *Route) convertComponentName(name string) string { diff --git a/route_test.go b/route_test.go index 61585f4..e4b33c0 100644 --- a/route_test.go +++ b/route_test.go @@ -18,7 +18,7 @@ func TestBasic(t *testing.T) { router.AddRoute(&Route{Method: "GET", Path: "/", Code: testRoute}) router.AddRoute(&Route{ Method: "GET", - Path: "/blog/:year/:month/:day", + Path: "/blog/{year}/{month}/{day}", Code: testRoute, Validations: map[string]*regexp.Regexp{ "year": regexp.MustCompile("[\\d]{4}"), @@ -31,7 +31,7 @@ func TestBasic(t *testing.T) { if router.routes[0].Path != "/" { t.Fatal() } - if router.routes[1].Path != "/blog/:year/:month/:day" { + if router.routes[1].Path != "/blog/{year}/{month}/{day}" { t.Fatal() } if router.routes[2].Path != "/blog" { @@ -42,7 +42,7 @@ func TestBasic(t *testing.T) { func TestWithSimleValidation(t *testing.T) { router := BuildRouter() router.AddRoute(&Route{ - Path: "/users/:id", + Path: "/users/{id}", Method: "GET", Code: testRoute, Validations: map[string]*regexp.Regexp{ @@ -50,7 +50,7 @@ func TestWithSimleValidation(t *testing.T) { }, }) router.AddRoute(&Route{ - Path: "/users/:name", + Path: "/users/{name}", Method: "GET", Code: testRoute, Validations: map[string]*regexp.Regexp{ @@ -63,7 +63,7 @@ func TestWithSimleValidation(t *testing.T) { if m == nil { t.Fatal() } - if m.Route.Path != "/users/:id" { + if m.Route.Path != "/users/{id}" { t.Fatal() } @@ -72,7 +72,7 @@ func TestWithSimleValidation(t *testing.T) { if m == nil { t.Fatal() } - if m.Route.Path != "/users/:name" { + if m.Route.Path != "/users/{name}" { t.Fatal() } } @@ -81,7 +81,7 @@ func TestMatch(t *testing.T) { router := BuildRouter() router.AddRoute(&Route{ Method: "GET", - Path: "/blog/:year/:month/:day", + Path: "/blog/{year}/{month}/{day}", Code: testRoute, Validations: map[string]*regexp.Regexp{ "year": regexp.MustCompile("[\\d]{4}"), @@ -111,7 +111,7 @@ func TestMatch(t *testing.T) { func TestMatchOptional(t *testing.T) { router := BuildRouter() router.AddRoute(&Route{ - Path: "/blog/?:year", + Path: "/blog/?{year}", Method: "GET", Code: testRoute, Validations: map[string]*regexp.Regexp{ @@ -119,7 +119,7 @@ func TestMatchOptional(t *testing.T) { }, }) router.AddRoute(&Route{ - Path: "/blog/:year/?:month", + Path: "/blog/{year}/?{month}", Method: "GET", Code: testRoute, Validations: map[string]*regexp.Regexp{ @@ -149,7 +149,7 @@ func TestAmbigiousSimple(t *testing.T) { router := BuildRouter() router.AddRoute(&Route{Path: "/foo/bar", Method: "GET", Code: testRoute}) router.AddRoute(&Route{ - Path: "/foo/:bar", + Path: "/foo/{bar}", Method: "GET", Code: testRoute, Validations: map[string]*regexp.Regexp{ @@ -169,7 +169,7 @@ func TestAmbigiousSimple(t *testing.T) { func TestAmbigiousFail(t *testing.T) { router := BuildRouter() router.AddRoute(&Route{ - Path: "/foo/:bar", + Path: "/foo/{bar}", Method: "GET", Code: testRoute, Validations: map[string]*regexp.Regexp{ @@ -177,7 +177,7 @@ func TestAmbigiousFail(t *testing.T) { }, }) router.AddRoute(&Route{ - Path: "/:foo/bar", + Path: "/{foo}/bar", Method: "GET", Code: testRoute, Validations: map[string]*regexp.Regexp{ @@ -199,7 +199,7 @@ func TestAmbigiousFail(t *testing.T) { func TestWithDefaults(t *testing.T) { router := BuildRouter() router.AddRoute(&Route{ - Path: "/foo/:bar", + Path: "/foo/{bar}", Method: "GET", Code: testRoute, Defaults: map[string]string{ @@ -220,7 +220,7 @@ func TestWithDefaults(t *testing.T) { func TestWithDefaultNoMatch(t *testing.T) { router := BuildRouter() router.AddRoute(&Route{ - Path: "/foo/:bar", + Path: "/foo/{bar}", Method: "GET", Code: testRoute, Defaults: map[string]string{ diff --git a/router_benchmark_test.go b/router_benchmark_test.go index a5d334a..205fb62 100644 --- a/router_benchmark_test.go +++ b/router_benchmark_test.go @@ -31,9 +31,9 @@ func BenchmarkSimple(b *testing.B) { for i := 0; i < 10; i++ { for j := 0; j < 5; j++ { - routePaths = append(routePaths, fmt.Sprintf("/resource%d/:id/property%d", i, j)) + routePaths = append(routePaths, fmt.Sprintf("/resource%d/{id}/property%d", i, j)) } - routePaths = append(routePaths, fmt.Sprintf("/resource%d/:id", i)) + routePaths = append(routePaths, fmt.Sprintf("/resource%d/{id}", i)) routePaths = append(routePaths, fmt.Sprintf("/resource%d", i)) } routePaths = append(routePaths, "/*") -- cgit 1.4.1