Skip to content

Commit

Permalink
Merge pull request #164 from seanprince/colon-in-path-segment
Browse files Browse the repository at this point in the history
Colon must be at beginning of path segment to denote a path parameter
  • Loading branch information
casualjim authored Nov 8, 2019
2 parents 553c9d1 + 0aa3c49 commit fe6d8cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions middleware/denco/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions middleware/denco/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fe6d8cc

Please sign in to comment.