Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@arp242
Copy link
Contributor

@arp242 arp242 commented Apr 4, 2018

Currently a route in the form of /foo/:id.json means echo will detect
the parameter name id.json instead of the expected id. I think this
is rather counter-intuitive, as adding an extension to paths is a fairly
common use case.

With this change both a / and a . will be treated as the end of a
parameter name.

Benchmark before this change:

$ go test -bench .
[..]

goos: linux
goarch: amd64
pkg: github.com/labstack/echo
BenchmarkRouterStaticRoutes-4             100000             17743 ns/op               0 B/op          0 allocs/op
BenchmarkRouterGitHubAPI-4                 50000             33081 ns/op               1 B/op          0 allocs/op
BenchmarkRouterParseAPI-4                 300000              5370 ns/op               0 B/op          0 allocs/op
BenchmarkRouterGooglePlusAPI-4            200000              9183 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/labstack/echo        8.565s

After this change:

goos: linux
goarch: amd64
pkg: github.com/labstack/echo
BenchmarkRouterStaticRoutes-4             100000             17699 ns/op               0 B/op          0 allocs/op
BenchmarkRouterGitHubAPI-4                 50000             32962 ns/op               1 B/op          0 allocs/op
BenchmarkRouterParseAPI-4                 300000              5450 ns/op               0 B/op          0 allocs/op
BenchmarkRouterGooglePlusAPI-4            200000              9205 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/labstack/echo        8.590s

Fixes #599

Currently a route in the form of `/foo/:id.json` means echo will detect
the parameter name `id.json` instead of the expected `id`. I think this
is rather counter-intuitive, as adding an extension to paths is a fairly
common use case.

With this change both a `/` and a `.` will be treated as the end of a
parameter name.

Benchmark before this change:

	$ go test -bench .
	[..]

	goos: linux
	goarch: amd64
	pkg: github.com/labstack/echo
	BenchmarkRouterStaticRoutes-4             100000             17743 ns/op               0 B/op          0 allocs/op
	BenchmarkRouterGitHubAPI-4                 50000             33081 ns/op               1 B/op          0 allocs/op
	BenchmarkRouterParseAPI-4                 300000              5370 ns/op               0 B/op          0 allocs/op
	BenchmarkRouterGooglePlusAPI-4            200000              9183 ns/op               0 B/op          0 allocs/op
	PASS
	ok      github.com/labstack/echo        8.565s

After this change:

	goos: linux
	goarch: amd64
	pkg: github.com/labstack/echo
	BenchmarkRouterStaticRoutes-4             100000             17699 ns/op               0 B/op          0 allocs/op
	BenchmarkRouterGitHubAPI-4                 50000             32962 ns/op               1 B/op          0 allocs/op
	BenchmarkRouterParseAPI-4                 300000              5450 ns/op               0 B/op          0 allocs/op
	BenchmarkRouterGooglePlusAPI-4            200000              9205 ns/op               0 B/op          0 allocs/op
	PASS
	ok      github.com/labstack/echo        8.590s

Fixes #599
@codecov
Copy link

codecov bot commented Apr 4, 2018

Codecov Report

Merging #1101 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1101   +/-   ##
=======================================
  Coverage   81.14%   81.14%           
=======================================
  Files          25       25           
  Lines        1899     1899           
=======================================
  Hits         1541     1541           
  Misses        250      250           
  Partials      108      108
Impacted Files Coverage Δ
router.go 93.3% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 37f1a47...6474d5c. Read the comment docs.

@ready4god2513
Copy link
Contributor

@vishr Any thoughts on this? We've had to add some unpleasant changes in our middleware and it would be great for the echo router to support this pattern out of the box.

@vishr vishr self-requested a review April 10, 2018 16:45
@vishr
Copy link
Member

vishr commented Apr 10, 2018

The caveat I see is that you can't have a param with .. What do you guys think about it?

@arp242
Copy link
Contributor Author

arp242 commented Apr 10, 2018

The caveat I see is that you can't have a param with .. What do you guys think about it?

Yeah, that's true. I don't know if anyone actually needs/does this though?

Any change to this will be backwards-incompatible. For example someone mentioned routes in the form of /x/:{id}.json in the other issue, but this will mean routes with { and } will no longer work, so that's not compatible either (and also a bit harder to implement).

@vishr
Copy link
Member

vishr commented Apr 10, 2018

@ansel1 @alexaandru ?

@ansel1
Copy link

ansel1 commented Apr 10, 2018

It won’t affect us. Periods in param names are probably rare.

If the allowed chars for param names has been well documented before now, I would not make a breaking change. If it hasn’t been, then this was always the intended behavior, and this is just fixing a bug ;)

@vishr vishr merged commit bfa1463 into labstack:master Apr 10, 2018
@vishr
Copy link
Member

vishr commented Apr 10, 2018

@Carpetsmoker @ansel1 thanks for your contribution 🎉

@arp242 arp242 deleted the dotroute branch April 10, 2018 18:42
vishr pushed a commit that referenced this pull request Apr 10, 2018
This is a follow-up to #1101. It turns out that that patch is
incomplete, as a similar check also needs to be added in the
`Router.Add()` method.

I don't understand why the test works fine, but when using it in a real
application. For example with this example:

	func main() {
		e := echo.New()
		e.GET("/xxx/:id.json", func(c echo.Context) error {
			return c.String(200, fmt.Sprintf("%#v: names: %#v; vals: %#v",
				c.Path(), c.ParamNames(), c.ParamValues()))
		})
		log.Fatal(e.Start(":8000"))
	}

Gives a 404 on `/xxx/42.json`, and for `/xxx/42` it gives the output:

	/xxx/:id.json": names: []string{"id.json"}; vals: []string{"42"}

It makes sense to add the test there too; I just don't get why the test
cases that I added in ##1101 *does* produce the correct output :-/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.