-
-
Notifications
You must be signed in to change notification settings - Fork 988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Made nested regexp pattern route properly. #506
Conversation
@Jahaja very nice work on this. I think this is very close. I've tried the code against the test case below and found it to fail, but looks extremely close! func TestRegexpRecursive(t *testing.T) {
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
tr := &node{}
tr.InsertRoute(mGET, "/one/{firstId:[a-z0-9-]+}/{secondId:[a-z0-9-]+}/first", hStub1)
tr.InsertRoute(mGET, "/one/{firstId:[a-z0-9-_]+}/{secondId:[a-z0-9-_]+}/second", hStub2)
// log.Println("~~~~~~~~~")
// log.Println("~~~~~~~~~")
// debugPrintTree(0, 0, tr, 0)
// log.Println("~~~~~~~~~")
// log.Println("~~~~~~~~~")
tests := []struct {
r string // input request path
h http.Handler // output matched handler
k []string // output param keys
v []string // output param values
}{
{r: "/one/hello/world/first", h: hStub1, k: []string{"firstId", "secondId"}, v: []string{"hello", "world"}},
{r: "/one/hi_there/ok/second", h: hStub2, k: []string{"firstId", "secondId"}, v: []string{"hi_there", "ok"}},
{r: "/one///first", h: nil, k: []string{}, v: []string{}},
{r: "/one/hi/123/second", h: hStub2, k: []string{"firstId", "secondId"}, v: []string{"hi", "123"}},
}
for i, tt := range tests {
rctx := NewRouteContext()
_, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
var handler http.Handler
if methodHandler, ok := handlers[mGET]; ok {
handler = methodHandler.handler
}
paramKeys := rctx.routeParams.Keys
paramValues := rctx.routeParams.Values
if fmt.Sprintf("%v", tt.h) != fmt.Sprintf("%v", handler) {
t.Errorf("input [%d]: find '%s' expecting handler:%v , got:%v", i, tt.r, tt.h, handler)
}
if !stringSliceEqual(tt.k, paramKeys) {
t.Errorf("input [%d]: find '%s' expecting paramKeys:(%d)%v , got:(%d)%v", i, tt.r, len(tt.k), tt.k, len(paramKeys), paramKeys)
}
if !stringSliceEqual(tt.v, paramValues) {
t.Errorf("input [%d]: find '%s' expecting paramValues:(%d)%v , got:(%d)%v", i, tt.r, len(tt.v), tt.v, len(paramValues), paramValues)
}
}
} results from running: ➜ chi git:(master) ✗ go test -v -run=RegexpRecursive .
=== RUN TestRegexpRecursive
TestRegexpRecursive: tree_test.go:56: input [3]: find '/one/hi/123/second' expecting paramValues:(2)[hi 123] , got:(2)[ 123]
--- FAIL: TestRegexpRecursive (0.00s)
FAIL
FAIL github.com/go-chi/chi 0.002s
FAIL |
the other bit of testing here is to ensure the algorithm doesn't create any new allocations and performance is the same as previously (for conditions that aren't regexp type params). See https://gist.github.com/pkieltyka/123032f12052520aaccab752bd3e78cc |
Thanks! I found the issue and committed a fix for it. The run from the benchmarks looks the same to me:
When I changed
|
@Jahaja thank you again! merged :) |
Fixes #411