Skip to content

Instantly share code, notes, and snippets.

@dkomanov
Created January 9, 2016 18:24
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 dkomanov/a3af45685d595e2ab8a2 to your computer and use it in GitHub Desktop.
Save dkomanov/a3af45685d595e2ab8a2 to your computer and use it in GitHub Desktop.
[scala] result code: either
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