-
Notifications
You must be signed in to change notification settings - Fork 37
TBrookRouter and regex support #52
Comments
@uaply, regex support seems a good idea? :) |
Yeah, regexps are frequently used for routing, for example in Django under Python. They are powerful, and sometimes too much powerful :) The only problem is how to implement it. We should find good RegExp library under Pascal. The one which present in FPC seems doesn't support named groups. Without named groups it will be very hard to use, harder than with current syntax. |
http://www.regular-expressions.info/download/TPerlRegEx.zip is a wrapper over PCRE and is licensed under MPL. Maybe only requires some cleanup to make it platform independent. I prefer a pure Pascal solution so I guess it's the time somebody needs to extend Sorokin's TRegExpr to support named group. The code is quite neat so I guess it won't be too difficult to support. |
We can use Sorokin's solution (it was moved to FPC after this looong thread: http://lists.lazarus.freepascal.org/pipermail/lazarus/2011-December/069381.html), and with an advantage: it's a code that comes with the Free Pascal. I think something like:
With What do you think? I'll test all uaply changes and merge it to the master branch, so we'll be ready for this new implementation. |
How would be router path look like with this regexpr library? Also we need to pass variables to action. Now they are passed as JSON dictionary and accessed by string names. |
You can see how to Slim implement it: Brook router is based on Slim framework. |
PHP has full-fledged support of regexp, so routes in Slim implementation internally are converted in such way: But we don't have named group functionality now in pascal, the best we can do is |
Hm... We can test if the chars "(" and ")" (the sintax will be:
Two variables will created (
Seems a good idea? :/ |
...Or, we can send a patch to improve regex lib of Free Pascal. |
I was also thinking about how we can pre-process initial route path to memorize names of variables. But things are complicated by the fact groups could be nested: '(?(?[^/]+?).?(?[^/]*))'. The better solution is to modify regexp lib, but not to do patches over top of it. As you see RegExp are much more cryptic :) It is obvious drawback, so we should leave old syntax too. |
Hm.. we can implement a basic regex support. I thought of another possibility, something like: TAction.Register('/user/:name/:id', ['^[a-z]$', '^[0-9]$'], ['Invalid name: %s.', 'Invalid ID: %d.']); Usage:
So regex will be used to validate paths in URL. Other demo:
What do you think?
Hehehe... we keep the old code, and the new feature using current implementation of RegExpr available on Free Pascal. :) |
Oops, I forgot ... I'm planning show a new idea about middleware class support. I'll create a new issue talking about ... Imagine it:
... procedure TMyMiddlewareClass.Execute;
var
Vid: Integer;
begin
Vid := Action.Fields['id'].AsInteger;
if not VId in [0..9] then
Error('Invalid ID: %d', [VId]);
end; And reuse
|
... Or: initialization
TDownloadAction.Register('/download/:id');
TDownloadAction.RegisterMiddleware(TMyMiddlewareClass); |
Done. (#54) |
TAction.Register('/user/:name/:id', ['^[a-z]$', '^[0-9]$'], ['Invalid name: %s.', 'Invalid ID: %d.']); Good idea – and it has very clear meaning. We validate values with regexp before matching. Error messages probably will not be needed (maybe only for debug). |
Hello @uaply, you want to implement this feature? |
Done, you can use |
Hello,
We planning to implement regex support in Brook router too. Please see here.
See other feature that will be implemented here.
The text was updated successfully, but these errors were encountered: