Open djx314 opened 2 years ago
And I find that macwire perhaps can work fine with cats-effect-cps
Here's the new version with inject part(also use other way to implement, thanks for the powerful expressive ability in Scala3)
object MainAppInjected:
object module1 extends InjectedModule1
import module1.{env as env1, Env as Env1}
val appRoutes: Resource[IO, AppRoutes] = async[Resource[IO, *]] {
given ZEnvironment[Env1] = env1.await
given FileService = new FileServiceImpl
given FileFinder = new FileFinderImpl
new AppRoutesImpl
}
end MainAppInjected
trait InjectedModule1:
type Env = DesuConfig & AppConfig & Transactor[IO]
val env: Resource[IO, ZEnvironment[Env]] = async[Resource[IO, *]] {
val configModel = new DesuConfigModelImpl
given DesuConfig = Resource.eval(configModel.configIO).await
given AppConfig = new AppConfigImpl
val doobieDB = new DoobieDBImpl
given Transactor[IO] = doobieDB.transactor.await
ZEnvironment(implicitly[DesuConfig], implicitly[Transactor[IO]], implicitly[AppConfig])
}
end InjectedModule1
given [ModelTag: Tag, S <: ModelTag](using ZEnvironment[S]): ModelTag = summon[ZEnvironment[S]].get
And the cps lib seems that can work with zio and scala future.
@mbore finally I use macwire in a simple way. No magic, compat in Scala 2 and Scala 3. If someone use zio, just ZIO.identity
.
https://scastie.scala-lang.org/djx314/r5hrGExzTKuUT1bRBGx6dg/64
case class DesuConfig(foo: Long, bar: String, mysqlDesuQuillDB: DataSource)
case class DataSource(dataSource: String)
class DesuConfigModel:
val configIO: IO[DesuConfig] = ???
end DesuConfigModel
class AppConfig(config: DesuConfig):
end AppConfig
class DoobieDB(config: DesuConfig):
private val dsConfigIO = IO(config.mysqlDesuQuillDB.dataSource)
val transactor: Resource[IO, HikariTransactor[IO]] = ???
end DoobieDB
class FileFinder(appConfig: AppConfig, xa: Transactor[IO]):
// code
end FileFinder
class AppRoutes(fileFinder: FileFinder, appConfig: AppConfig):
// code
end AppRoutes
object MainAppInjected:
val envResource1: Resource[IO, AppRoutes] =
Resource.eval(wire[DesuConfigModel].configIO).flatMap {
(desuConfig: DesuConfig) =>
Resource.pure(wire[AppConfig]).flatMap { (appConf: AppConfig) =>
Resource.pure(wire[DoobieDB]).flatMap { (doobieDB: DoobieDB) =>
doobieDB.transactor.flatMap { (transactor: Transactor[IO]) =>
Resource.pure(wire[FileFinder]).map { (fileFinder: FileFinder) =>
wire[AppRoutes]
}
}
}
}
}
val envResource2: Resource[IO, AppRoutes] = for
desuConfig: DesuConfig <- Resource.eval(wire[DesuConfigModel].configIO)
appConf: AppConfig <- Resource.pure(wire[AppConfig])
doobieDB: DoobieDB <- Resource.pure(wire[DoobieDB])
transactor: Transactor[IO] <- doobieDB.transactor
fileFinder: FileFinder <- Resource.pure(wire[FileFinder])
yield wire[AppRoutes]
end MainAppInjected
The problem that in macwire
is, we can't use these code.
// === Error 1
for {
objectA <- Resource.pure(wire[ClassA]) // Right
objectB = wire[ClassB] // Wrong, objectB can not be wired.
} yield wire[ClassC]
// === workaround 1
for {
objectA <- Resource.pure(wire[ClassA])
objectB <- Rerource.pure(wire[ClassB])
} yield wire[ClassC]
// === Error 2
Resource.pure(wire[ClassA]).flatMap { (objectA: ClassA) => // Right
val objectD: ClassD = wire[ClassD] // Wrong, objectD can not be wired.
Resource.pure(wire[ClassB]).map { (objectB: ClassB) => // Right
wire[ClassC]
}
}
// workaround 2
Resource.pure(wire[ClassA]).flatMap { (objectA: ClassA) =>
Resource.pure(wire[ClassD]).flatMap { (objectD: ClassD) =>
Resource.pure(wire[ClassB]).map { (objectB: ClassB) =>
wire[ClassC]
}
}
}
As the code show in gitter
Macwire can support wire in F[_] like distage. Since implicit value and constructor value is fetched by type. You can declare a implicit value like
Here request a feature that support sum type in macwire like zio.ZEnvironment. Then we can support something like distage's Module include(simple version) in Scala2 and Scala3.(distage doc)
Here is the code also in gitter
It circuitously performs the macwire function I imagined use Scala3 and zio.ZEnvironment.