Skip to content

Commit fe6d8cc

Browse files
authored
Merge pull request #164 from seanprince/colon-in-path-segment
Colon must be at beginning of path segment to denote a path parameter
2 parents 553c9d1 + 0aa3c49 commit fe6d8cc

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

middleware/denco/router.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const (
1717
// TerminationCharacter is a special character for end of path.
1818
TerminationCharacter = '#'
1919

20+
// SeparatorCharacter separates path segments.
21+
SeparatorCharacter = '/'
22+
2023
// MaxSize is max size of records and internal slice.
2124
MaxSize = (1 << 22) - 1
2225
)
@@ -420,10 +423,11 @@ type record struct {
420423

421424
// makeRecords returns the records that use to build Double-Arrays.
422425
func makeRecords(srcs []Record) (statics, params []*record) {
423-
spChars := string([]byte{ParamCharacter, WildcardCharacter})
424426
termChar := string(TerminationCharacter)
427+
paramPrefix := string(SeparatorCharacter) + string(ParamCharacter)
428+
wildcardPrefix := string(SeparatorCharacter) + string(WildcardCharacter)
425429
for _, r := range srcs {
426-
if strings.ContainsAny(r.Key, spChars) {
430+
if strings.Contains(r.Key, paramPrefix) || strings.Contains(r.Key, wildcardPrefix) {
427431
r.Key += termChar
428432
params = append(params, &record{Record: r})
429433
} else {

middleware/denco/router_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ func TestRouter_Build_withoutSizeHint(t *testing.T) {
448448
{[]string{"/user"}, 0},
449449
{[]string{"/user/:id"}, 1},
450450
{[]string{"/user/:id/post"}, 1},
451+
{[]string{"/user/:id/post:validate"}, 2},
451452
{[]string{"/user/:id/:group"}, 2},
452453
{[]string{"/user/:id/post/:cid"}, 2},
453454
{[]string{"/user/:id/post/:cid", "/admin/:id/post/:cid"}, 2},
@@ -487,6 +488,7 @@ func TestRouter_Build_withSizeHint(t *testing.T) {
487488
{"/user/:id", 3, 3},
488489
{"/user/:id/:group", 0, 0},
489490
{"/user/:id/:group", 1, 1},
491+
{"/user/:id/:group:validate", 1, 1},
490492
} {
491493
r := denco.New()
492494
r.SizeHint = v.sizeHint

0 commit comments

Comments
 (0)