简体   繁体   中英

Type conversion getting ZManaged from ZIO (Scala)

I need help to convert value of type ZIO[WsConfig, Throwable, A] into ZManaged[A] Have next code (I can build it in IDEA, no errors related with types, no one at all), but I have??? in just on place.

    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)
    }

May be can be helpful - this method poolCache I use to produce ZLayer

object EnvContainer {
  type IncConnSrvBind = akka.stream.scaladsl.Source[IncomingConnection, Future[ServerBinding]]

  type ZEnvLog = ZEnv with Logging
  type ZEnvLogCache =  ZEnvLog with CacheManager
  type ZenvLogConfCache_ =  ZEnvLogCache with config.Config[WsConfig]
  type ZEnvConfLogCache =  ZEnvLogCache with config.Config[WsConfig] with UcpZLayer

   val envLog: ZLayer[Console with Clock, Nothing, Logging]   =
    Logging.console((_, logEntry) =>
      logEntry
    )

  val ZEnvLogLayer:  ZLayer[ZEnv, Nothing, ZEnvLog] = ZEnv.live ++ envLog

  val ZEnvLogCacheLayer: ZLayer[ZEnv, Nothing, ZEnvLogCache] =
    ZEnv.live ++ envLog ++ CacheManager.refCache

  def ZEnvConfLogCacheLayer(confFileName: String): ZLayer[ZEnv, Throwable, ZEnvConfLogCache] = {
    val confLayer = configLayer(confFileName)
    val combEnvWithoutPool = ZEnv.live ++ envLog ++ confLayer ++ CacheManager.refCache
    combEnvWithoutPool ++ (combEnvWithoutPool >>> Ucp.UcpZLayer.poolCache)
  }

}

I combine any ZLayers (with confLayer) horizontally with ++ and pass into poolCache with >>>.

I would suggest doing something like this instead:

    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

paulpdaniels

Thanks too much. When I try use your suggestion direct, it raise type error:

    Required: zio.ZLayer[ZenvLogConfCache_, Throwable, zio.Has[Service]]
    Found:    zio.ZLayer[R with Config[WsConfig], Throwable, zio.Has[poolCache]]

I rewrite it in next form

      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
      }

now I can build without any errors. Thanks again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM