Skip to content

Commit

Permalink
Implement noneOrFail zio#4841
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Daviot committed Mar 29, 2021
1 parent 31234be commit 2c2eedd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions .bsp/sbt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"sbt","version":"1.4.9","bspVersion":"2.0.0-M5","languages":["scala"],"argv":["/Users/michel/.sdkman/candidates/java/11.0.10.hs-adpt/bin/java","-Xms100m","-Xmx100m","-classpath","/Users/michel/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/IntelliJ IDEA CE.app.plugins/Scala/launcher/sbt-launch.jar","xsbt.boot.Boot","-bsp"]}
25 changes: 24 additions & 1 deletion core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,24 @@ object ZIOSpec extends ZIOBaseSpec {
assertM(ZIO.succeed(false).negate)(equalTo(true))
}
),
suite("noneOrFail")(
testM("on None succeeds with Unit") {
val option: Option[String] = None
val adaptError: String => String = identity
for {
value <- ZIO.noneOrFail(option, adaptError)
} yield {
assert(value)(equalTo(()))
}
},
testM("on Some fails") {
for {
value <- ZIO.noneOrFail(Some("value"), (v: String) => v + v).catchAll(e => ZIO.succeed(e))
} yield {
assert(value)(equalTo("valuevalue"))
}
} @@ zioTag(errors)
),
suite("once")(
testM("returns an effect that will only be executed once") {
for {
Expand Down Expand Up @@ -2080,7 +2098,12 @@ object ZIOSpec extends ZIOBaseSpec {
testM("fail ensuring") {
var finalized = false

val io = Task.fail(ExampleError).ensuring(IO.effectTotal { finalized = true; () })
val io = Task
.fail(ExampleError)
.ensuring(IO.effectTotal {
finalized = true;
()
})

for {
a1 <- assertM(io.run)(fails(equalTo(ExampleError)))
Expand Down
6 changes: 6 additions & 0 deletions core/shared/src/main/scala/zio/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,12 @@ object IO {
*/
val none: UIO[Option[Nothing]] = ZIO.none

/**
* @see See [[zio.ZIO.noneOrFail()]]
*/
def noneOrFail[E, O](o: Option[O], f: O => E): IO[E, Unit] =
ZIO.noneOrFail(o, f)

/**
* @see See [[zio.ZIO.not]]
*/
Expand Down
8 changes: 8 additions & 0 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,14 @@ object ZIO extends ZIOCompanionPlatformSpecific {
*/
val none: UIO[Option[Nothing]] = succeedNow(None)

/**
* Lifts an Option into a IO.
* If the option is empty it succeeds with Unit.
* If the option is defined it fails with an error adapted with f.
*/
def noneOrFail[E, O](o: Option[O], f: O => E): IO[E, Unit] =
getOrFailUnit(o).flip.mapError(f)

/**
* Feeds elements of type `A` to a function `f` that returns an effect.
* Collects all successes and failures in a tupled fashion.
Expand Down

0 comments on commit 2c2eedd

Please sign in to comment.