Skip to content

Instantly share code, notes, and snippets.

@jroper
Created March 5, 2015 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jroper/3bbd3e525c795123d0bd to your computer and use it in GitHub Desktop.
Save jroper/3bbd3e525c795123d0bd to your computer and use it in GitHub Desktop.
Play query string DSL possibilities
// In the path string:
case GET(p"/foo?param1=${param1}&param2=${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
@adriaanm
Copy link

adriaanm commented Mar 5, 2015

Cool! I also like the second one better. Just FTR, you could also extend the first example with printf-style format specifiers

@sirthias
Copy link

sirthias commented Mar 7, 2015

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