Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZIOLazinessSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("fromFiber")(assertLazy(IO.fromFiber)),
testM("fromOption")(assertLazy(IO.fromOption)),
testM("fromTry")(assertLazy(IO.fromTry)),
testM("getOrFailUnit")(assertLazy(IO.getOrFailUnit)),
testM("halt")(assertLazy(IO.halt)),
testM("interruptAs")(assertLazy(IO.interruptAs)),
testM("left")(assertLazy(IO.left)),
Expand Down Expand Up @@ -126,6 +127,7 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("fromFiber")(assertLazy(ZIO.fromFiber)),
testM("fromOption")(assertLazy(ZIO.fromOption)),
testM("fromTry")(assertLazy(ZIO.fromTry)),
testM("getOrFailUnit")(assertLazy(ZIO.getOrFailUnit)),
testM("halt")(assertLazy(ZIO.halt)),
testM("interruptAs")(assertLazy(ZIO.interruptAs)),
testM("left")(assertLazy(ZIO.left)),
Expand Down
68 changes: 66 additions & 2 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package zio

import scala.annotation.tailrec
import scala.util.{ Failure, Success }

import scala.util.{ Failure, Success, Try }
import zio.Cause._
import zio.LatchOps._
import zio.clock.Clock
Expand Down Expand Up @@ -3394,6 +3393,71 @@ object ZIOSpec extends ZIOBaseSpec {
val effect: Task[Unit] = ZIO.fail(error).unit.orDie.resurrect
assertM(effect.either)(isLeft(equalTo(error)))
}
),
suite("options")(
testM("basic option test") {
for {
value <- ZIO.getOrFailUnit(Some("foo"))
} yield {
assert(value)(equalTo("foo"))
}
},
testM("side effect unit in option test") {
for {
value <- ZIO.getOrFailUnit(None).catchAll(_ => ZIO.succeed("Controlling unit side-effect"))
} yield {
assert(value)(equalTo("Controlling unit side-effect"))
}
}
),
suite("promises")(
testM("promise test") {
val func: String => String = s => s.toUpperCase
for {
promise <- ZIO.succeed(scala.concurrent.Promise[String]())
_ <- ZIO.effect {
Try(func("hello world from future")) match {
case Success(value) => promise.success(value)
case Failure(exception) => promise.failure(exception)
}
}.fork
value <- ZIO.fromPromiseScala(promise)
} yield {
assert(value)(equalTo("HELLO WORLD FROM FUTURE"))
}
},
testM("promise supplier test") {
val func: Unit => String = _ => "hello again from future"
for {
promise <- ZIO.succeed(scala.concurrent.Promise[String]())
_ <- ZIO.effect {
Try(func(())) match {
case Success(value) => promise.success(value)
case Failure(exception) => promise.failure(exception)
}
}.fork
value <- ZIO.fromPromiseScala(promise)
} yield {
assert(value)(equalTo("hello again from future"))
}
},
testM("promise ugly path test") {
val func: String => String = s => s.toUpperCase
for {
promise <- ZIO.succeed(scala.concurrent.Promise[String]())
_ <- ZIO.effect {
Try(func(null)) match {
case Success(value) => promise.success(value)
case Failure(exception) => promise.failure(exception)
}
}.fork
value <- ZIO
.fromPromiseScala(promise)
.catchAll(_ => ZIO.succeed("Controlling side-effect of function passed to promise"))
} yield {
assert(value)(equalTo("Controlling side-effect of function passed to promise"))
}
}
)
)

Expand Down
5 changes: 5 additions & 0 deletions core/shared/src/main/scala/zio/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ object IO {
*/
def fromOption[A](v: => Option[A]): IO[Option[Nothing], A] = ZIO.fromOption(v)

/**
* @see See [[zio.ZIO.getOrFailUnit]]
*/
def getOrFailUnit[A](v: => Option[A]): IO[Unit, A] = ZIO.getOrFailUnit(v)

/**
* @see See [[zio.ZIO.fromTry]]
*/
Expand Down
14 changes: 13 additions & 1 deletion core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import scala.collection.mutable.Builder
import scala.concurrent.ExecutionContext
import scala.reflect.ClassTag
import scala.util.{ Failure, Success }

import zio.clock.Clock
import zio.duration._
import zio.internal.tracing.{ ZIOFn, ZIOFn1, ZIOFn2 }
Expand Down Expand Up @@ -3059,6 +3058,13 @@ object ZIO extends ZIOCompanionPlatformSpecific {
}
}

/**
* Imports a [[scala.concurrent.Promise]] we generate a future from promise,
* and we pass to [fromFuture] to transform into Task[A]
*/
def fromPromiseScala[A](promise: scala.concurrent.Promise[A]): Task[A] =
ZIO.fromFuture(_ => promise.future)

/**
* Imports a function that creates a [[scala.concurrent.Future]] from an
* [[scala.concurrent.ExecutionContext]] into a `ZIO`. The provided
Expand Down Expand Up @@ -3142,6 +3148,12 @@ object ZIO extends ZIOCompanionPlatformSpecific {
case Some(v) => ZIO.succeedNow(v)
})

/**
* Lifts an Option into a IO, if the option is not defined it fails with Unit.
*/
final def getOrFailUnit[A](v: => Option[A]): IO[Unit, A] =
effectTotal(v).flatMap(_.fold[IO[Unit, A]](fail(()))(succeedNow))

/**
* Returns an effect that models failure with the specified `Cause`.
*/
Expand Down