Created
March 5, 2015 22:06
-
-
Save jroper/3bbd3e525c795123d0bd to your computer and use it in GitHub Desktop.
Play query string DSL possibilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In the path string: | |
case GET(p"/foo?param1=${param1}¶m2=${param2}") | |
// Problems: doesn't support extracting lists of parameters or optional parameters | |
// Using ? and & extractors to combine multiple string interpolated query string parameters: | |
case GET(p"/foo" ? q"required=${required}" & q?"optional=${optional}" & q*"many=${many}") | |
// I think I like this one, q extracts String, q? extracts Option[String], q* extracts List[String] | |
// One question, how strict should we make it? | |
// Eg, we can do: | |
object ? { def unapply[A](a: A) = Some((a, a)) } | |
object & { def unapply[A](a: A) = Some((a, a)) } | |
// And then make the q, q? and q* extractors all take RequestHeader. | |
// Or, we could do this: | |
object ? { def unapply(req: RequestHeader) = Some((req.path, req.queryString)) } | |
object & { def unapply(qs: Map[String, Seq[String]]) = Some((qs, qs)) } | |
// And then q, q? and q* extractors can take Map[String, Seq[String]] | |
// And, we could implement q, q? and q* using a macro, validating the format of the | |
// extractor at compile time |
What about typed extraction?
Say, I want an orderId
parameter to be a positive 32-bit signed integer?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool! I also like the second one better. Just FTR, you could also extend the first example with printf-style format specifiers