Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Intersection Types in Scala 2 #3126

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
@@ -39,10 +39,13 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
def @@[Env0](aspect: HandlerAspect[Env0, Unit]): Routes[Env with Env0, Err] =
aspect(self)

def @@[Env0, Ctx <: Env](
aspect: HandlerAspect[Env0, Ctx],
)(implicit tag: Tag[Ctx]): Routes[Env0, Err] =
self.transform(_ @@ aspect)
def @@[Env0, Ctx <: Env](aspect: HandlerAspect[Env0, Ctx])(implicit tag: Tag[Ctx]): Routes[Env0, Err] = {
if (isScala2 && isIntersectionType[Ctx]) {
self.transform(_ @@ aspect).asInstanceOf[Routes[Env0, Err]]
} else {
self.transform(_ @@ aspect)
}
}

def @@[Env0]: ApplyContextAspect[Env, Err, Env0] =
new ApplyContextAspect[Env, Err, Env0](self)
@@ -104,6 +107,15 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
def handleErrorCauseZIO(f: Cause[Err] => ZIO[Any, Nothing, Response])(implicit trace: Trace): Routes[Env, Nothing] =
new Routes(routes.map(_.handleErrorCauseZIO(f)))

def isScala2: Boolean = util.Properties.versionNumberString.startsWith("2.")

def isIntersectionType[T](implicit tag: Tag[T]): Boolean = {
tag.tag match {
case t if t.toString.contains("with") => true
case _ => false
}
}

/**
* Allows the transformation of the Err type through an Effectful program
* allowing one to build up Routes in Stages delegates to the Route.
Loading