Skip to content

Commit

Permalink
fix Intersection Types in scala2
Browse files Browse the repository at this point in the history
  • Loading branch information
asr2003 committed Sep 11, 2024
1 parent 1f1c776 commit 26ed2cb
Showing 1 changed file with 16 additions and 4 deletions.
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
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 26ed2cb

Please sign in to comment.