aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/execution.scala
blob: 0cf92fdeb30e21c5bc399e4d1a103f38cde8458a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package xyz.driver.core

import scala.concurrent.{ExecutionContext, Future}
import scalaz.OptionT

object execution {

  implicit class FutureOptionTExtensions[T](future: Future[T])(implicit executionContext: ExecutionContext) {

    def toOptionT: OptionT[Future, T] =
      OptionT.optionT[Future](future.map(value => Option(value)))

    def returnUnit: Future[Unit] =
      future.map(_ => Option(()))

    def returnUnitOpt: OptionT[Future, Unit] =
      OptionT.optionT[Future](future.map(_ => Option(())))

    def andEffect[E](effect: Future[E]): Future[T] =
      for {
        result <- future
        _      <- effect
      } yield result

    def andEffect[E](effect: OptionT[Future, E]): Future[T] =
      andEffect(effect.run)
  }

  def illegalState[T](message: String): OptionT[Future, T] =
    failure[T](new IllegalStateException(message))

  def illegalArgument[T](message: String): OptionT[Future, T] =
    failure[T](new IllegalArgumentException(message))

  def failure[T](throwable: Throwable): OptionT[Future, T] =
    OptionT.optionT(Future.failed[Option[T]](throwable))

  def collectOrNone[T, R](value: T)(f: PartialFunction[T, OptionT[Future, R]]): OptionT[Future, R] =
    f.lift(value).getOrElse(OptionT.optionT(Future.successful(Option.empty[R])))

  def collectOrDoNothing[T](value: T)(f: PartialFunction[T, OptionT[Future, Unit]]): OptionT[Future, Unit] =
    f.lift(value).getOrElse(doNothing)

  val doNothing = OptionT.optionT(Future.successful(Option(())))
}