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
6 changes: 6 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZLayerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ object ZLayerSpec extends ZIOBaseSpec {
val layer4 = ZLayer.fromAcquireRelease(sleep)(_ => sleep)
val env = layer1 ++ ((layer2 ++ layer3) >+> layer4)
assertM(ZIO.unit.provideCustomLayer(env).run)(fails(equalTo("foo")))
},
testM("project") {
final case class Person(name: String, age: Int)
val personLayer = ZLayer.succeed(Person("User", 42))
val ageLayer = personLayer.project(_.age)
assertM(ZIO.service[Int].provideLayer(ageLayer))(equalTo(42))
}
)
}
10 changes: 10 additions & 0 deletions core/shared/src/main/scala/zio/ZLayer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,16 @@ object ZLayer {
ZLayer.identity[RIn] ++ self
}

implicit final class ZLayerProjectOps[R, E, A](private val self: ZLayer[R, E, Has[A]]) extends AnyVal {

/**
* Projects out part of one of the layers output by this layer using the
* specified function
*/
final def project[B: Tag](f: A => B)(implicit tag: Tag[A]): ZLayer[R, E, Has[B]] =
self >>> ZLayer.fromFunction(r => f(r.get))
}

/**
* A `MemoMap` memoizes dependencies.
*/
Expand Down