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

Fix Scala 3 HasConstructor: support more than 2 dependencies #1840

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,30 @@ object HasConstructorMacro {
}

val lamExpr = util.wrapIntoFunctoidRawLambda[R](lamParams) {
case (_, allParams) =>
allParams.zip(lamParams) match {
case (lambdaSym, allParams) =>
(allParams.zip(lamParams) match {
case (headParam, ParamRepr(_, _, TypeReprAsType('[t]))) :: params =>
def addAccum[A <: zio.Has[?], B](exprAcc: Typed, arg: Term)(using Type[B]): Typed = {
val Typed(term, exprTpe) = exprAcc
given Type[A] = exprTpe.tpe.asType.asInstanceOf[Type[A]]

val addExpr = '{ zio.Has.HasSyntax[A](${ term.asExprOf[A] }).add[B](${ arg.asExprOf[B] })(summonInline[Tag[B]]) }
Typed(addExpr.asTerm, TypeTree.of[A & zio.Has[B]])
}

params
.foldLeft('{ zio.Has.apply[t](${ headParam.asExprOf[t] })(summonInline[Tag[t]]) }) {
.foldLeft(
Typed('{ zio.Has.apply[t](${ headParam.asExprOf[t] })(summonInline[Tag[t]]) }.asTerm, TypeTree.of[zio.Has[t]])
) {
case (expr, (arg, ParamRepr(_, _, tpe))) =>
tpe.asType match {
case '[g] =>
'{ $expr.add(${ arg.asExprOf[g] })(summonInline[Tag[g]]) }
case '[b] =>
addAccum[zio.Has[?], b](expr, arg)
}
}.asTerm
}
case _ =>
report.errorAndAbort(s"Impossible happened, empty Has intersection or malformed type ${typeRepr.show} in HasConstructorMacro")
}
}).changeOwner(lambdaSym)
}

val f = util.makeFunctoid[R](lamParams, lamExpr, '{ ProviderType.ZIOHas })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ object FunctoidMacro {
val strExpr = Expr(str)
'{ new DIKey.IdKey($safeTpe, $strExpr, None)(scala.compiletime.summonInline[IdContract[String]]) }
case None =>
'{ new DIKey.TypeKey($safeTpe) }
'{ new DIKey.TypeKey($safeTpe, None) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,28 @@ class ZIOHasInjectionTest extends AnyWordSpec with MkInjector {
assert(context.get[TestTrait].anyValDep.d eq context.get[Dep])
}

"Scala 3 regression test: support more than 2 dependencies in HasConstructor" in {
trait OpenTracingService

type OpenTracing = Has[OpenTracingService]

trait SttpBackend[F[_], +P]

trait MyEndpoints[F[_, _]]

trait ZioStreams
trait WebSockets

trait MyPublisher
trait MyClient

object MyPlugin extends ModuleDef {
make[MyClient].fromHas[OpenTracing with Has[MyPublisher] with Has[SttpBackend[Task, ZioStreams with WebSockets]] with Has[MyEndpoints[IO]], Nothing, MyClient] {
??? : zio.ZIO[OpenTracing with Has[MyPublisher] with Has[SttpBackend[Task, ZioStreams with WebSockets]] with Has[MyEndpoints[IO]], Nothing, MyClient]
}
}
}

}

}