Skip to content

Commit

Permalink
🐛 bug: fix constraints when to use multiple params
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Sep 7, 2022
1 parent bb10225 commit 6603de9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
14 changes: 3 additions & 11 deletions path.go
Expand Up @@ -362,23 +362,15 @@ func findLastCharsetPosition(search string, charset []byte) int {
// findNextCharsetPositionConstraint search the next char position from the charset
// unlike findNextCharsetPosition, it takes care of constraint start-end chars to parse route pattern
func findNextCharsetPositionConstraint(search string, charset []byte) int {
constraintStart := findNextNonEscapedCharsetPosition(search, parameterConstraintStartChars)
constraintEnd := findNextNonEscapedCharsetPosition(search, parameterConstraintEndChars)
nextPosition := -1
constraintStart := -1
constraintEnd := -1

for _, char := range charset {
pos := strings.IndexByte(search, char)

if char == paramConstraintStart {
constraintStart = pos
}

if char == paramConstraintEnd {
constraintEnd = pos
}

if pos != -1 && (pos < nextPosition || nextPosition == -1) {
if pos > constraintStart && pos < constraintEnd {
if (pos > constraintStart && pos > constraintEnd) || (pos < constraintStart && pos < constraintEnd) {
nextPosition = pos
}
}
Expand Down
58 changes: 58 additions & 0 deletions path_test.go
Expand Up @@ -568,6 +568,35 @@ func Test_Path_matchParams(t *testing.T) {
{url: "/api/v1/1200", params: []string{"1200"}, match: true},
{url: "/api/v1/true", params: []string{"true"}, match: false},
})
testCase("/api/v1/:lang<len(2)>/videos/:page<range(100,1500)>", []testparams{
{url: "/api/v1/try/videos/200", params: []string{"try", "200"}, match: false},
{url: "/api/v1/tr/videos/1800", params: []string{"tr", "1800"}, match: false},
{url: "/api/v1/tr/videos/100", params: []string{"tr", "100"}, match: true},
{url: "/api/v1/e/videos/10", params: []string{"tr", "200"}, match: false},
})
testCase("/api/v1/:lang<len(2)>/:page<range(100,1500)>", []testparams{
{url: "/api/v1/try/200", params: []string{"try", "200"}, match: false},
{url: "/api/v1/tr/1800", params: []string{"tr", "1800"}, match: false},
{url: "/api/v1/tr/100", params: []string{"tr", "100"}, match: true},
{url: "/api/v1/e/10", params: []string{"tr", "200"}, match: false},
})
testCase("/api/v1/:lang/:page<range(100,1500)>", []testparams{
{url: "/api/v1/try/200", params: []string{"try", "200"}, match: true},
{url: "/api/v1/tr/1800", params: []string{"tr", "1800"}, match: false},
{url: "/api/v1/tr/100", params: []string{"tr", "100"}, match: true},
{url: "/api/v1/e/10", params: []string{"tr", "200"}, match: false},
})
testCase("/api/v1/:lang<len(2)>/:page", []testparams{
{url: "/api/v1/try/200", params: []string{"try", "200"}, match: false},
{url: "/api/v1/tr/1800", params: []string{"tr", "1800"}, match: true},
{url: "/api/v1/tr/100", params: []string{"tr", "100"}, match: true},
{url: "/api/v1/e/10", params: []string{"tr", "200"}, match: false},
})
testCase("/api/v1/:date<datetime(2006\\-01\\-02)>/:regex<regex(p([a-z]+)ch)>", []testparams{
{url: "/api/v1/2005-11-01/a", params: []string{"2005-11-01", "a"}, match: false},
{url: "/api/v1/2005-1101/paach", params: []string{"2005-1101", "a"}, match: false},
{url: "/api/v1/2005-11-01/peach", params: []string{"2005-11-01", "peach"}, match: true},
})
}

func Test_Utils_GetTrimmedParam(t *testing.T) {
Expand Down Expand Up @@ -793,4 +822,33 @@ func Benchmark_Path_matchParams(t *testing.B) {
{url: "/api/v1/1200", params: []string{"1200"}, match: true},
{url: "/api/v1/true", params: []string{"true"}, match: false},
})
benchCase("/api/v1/:lang<len(2)>/videos/:page<range(100,1500)>", []testparams{
{url: "/api/v1/try/videos/200", params: []string{"try", "200"}, match: false},
{url: "/api/v1/tr/videos/1800", params: []string{"tr", "1800"}, match: false},
{url: "/api/v1/tr/videos/100", params: []string{"tr", "100"}, match: true},
{url: "/api/v1/e/videos/10", params: []string{"tr", "200"}, match: false},
})
benchCase("/api/v1/:lang<len(2)>/:page<range(100,1500)>", []testparams{
{url: "/api/v1/try/200", params: []string{"try", "200"}, match: false},
{url: "/api/v1/tr/1800", params: []string{"tr", "1800"}, match: false},
{url: "/api/v1/tr/100", params: []string{"tr", "100"}, match: true},
{url: "/api/v1/e/10", params: []string{"tr", "200"}, match: false},
})
benchCase("/api/v1/:lang/:page<range(100,1500)>", []testparams{
{url: "/api/v1/try/200", params: []string{"try", "200"}, match: true},
{url: "/api/v1/tr/1800", params: []string{"tr", "1800"}, match: false},
{url: "/api/v1/tr/100", params: []string{"tr", "100"}, match: true},
{url: "/api/v1/e/10", params: []string{"tr", "200"}, match: false},
})
benchCase("/api/v1/:lang<len(2)>/:page", []testparams{
{url: "/api/v1/try/200", params: []string{"try", "200"}, match: false},
{url: "/api/v1/tr/1800", params: []string{"tr", "1800"}, match: true},
{url: "/api/v1/tr/100", params: []string{"tr", "100"}, match: true},
{url: "/api/v1/e/10", params: []string{"tr", "200"}, match: false},
})
benchCase("/api/v1/:date<datetime(2006\\-01\\-02)>/:regex<regex(p([a-z]+)ch)>", []testparams{
{url: "/api/v1/2005-11-01/a", params: []string{"2005-11-01", "a"}, match: false},
{url: "/api/v1/2005-1101/paach", params: []string{"2005-1101", "a"}, match: false},
{url: "/api/v1/2005-11-01/peach", params: []string{"2005-11-01", "peach"}, match: true},
})
}

1 comment on commit 6603de9

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 6603de9 Previous: ca0f663 Ratio
Benchmark_Ctx_Protocol 17.59 ns/op 0 B/op 0 allocs/op 3.115 ns/op 0 B/op 0 allocs/op 5.65
Benchmark_StatusMessage/default 13.34 ns/op 0 B/op 0 allocs/op 5.411 ns/op 0 B/op 0 allocs/op 2.47

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.