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

Making Echo#Reverse() deterministic #1893

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 3 additions & 5 deletions echo.go
Expand Up @@ -540,7 +540,7 @@ func (e *Echo) add(host, method, path string, handler HandlerFunc, middleware ..
Path: path,
Name: name,
}
e.router.routes[method+path] = r
e.router.routes = append(e.router.routes, r)
return r
}

Expand Down Expand Up @@ -602,10 +602,8 @@ func (e *Echo) Reverse(name string, params ...interface{}) string {

// Routes returns the registered routes.
func (e *Echo) Routes() []*Route {
routes := make([]*Route, 0, len(e.router.routes))
for _, v := range e.router.routes {
routes = append(routes, v)
}
routes := make([]*Route, len(e.router.routes))
copy(routes, e.router.routes)
return routes
}

Expand Down
6 changes: 6 additions & 0 deletions echo_test.go
Expand Up @@ -1152,6 +1152,8 @@ 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("/repeated_name_one", dummyHandler).Name = "/repeated_name"
e.GET("/repeated_name_two", dummyHandler).Name = "/repeated_name"

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

for i := 0; i < 100; i++ {
assert.Equal("/repeated_name_one", e.Reverse("/repeated_name"))
}
}

func TestEcho_ListenerAddr(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions router.go
Expand Up @@ -9,7 +9,7 @@ type (
// request matching and URL path parameter parsing.
Router struct {
tree *node
routes map[string]*Route
routes []*Route
echo *Echo
}
node struct {
Expand Down Expand Up @@ -74,7 +74,7 @@ func NewRouter(e *Echo) *Router {
tree: &node{
methodHandler: new(methodHandler),
},
routes: map[string]*Route{},
routes: []*Route{},
echo: e,
}
}
Expand Down