Skip to content

Instantly share code, notes, and snippets.

@adamw
Created October 21, 2020 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamw/b1425b5b8ef053a2b73943297ea02b2c to your computer and use it in GitHub Desktop.
Save adamw/b1425b5b8ef053a2b73943297ea02b2c to your computer and use it in GitHub Desktop.
package sttp.tapir.server.http4s.ztapir
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import sttp.monad.MonadError
import zio.{Has, RIO, Runtime, ZIO}
import zio.interop.catz._
import zio.interop.catz.implicits._
import sttp.tapir._
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.ztapir.ZServerEndpoint
class ZEndpointTest extends AnyFlatSpec with Matchers {
it should "compile" in {
trait Component1
trait Component2
type Service1 = Has[Component1]
type Service2 = Has[Component2]
val serverEndpoint1: ZServerEndpoint[Service1, Unit, Unit, Unit] =
endpoint.serverLogic(_ => ZIO.succeed(Right(())): ZIO[Service1, Nothing, Either[Unit, Unit]])
val serverEndpoint2: ZServerEndpoint[Service2, Unit, Unit, Unit] =
endpoint.serverLogic(_ => ZIO.succeed(Right(())): ZIO[Service2, Nothing, Either[Unit, Unit]])
implicit val runtime: Runtime[Service1 with Service2] = ???
implicit class RichServiceEndpoint[R, I, E, O](zse: ZServerEndpoint[R, I, E, O]) {
def widen[R2 <: R]: ZServerEndpoint[R2, I, E, O] =
ServerEndpoint(zse.endpoint, (monad: MonadError[RIO[R2, *]]) => i => zse.logic(monad.asInstanceOf[MonadError[RIO[R, *]]])(i))
}
type Env = Service1 with Service2
List(serverEndpoint1.widen[Env], serverEndpoint2.widen[Env]).toRoutes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment