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

Support regex matching for URLRouter placeholders #1479

Open
s-ludwig opened this issue Apr 21, 2016 · 7 comments
Open

Support regex matching for URLRouter placeholders #1479

s-ludwig opened this issue Apr 21, 2016 · 7 comments

Comments

@s-ludwig
Copy link
Member

See #1062.

Suggested syntax coming from Python: router.get("/items/:(?P<var>[0-9]+)/something/", ...)

@SkyzohKey
Copy link

Another nice syntax could be router.get("/items/:var([0-9]+)/something/", ...)

@mintyfresh
Copy link
Contributor

Could this also be extended to add strong parameter types?

ie. router.get("/items/:var(uint)/something/", ...)

@s-ludwig
Copy link
Member Author

It won't work at that level, because the router takes runtime strings. But the vibe.web.web/vibe.web.rest modules already provide a type checked interface on top.

@mintyfresh
Copy link
Contributor

I actually meant that for baked in regex patterns to match those types, so as to not have to repeat yourself over and over, whenever you want a URL parameter to match an integer, for example.

@s-ludwig
Copy link
Member Author

Yes, but since it is based on a runtime parameter, that would mean that it can only work with a set of known types, which either limits its usefulness or makes for a relatively cumbersome interface (need to register() types up front). It would also mean that the parsing work has to be done twice (worse performance and risk of having inconsistent parsers).

Generally, I don't think the router should be extended with high-level functionality, but should stay focused on it's main task of performing plain string matching as much as possible. Layering this on top is perfectly possible and avoids the mentioned issues, as the web interface generator shows.

@mintyfresh
Copy link
Contributor

Fair point. Adding this sort of functionality in project-space is equally viable.

@wilzbach
Copy link
Member

wilzbach commented Feb 8, 2017

A first step could be to add support for optional parameters that behave like this:

/foo/:var?
/foo/bar [match with var="bar"]
/foo/bar [match with var=""]

or in D code:

@path("/foo/:var?", var)
void getFoo(string var = "defaultValue")

At the moment having optional parameters in a path with the REST or Web wrapper is a bit non-DRY:

void getFoo() {
   getFoo("defaultValue");
}
@path("/foo/:var", var)
void getFoo(string var)

@s-ludwig s-ludwig added the http label Jul 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants