From 0aa3c495f9a3e28afa8fcdcca11da6d127446894 Mon Sep 17 00:00:00 2001 From: Sean Prince Date: Fri, 8 Nov 2019 17:04:35 +0000 Subject: [PATCH] Colon must be at beginning of path segment to denote a path parameter Signed-off-by: Sean Prince <7532924+seanprince@users.noreply.github.com> --- middleware/denco/router.go | 8 ++++++-- middleware/denco/router_test.go | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/middleware/denco/router.go b/middleware/denco/router.go index 73703fdd..edb4c57a 100755 --- a/middleware/denco/router.go +++ b/middleware/denco/router.go @@ -17,6 +17,9 @@ const ( // TerminationCharacter is a special character for end of path. TerminationCharacter = '#' + // SeparatorCharacter separates path segments. + SeparatorCharacter = '/' + // MaxSize is max size of records and internal slice. MaxSize = (1 << 22) - 1 ) @@ -420,10 +423,11 @@ type record struct { // makeRecords returns the records that use to build Double-Arrays. func makeRecords(srcs []Record) (statics, params []*record) { - spChars := string([]byte{ParamCharacter, WildcardCharacter}) termChar := string(TerminationCharacter) + paramPrefix := string(SeparatorCharacter) + string(ParamCharacter) + wildcardPrefix := string(SeparatorCharacter) + string(WildcardCharacter) for _, r := range srcs { - if strings.ContainsAny(r.Key, spChars) { + if strings.Contains(r.Key, paramPrefix) || strings.Contains(r.Key, wildcardPrefix) { r.Key += termChar params = append(params, &record{Record: r}) } else { diff --git a/middleware/denco/router_test.go b/middleware/denco/router_test.go index c0bd1cb4..7e4b7849 100755 --- a/middleware/denco/router_test.go +++ b/middleware/denco/router_test.go @@ -448,6 +448,7 @@ func TestRouter_Build_withoutSizeHint(t *testing.T) { {[]string{"/user"}, 0}, {[]string{"/user/:id"}, 1}, {[]string{"/user/:id/post"}, 1}, + {[]string{"/user/:id/post:validate"}, 2}, {[]string{"/user/:id/:group"}, 2}, {[]string{"/user/:id/post/:cid"}, 2}, {[]string{"/user/:id/post/:cid", "/admin/:id/post/:cid"}, 2}, @@ -487,6 +488,7 @@ func TestRouter_Build_withSizeHint(t *testing.T) { {"/user/:id", 3, 3}, {"/user/:id/:group", 0, 0}, {"/user/:id/:group", 1, 1}, + {"/user/:id/:group:validate", 1, 1}, } { r := denco.New() r.SizeHint = v.sizeHint