Skip to content

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

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

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

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add :from-:to range route formats
  • Loading branch information
ProgrammingMuffin committed Dec 17, 2023
commit 85625bf50ea386bd07c8016407266228f2fca7a9
6 changes: 3 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (r *Router) Reverse(name string, params ...interface{}) string {
}
if n < ln && (route.Path[i] == '*' || (!hasBackslash && route.Path[i] == ':')) {
// in case of `*` wildcard or `:` (unescaped colon) param we replace everything till next slash or end of path
for ; i < l && route.Path[i] != '/'; i++ {
for ; i < l && route.Path[i] != '/' && route.Path[i] != '-'; i++ {
}
uri.WriteString(fmt.Sprintf("%v", params[n]))
n++
Expand Down Expand Up @@ -220,7 +220,7 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
j := i + 1

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

pnames = append(pnames, path[j:i])
Expand Down Expand Up @@ -660,7 +660,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] != '/'; i++ {
for ; i < l && search[i] != '/' && search[i] != '-'; i++ {
}
}

Expand Down
36 changes: 36 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,42 @@ func TestRouterParam(t *testing.T) {
}
}

func TestRouterRangeParam(t *testing.T) {
e := New()
r := e.router

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

var testCases = []struct {
name string
whenURL string
expectRoute interface{}
expectParam map[string]string
}{
{
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"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

c := e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, tc.whenURL, c)

c.handler(c)
assert.Equal(t, tc.expectRoute, c.Get("path"))
for param, expectedValue := range tc.expectParam {
assert.Equal(t, expectedValue, c.Param(param))
}
checkUnusedParamValues(t, c, tc.expectParam)
})
}
}

func TestRouter_addAndMatchAllSupportedMethods(t *testing.T) {
var testCases = []struct {
name string
Expand Down