-
Notifications
You must be signed in to change notification settings - Fork 102
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
argument constraints and coercions #77
Comments
So the idea right now is something like (for the simple case) Package MyApp::Controller::Root; sub myaction :Local Args(Int) { and the arg would not match if it was not an Int. This would probably use types registered into moose, or into a project namespace (being able to do with with Type::Tiny would be ideal). Furthermore which could allow that a constraint has coercions in the what that Moose and Type::Tiny allow so that the following would also work: package MyApp::Controller::Root; sub myaction :Local Args(User) { where $user is a MyApp::Schema::Result::User (a row in the database basically) the idea is that we allow Catalyst to register Types (instances or classes of the defined models) and add coercions to them so that you could coerce an Integer to a DBIC::Result. In this case if the coerce failed to produce the expected type, the action would not match. Would remove a lot of boilerplate I think and possible advance the conversation on how we are going to improve Catalyst IOC and related features. Since the coercions could be more complex than one arg we'd probably need a syntax like Args(User{2},...) In the above case if the coerce fails that would mean the action does not match. so you'd do like sub found :Path(user) Args(User) { sub not_found :Path(user) Args(Id) { handle not found the $id in the DB} the question is the best approach to handle the coercions. Would it be enough to have a ->coerce_from or similar method on the model or should we define a type of 'type library and coercion library' (see type::tiny and all the related moose stuff. I don't want this to get too complex but I do want people to keep in mind I'd like to see something like this workable for query and post params as well, and possible even in the body response (return a DBIC row, coerce it to a string body, for example. That's down the road but its good to keep in mind. |
another thing this could be used to provide people with 'regexp' style Args, to stop the complaining since we removed Regexp! |
So for another take on this, it was suggested to skip the subroutine attributes approach and use a keyword (or keyword + method) signature approach (see Kavorka, Moops). concepts /examples action myaction under(..) args(Int $id, Date $date) { such that would match .../100/01-jan-2014 for example 2:33 PM and if you don't want them put into @_, args(Int method myaction(User 2:38 PM not sure what 'User $user' would do, but assuming that's the DI (Dependency Injection, sic) trigger Concerns I have with the approach include that after reviewing the examples on CPAN it feels like a lot more effort and additional would require dependencies that don't have a solid pass rate (for example Kavorka use Keyword::Parse which is marked deprecated with known issues and has a fail to pass it own test rate of >5% In general we aim for 99%+ pass rate on Catalyst tests. Upsides include a definitive step away from subroutine attributes (which are widely despised) and the possibility of a more concise and elegant syntax. |
some notes from chat 1:24 PM @mst what I reckon you could do is |
completed for Args and CaptureArgs. the rest is on hold |
more later
The text was updated successfully, but these errors were encountered: