Created
November 11, 2010 14:25
-
-
Save tbje/672551 to your computer and use it in GitHub Desktop.
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
optional[String, Int](S.param("optional"), Helpers.asInt(_)) |
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
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