ratpack / ratpack

Lean & powerful HTTP apps
https://ratpack.io
Other
1.94k stars 396 forks source link

Add the ability automatic type coercion on chain routes. #775

Open niwinz opened 9 years ago

niwinz commented 9 years ago

At this moment, with the ratpack chain you can build very powerfull route/handlers chains using simple literals, optional literals or regex matching (Path Bindings at http://ratpack.io/manual/current/api/ratpack/handling/Chain.html)

But I think that would be awesome to have more user friendly and with automatic type coersions matchers. For example:

The books/:id:long can be two things: a sugar syntax for books/:id:\d+ with and additional "converter" that coercers automatically the parameter to an long value. So when you obtain the parameters using context.getPathTokens() it will return a Map with id key with corresponding value as long instead of string.

That is feasible to have in ratpack? What do you think?

References:

ldaley commented 9 years ago

Did you see that path tokens is a http://ratpack.io/manual/current/api/ratpack/util/TypeCoercingMap.html ?

niwinz commented 9 years ago

Yes, I aware about it. As you can read on the issue description, this implies manual type coersion and I'm purposing automatic casting.

The purposed syntax allows to the user specify explicit converter in the url path parameter that will allow user explicit calls to .asInt(), and just have uniform code that access to the plain map.

Please, review the provided links, that shows you a way that flask uses that and is very very useful and comfortable to use.

I'm suggesting it because some users of catacumba (a web toolkit built on top of ratpack in clojure) have suggested it and I have thought that is maybe useful for all ratpack users.

ldaley commented 9 years ago

This is less applicable to strongly typed languages. This would mean that at the time of consumption we'd have to advertise as Object which negates any benefit here.

niwinz commented 9 years ago

In my opinion using object on this case is not bad. In scala we will use Any or in haskell maybe we will use a union of possible types for represent the map value type. Java I think does not provides a better way to do it, but it still a good way to do in java.

Parameters can be of different types and java has only one way I think to express it on types: using object. You are already using Object on .render method. And netty uses that pattern also. I think that using the same pattern for params that intrinsically can be of different types seems a good approach.

ldaley commented 9 years ago

Sorry Andrey, I will get back to this soon.