-1

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 >>>.

Aleksey N Yakushev
  • 443
  • 1
  • 3
  • 11

2 Answers2

1

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
  • 18,395
  • 2
  • 51
  • 55
0

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.

Aleksey N Yakushev
  • 443
  • 1
  • 3
  • 11
  • @Alekey N Yakushev good point, I should have unwrapped the `Has` with an `access`. I updated my answer just so there isn't any confusion to future readers. – paulpdaniels Apr 24 '20 at 03:02