Created
January 9, 2016 18:24
-
-
Save dkomanov/a3af45685d595e2ab8a2 to your computer and use it in GitHub Desktop.
[scala] result code: either
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 processRequestEither(userId: UUID, requestId: UUID): BusinessResult = { | |
val result: Either[BusinessResult, BusinessResult] = for { | |
user <- either(getUser(userId), BusinessResult.UserNotFound) | |
request <- either(getRequestById(requestId), BusinessResult.RequestNotFound) | |
_ <- either(checkAccess(request, user).toOption, BusinessResult.NotOwner) | |
} yield BusinessResult.Ok | |
result.fold(identity, identity) | |
} | |
private def either[T](opt: Option[T], result: BusinessResult) = | |
opt.fold[Either[BusinessResult, T]](Left(result))(Right(_)).right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment