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

Proper colon support in reverse #2416

Merged
merged 4 commits into from Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions echo_test.go
Expand Up @@ -1525,6 +1525,7 @@ func TestEchoReverse(t *testing.T) {
e.GET("/params/:foo", dummyHandler).Name = "/params/:foo"
e.GET("/params/:foo/bar/:qux", dummyHandler).Name = "/params/:foo/bar/:qux"
e.GET("/params/:foo/bar/:qux/*", dummyHandler).Name = "/params/:foo/bar/:qux/*"
e.GET("/params\\::customVerb", dummyHandler).Name = "/params:customVerb"
aldas marked this conversation as resolved.
Show resolved Hide resolved

assert.Equal(t, "/static", e.Reverse("/static"))
assert.Equal(t, "/static", e.Reverse("/static", "missing param"))
Expand All @@ -1537,6 +1538,8 @@ func TestEchoReverse(t *testing.T) {
assert.Equal(t, "/params/one/bar/:qux", e.Reverse("/params/:foo/bar/:qux", "one"))
assert.Equal(t, "/params/one/bar/two", e.Reverse("/params/:foo/bar/:qux", "one", "two"))
assert.Equal(t, "/params/one/bar/two/three", e.Reverse("/params/:foo/bar/:qux/*", "one", "two", "three"))

assert.Equal(t, "/params:PATCH", e.Reverse("/params:customVerb", "PATCH"))
}

func TestEchoReverseHandleHostProperly(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion router.go
Expand Up @@ -159,7 +159,10 @@ func (r *Router) Reverse(name string, params ...interface{}) string {
for _, route := range r.routes {
if route.Name == name {
for i, l := 0, len(route.Path); i < l; i++ {
if (route.Path[i] == ':' || route.Path[i] == '*') && n < ln {
if route.Path[i] == '\\' {
aldas marked this conversation as resolved.
Show resolved Hide resolved
continue
}
if ((route.Path[i] == ':' && (i == 0 || route.Path[i-1] != '\\')) || route.Path[i] == '*') && n < ln {
for ; i < l && route.Path[i] != '/'; i++ {
}
uri.WriteString(fmt.Sprintf("%v", params[n]))
Expand Down