Construct ZLayers automagically, with friendly compile-time hints!
// build.sbt
libraryDependencies += "io.github.kitlangton" %% "zio-magic" % "0.1.8"
// Given a dependency graph (Pie needs Berries & Flour, which in turn need Spoon)*
//
// Pie
// / \
// Berries Flour
// | |
// Spoon Spoon
//
// *Not an actual recipe.
def run(args: List[String]): URIO[ZEnv, ExitCode] = {
// An effect requiring Pie and Console. Yum!
val program: URIO[Console with Pie, Unit] =
Pie.isDelicious.flatMap { bool => console.putStrLn(s"Pie is delicious: $bool") }
// The old way
val manually: ULayer[Pie with Console] =
((Spoon.live >>> Flour.live) ++ (Spoon.live >>> Berries.live)) >>> Pie.live ++ Console.live
// The magical way (The order doesn't matter)
val magically: UIO[Unit] =
program.provideMagicLayer(
Pie.live,
Flour.live,
Berries.live,
Spoon.live,
Console.live
)
magically.exitCode
}
And if you leave something off, a compile time clue!
val magically: UIO[Unit] =
program.provideMagicLayer(
Pie.live,
//Flour.live, <-- Oops
Berries.live,
Spoon.live,
Console.live
)
πͺ ZLayer Magic Missing Components
πͺ
πͺ provide zio.magic.Example.Flour.Service
πͺ for Pie.live
Versus leaving out a dependency when manually constructing your layer...
val manually: ULayer[Pie with Console] =
(Flour.live ++ (Spoon.live >>> Berries.live)) >>> Pie.live ++ Console.live
// ^ A Spoon is missing here!
type mismatch;
found : zio.ZLayer[zio.magic.Example.Spoon.Spoon with Any,Nothing,zio.magic.Example.Pie.Pie with zio.console.Console]
(which expands to) zio.ZLayer[zio.Has[zio.magic.Example.Spoon.Service] with Any,Nothing,zio.Has[zio.magic.Example.Pie.Service] with zio.Has[zio.console.Console.Service]]
required: zio.ULayer[zio.magic.Example.Pie.Pie with zio.console.Console]
(which expands to) zio.ZLayer[Any,Nothing,zio.Has[zio.magic.Example.Pie.Service] with zio.Has[zio.console.Console.Service]]
((Flour.live) ++ (Spoon.live >>> Berries.live)) >>> Pie.live ++ Console.live
You can also directly construct a ZLayer (However you must annotate the call to ZLayer.fromMagic[LikeThis]
, because macros).
val layer = Zlayer.fromMagic[Flour with Console](Console.live, Flour.live, Spoon.live)
To construct URLayer[In, Out]
use Zlayer.fromSomeMagic[In, Out]
this way:
val layer = Zlayer.fromSomeMagic[CommonEnv, Flour with Console](Console.live, Flour.live, Spoon.live)
Alternatively you can provide environment partially with provideSomeMagicLayer[Rest](l1, l2, l3)
- similarly to .provideSomeLayer
.
There's also .provideCustomMagicLayer
for which behaves similarly to .provideCustomLayer
, only it also provides ZEnv.any
to all transitive dependencies.
val program: URIO[Console with Car, Unit] = ???
val carLayer: URLayer[Blocking with Wheels, Car] = ???
val wheelLayer: ULayer[Wheels] = ???
// The ZEnv you use later will provide both Blocking to carLayer and Console to the program
val provided: URIO[ZEnv, Unit] =
program.provideCustomMagicLayer(carLayer, wheelLayer)
provideMagicLayer
, provideCustomMagicLayer
, provideSomeMagicLayer
, provideMagicLayerShared
, provideCustomMagicLayerShared
and provideSomeMagicLayerShared
all work for zio-test's Spec
.
Try ZLayer.fromMagicDebug[Pie]
or ZLayer.fromSomeMagicDebug[Blocking with Console, Pie]
to print out a pretty graph! Ooh la la!
Your Delicately Rendered Graph
Pie.live
βββββββββ΄βββββββββββββββ
Flour.live Berries.live
ββββββββ΄ββββββββ β
Spoon.live Console.live Spoon.live
β β
Blocking.live Blocking.live
Let me know if you can think of any helpful variants, and I'll give 'em a whirl!