Skip to content

Instantly share code, notes, and snippets.

@joecwu
Created July 20, 2015 20:31
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 joecwu/f62178bdd9eb0fa20cb7 to your computer and use it in GitHub Desktop.
Save joecwu/f62178bdd9eb0fa20cb7 to your computer and use it in GitHub Desktop.
case class InvalidArgumentException(msg:String) extends Exception(msg)
case class SearchUsersRequest(keyword: Option[String], userType: Option[Int], maxNum: Option[Int], returnFields: List[String]) {
def getKeywork() = keyword | {throw InvalidArgumentException("keyword cannot be empyt.")}
def getUserType() = {
userType.map{ tp => (1 until 10 contains tp) ? tp | {throw InvalidArgumentException("userType must between 1 to 10.")}} |{ throw InvalidArgumentException("userType cannot be empyt.")}
}
def getMaxNum() = maxNum.getOrElse(10)
def getReturnFields() = (returnFields.length > 0) ? returnFields | {throw InvalidArgumentException("must provide returnFields.")}
}
class BaseException(val msg:String) extends Exception(msg)
case class InvalidArgumentException(override val msg:String) extends BaseException(msg)
case class SearchUsersRequest(keyword: Option[String], userType: Option[Int], maxNum: Option[Int], returnFields: List[String]) {
def getKeyword() = keyword.map{_.successNel[BaseException]} | InvalidArgumentException("keyword cannot be empyt.").failNel[String]
def getUserType() = userType.map{ tp => (1 until 10 contains tp) ? tp.successNel[BaseException] | InvalidArgumentException("userType must between 1 to 10.").failNel[Int]} | InvalidArgumentException("userType cannot be empyt.").failNel
def getMaxNum() = maxNum.getOrElse(10).successNel[BaseException]
def getReturnFields() = (returnFields.length > 0) ? returnFields.successNel[BaseException] | InvalidArgumentException("must provide returnFields.").failNel[List[String]]
def validate() = {
( getKeyword() |@| getUserType() |@| getMaxNum |@| getReturnFields ) { (keyword,userType,maxNum,returnFields) => (keyword,userType,maxNum,returnFields) }
}
}
object TestApp {
def run = {
val reqFail = SearchUsersRequest(none[String],11.some,3.some,List("field1"))
val reqSuccess = SearchUsersRequest("joe".some,6.some,20.some,List("field1","field2"))
reqSuccess.validate match {
case scalaz.Success(resp) => resp match {case (keyword,userType,maxNum,fields) => println(s"Got keyword:[$keyword] userType:[$userType] maxNum:[$maxNum] fields:[${fields.mkString(",")}]")}
case scalaz.Failure(errList) => println(errList.toList.map(_.msg))
}
//REPL result for reqSuccess
//Got keyword:[joe] userType:[6] maxNum:[20] fields:[field1,field2]
//REPL result for reqFail
//List(keyword cannot be empyt., userType must between 1 to 10.)
// Handle success value directly
reqSuccess.validate.map{
case (keyword,userType,maxNum,fields) => println(s"Got keyword:[$keyword] userType:[$userType] maxNum:[$maxNum] fields:[${fields.mkString(",")}]")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment