从ZIO(Scala)管理类型转换
我需要帮助将ZIO[WsConfig,Throwable,A]类型的值转换为ZManaged[A] 有下一个代码(我可以在IDEA中构建它,没有与类型相关的错误,没有人),但我有???恰到好处从ZIO(Scala)管理类型转换,scala,zio,Scala,Zio,我需要帮助将ZIO[WsConfig,Throwable,A]类型的值转换为ZManaged[A] 有下一个代码(我可以在IDEA中构建它,没有与类型相关的错误,没有人),但我有???恰到好处 def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, UcpZLayer] = { val zm: ZIO[WsConfig, Throwable, Z
def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, UcpZLayer] = {
val zm: ZIO[WsConfig, Throwable, ZManaged[Any, Throwable, UcpZLayer.Service]] =
for {
conf <- ZIO.environment[WsConfig]
cpool <- Ref.make(new OraConnectionPool(conf.dbconf, conf.ucpconf))
acquire = ZIO(new poolCache(cpool))
release: (UcpZLayer.Service => zio.ZIO[Any,Nothing,Any]) = (pc: UcpZLayer.Service) => pc.closeAll
zm: ZManaged[Any, Throwable, UcpZLayer.Service] = ZManaged.make(acquire)(release)
} yield zm
val managedConnPool: ZManaged[Any, Throwable, UcpZLayer.Service] = ???
ZLayer.fromManaged(managedConnPool)
}
我将任何Zlayer(带有confLayer)与++水平组合,并通过>>>传递到poolCache中。我建议改为这样做:
def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, UcpZLayer] =
(for {
// Use a Managed directly when access then environment
// `access` will remove the `Has` wrapping.
conf <- ZManaged.access[Config[WsConfig]](_.get)
// Convert the effect into a no-release managed
cpool <- Ref.make(new OraConnectionPool(conf.dbconf, conf.ucpconf)).toManaged_
// Create the managed
zm <- ZManaged.make(ZIO(new poolCache(cpool)))(_.closeAll)
} yield zm).toLayer // Convert a `Managed` to `ZLayer` directly
def poolCache(隐式标记:taged[UcpZLayer.Service]):ZLayer[ZenvLogConfCache\ux,Throwable,UcpZLayer]=
(用于{
//在访问环境时直接使用托管数据库
//“access”将删除“Has”包装。
confpaupdaniels
非常感谢。当我尝试直接使用您的建议时,会引发类型错误:
Required: zio.ZLayer[ZenvLogConfCache_, Throwable, zio.Has[Service]]
Found: zio.ZLayer[R with Config[WsConfig], Throwable, zio.Has[poolCache]]
我在下一个表格中重写它
def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, Has[UcpZLayer.Service]] = {
val zm: ZManaged[Config[WsConfig], Throwable, poolCache] =
for {
// Use a Managed directly when access then environment
conf <- ZManaged.access[Config[WsConfig]](_.get)
// Convert the effect into a no-release managed
cpool <- Ref.make(new OraConnectionPool(conf.dbconf, conf.ucpconf)).toManaged_
// Create the managed
zm <- ZManaged.make(ZIO(new poolCache(cpool)))(_.closeAll)
} yield zm
zm.toLayer // Convert a `Managed` to `ZLayer` directly
}
def poolCache(隐式标记:taged[UcpZLayer.Service]):ZLayer[ZenvLogConfCache\ux,Throwable,Has[UcpZLayer.Service]={
val zm:ZManaged[Config[WsConfig],可丢弃,池缓存]=
为了{
//在访问环境时直接使用托管数据库
conf也许你想要ZManaged.fromfeffect
?看看@Alekey N Yakushev的好观点,我应该用访问打开Has
。我更新了我的答案,这样未来的读者就不会感到困惑了。
def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, Has[UcpZLayer.Service]] = {
val zm: ZManaged[Config[WsConfig], Throwable, poolCache] =
for {
// Use a Managed directly when access then environment
conf <- ZManaged.access[Config[WsConfig]](_.get)
// Convert the effect into a no-release managed
cpool <- Ref.make(new OraConnectionPool(conf.dbconf, conf.ucpconf)).toManaged_
// Create the managed
zm <- ZManaged.make(ZIO(new poolCache(cpool)))(_.closeAll)
} yield zm
zm.toLayer // Convert a `Managed` to `ZLayer` directly
}