mustermann icon indicating copy to clipboard operation
mustermann copied to clipboard

Anchors

Open rschwass opened this issue 6 years ago • 5 comments

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?

rschwass avatar Aug 22 '17 19:08 rschwass

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

namusyaka avatar Oct 17 '17 00:10 namusyaka

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

yb66 avatar Dec 12 '17 07:12 yb66

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).

rkh avatar Dec 12 '17 09:12 rkh

@rkh Okay, that makes sense. Thanks.

yb66 avatar Dec 12 '17 13:12 yb66

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!

zzak avatar Dec 12 '17 13:12 zzak