Skip to content
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

[issue-2557] Add :from-:to range route formats #2560

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
}

for i, lcpIndex := 0, len(path); i < lcpIndex; i++ {
if path[i] == ':' {
if path[i] == ':' || path[i] == '{' {
bracketsOpen := false
if path[i] == '{' {
bracketsOpen = true
}
if i > 0 && path[i-1] == '\\' {
path = path[:i-1] + path[i:]
i--
Expand All @@ -220,11 +224,15 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
j := i + 1

r.insert(method, path[:i], staticKind, routeMethod{})
for ; i < lcpIndex && (path[i] != '/' && path[i] != '-'); i++ {
for ; i < lcpIndex && (path[i] != '/' && !(path[j-1] == '{' && path[i] == '}')); i++ {
}

pnames = append(pnames, path[j:i])
path = path[:j] + path[i:]
if bracketsOpen {
path = path[:j-1] + ":" + path[i+1:]
} else {
path = path[:j] + path[i:]
}
i, lcpIndex = j, len(path)

if i == lcpIndex {
Expand Down Expand Up @@ -660,7 +668,7 @@ func (r *Router) Find(method, path string, c Context) {
// act similarly to any node - consider all remaining search as match
i = l
} else {
for ; i < l && search[i] != '/' && search[i] != '-'; i++ {
for ; i < l && search[i] != '/' && !(search[i] == '-' || search[i] == '.'); i++ {
Copy link
Author

Choose a reason for hiding this comment

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

@aldas This is the best I could come up with, after spending some time trying around some stuff. Turns out that the dependency on "-" or "." is removed from the "route adding" and instead it is now on the "route detection". But one place should definitely have it or else, it cannot distinguish the params.

Copy link
Author

@ProgrammingMuffin ProgrammingMuffin Dec 18, 2023

Choose a reason for hiding this comment

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

actually I might be wrong here. Let me go over this again

Edit done. This is resolved.

}
}

Expand Down Expand Up @@ -714,7 +722,6 @@ func (r *Router) Find(method, path string, c Context) {
if currentNode == nil && previousBestMatchNode == nil {
return // nothing matched at all
}

// matchedHandler could be method+path handler that we matched or notFoundHandler from node with matching path
// user provided not found (404) handler has priority over generic method not found (405) handler or global 404 handler
var rPath string
Expand Down
13 changes: 10 additions & 3 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,8 @@ func TestRouterRangeParam(t *testing.T) {
e := New()
r := e.router

r.Add(http.MethodGet, "/flights/:from-:to", handlerFunc)
r.Add(http.MethodGet, "/flights/{from}.{to}", handlerFunc)
r.Add(http.MethodGet, "/flights/{from}-{to}", handlerFunc)

var testCases = []struct {
name string
Expand All @@ -741,9 +742,15 @@ func TestRouterRangeParam(t *testing.T) {
expectParam map[string]string
}{
{
name: "route /flights/LAX-DEN to /flights/:from-:to",
name: "route /flights/LAX.DEN to /flights/{from}.{to}",
whenURL: "/flights/LAX.DEN",
expectRoute: "/flights/{from}.{to}",
expectParam: map[string]string{"from": "LAX", "to": "DEN"},
},
{
name: "route /flights/LAX-DEN to /flights/{from}-{to}",
whenURL: "/flights/LAX-DEN",
expectRoute: "/flights/:from-:to",
expectRoute: "/flights/{from}-{to}",
expectParam: map[string]string{"from": "LAX", "to": "DEN"},
},
}
Expand Down