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

Anchors #74

Open
rschwass opened this issue Aug 22, 2017 · 5 comments
Open

Anchors #74

rschwass opened this issue Aug 22, 2017 · 5 comments

Comments

@rschwass
Copy link

rschwass commented Aug 22, 2017

If I dont set anchors and the option to ignore checking for them, is mustermann automatically putting them in at the beginning and end of my route like in sinatra i do:

get %r{/stuff/([0-9a-f]{32})}i

is that the same as

get %r{\A/stuff/([0-9a-f]{32})\z}i, :mustermann_opts => { :type => :regexp, :check_anchors => false}

How do I know where mustermann is applying the anchors without specifically setting them?

@namusyaka
Copy link
Member

@rschwass Could you write up a spec for reproducing your paint point?

@yb66
Copy link

yb66 commented Dec 12, 2017

First run of an old app under sinatra v2, exciting!
Oh, an error…

web.1  | ! Unable to load application: Mustermann::CompileError: regular expression should not contain \A: "\\A\\/(\\d+)\\/?\\Z"

I find I've wrapped a lot of path patterns in anchors.

$ ag '\\A'
app/api.rb
127:    options %r{\A/(\d+)/?\Z} do |_|
182:    get %r{\A/(\d+)/?\Z}, :provides => :json do |id|
191:    put %r{\A/(\d+)/?\Z}, :provides => :json do |id| # update
210:    delete %r{\A/(\d+)/?\Z}, :provides => :json do |id|
220:    options %r{\A/(\d+)/tags/?\Z} do |_|
227:    get %r{\A/(\d+)/tags/?\Z}, :provides => :json do |id|
234:    patch %r{\A/(\d+)/tags/?\Z}, :provides => :json do |id|
259:    delete %r{\A/(\d+)/tags/?\Z}, :provides => :json do |id|

I update all of the above to use ^ and $

# run app again
web.1  | ! Unable to load application: Mustermann::CompileError: regular expression should not contain ^: "^\\/(\\d+)\\/?$"

Using anchors is good practice with regex but Mustermann is new to me. If it does deal with it then I'm fine, it's just good to know or not.

Regards,
iain

@rkh
Copy link
Member

rkh commented Dec 12, 2017

Mustermann will insert them for you. The reason it insists on not having them in there is to allow pattern concatenation without having to rewrite the regular expression (which might be faulty).

@yb66
Copy link

yb66 commented Dec 12, 2017

@rkh Okay, that makes sense. Thanks.

@zzak
Copy link
Member

zzak commented Dec 12, 2017

Mustermann will insert them for you. The reason it insists on not having them in there is to allow pattern concatenation without having to rewrite the regular expression (which might be faulty).

That is a really good point I never thought of before, perhaps this should go in the docs if it's not already!

foxxx0 added a commit to vhost-api/vhost-api that referenced this issue Mar 24, 2018
sinatra/mustermann#74

Signed-off-by: Thore Bödecker <me@foxxx0.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants