Skip to content

Instantly share code, notes, and snippets.

@tbje
Created November 11, 2010 14:25
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 tbje/672551 to your computer and use it in GitHub Desktop.
Save tbje/672551 to your computer and use it in GitHub Desktop.
optional[String, Int](S.param("optional"), Helpers.asInt(_))
def optional[A,B](v: Box[A], func: A => Box[B]): Box[Box[B]] =
v match {
case Full(v) => func(v) match {
case Full(v) => Full(Full(v))
case Empty => Empty
case Failure(msg, ex, chain) => Failure(msg, ex, chain)
case ParamFailure(msg, ex, chain, param) => ParamFailure(msg, ex, chain, param)
}
case Empty => Full(Empty)
case Failure(msg, ex, chain) => Failure(msg, ex, chain)
case ParamFailure(msg, ex, chain, param) => ParamFailure(msg, ex, chain, param)
}
/* In lift you would typically do: */
for {
requiredInt <- S.param("required") ?~ "required is missing" ~> 400 // 1
requiredInt <- Helpers.asInt(requiredInt) ?~ "required must be an integer" ~> 400 // 2
optionalInt <- optional(S.param("optional"), (x:String)=>Helpers.asInt(x)) ?~
"optional must be an integer" ~> 400 // 3
} yield (requiredInt, optionalInt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment