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
implicit class FutureOfBooleanExtensions(val future: Future[Boolean]) extends AnyVal { | |
def &&(other: => Future[Boolean]): Future[Boolean] = | |
future.flatMap(value => if (!value) Future.successful(false) else other) | |
def ||(other: => Future[Boolean]): Future[Boolean] = | |
future.flatMap(value => if (value) Future.successful(true) else other) | |
} |
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 condition1: Boolean = ... | |
def condition2: Boolean = ... | |
def condition3: Boolean = ... | |
if (condition1 && (condition2 || condition3)) ... |
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 condition: Future[Boolean] = ... | |
def action1(): Future[Unit] = ... | |
def action2(): Future[Unit] = ... | |
condition.flatMap( | |
if (_) | |
action1() | |
else | |
action2() |
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 condition: Boolean = ... | |
def action1(): Unit = ... | |
def action2(): Unit = ... | |
if (condition) { | |
action1() | |
} else { | |
action2() | |
} |
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 regularRpcEndpoint(request: Request): Future[Response] = { | |
validateRequest(request) | |
isPermittedViaRpc.flatMap { isPermitted => | |
if (!isPermitted) { | |
Future.failed(new PermissionDeniedException("...")) | |
} else { | |
retrieveFromDatabase(request.id) | |
.map(convertPayloadFromDb) | |
.map(Response) |
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 validateRequest(request: Request): Unit = { | |
require(request != null) | |
} | |
def regularRpcEndpoint(request: Request): Response = { | |
validateRequest(request) | |
if (!isPermittedViaRpc) { | |
throw new PermissionDeniedException("...") | |
} |
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
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import java.util.ArrayList; | |
import java.util.concurrent.*; | |
import java.util.concurrent.atomic.AtomicInteger; | |
@State(Scope.Benchmark) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) |
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
import scala.language.implicitConversions | |
import scala.util.Try | |
package object business { | |
implicit final class ModelOrResultFromOption[M](private val opt: Option[M]) | |
extends AnyVal { | |
// converts Option to Either.RightProjection | |
def orResult[R](result: => R) = | |
opt.fold[Either[R, M]](Left(result))(Right(_)).right |
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 processRequestNew(userId: UUID, requestId: UUID): BusinessResult = { | |
for { | |
user <- getUser(userId) orResult BusinessResult.UserNotFound | |
request <- getRequestById(requestId) orResult BusinessResult.RequestNotFound | |
_ <- checkAccess(request, user) orResult BusinessResult.NotOwner | |
} yield BusinessResult.Ok | |
} |
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) | |
} |