Skip to content

Commit

Permalink
Fix Scala 2 implementation after refactor and let it handle multiple …
Browse files Browse the repository at this point in the history
…parameter lists
  • Loading branch information
MateuszKubuszok committed Dec 16, 2023
1 parent df67bb0 commit 35a6faa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ trait ProductTypesPlatform extends ProductTypes { this: DefinitionsPlatform =>
}

import methodType.Underlying as MethodType
println(methodType)
val tree = expr.asInstanceOfExpr[MethodType].tree
c.Expr[A](q"$tree(...${(args.map(_.map { case (paramName, _) =>
constructorArguments(paramName).value.tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,43 @@ private[chimney] trait DslMacroUtils {
}

private trait ExistentialCtor {
type Underlying <: runtime.Path
implicit val Underlying: Type[Underlying]
type Underlying <: runtime.ArgumentLists
implicit val Underlying: WeakTypeTag[Underlying]
}
private object ExistentialCtor {

def parse(t: Tree): Either[String, ExistentialCtor] = t match {
// TODO: add support for multiple parameter lists
case Function(params, _) =>
val t = paramsToType(List(params))
def parse(t: Tree): Either[String, ExistentialCtor] = {
def extractParams(t: Tree): Either[String, List[List[ValDef]]] = t match {
// TODO: add support for multiple parameter lists
case Function(params, tail) =>
extractParams(tail) match {
case Left(_) => Right(List(params))
case Right(tail) => Right(params :: tail)
}
// TODO: remove once everything works
case _ =>
Left(invalidConstructor(t))
}

extractParams(t).map { params =>
val tpe = paramsToType(params)
println(s"""Expression:
|${Console.MAGENTA}${show(f)}${Console.RESET}
|${Console.MAGENTA}${show(t)}${Console.RESET}
|defined as:

Check warning on line 104 in chimney/src/main/scala-2/io/scalaland/chimney/internal/compiletime/dsl/utils/DslMacroUtils.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-2/io/scalaland/chimney/internal/compiletime/dsl/utils/DslMacroUtils.scala#L103-L104

Added lines #L103 - L104 were not covered by tests
|${Console.MAGENTA}${showRaw(f)}${Console.RESET}
|${Console.MAGENTA}${showRaw(t)}${Console.RESET}
|of type:
|${Console.MAGENTA}${f.tpe}${Console.RESET}
|${Console.MAGENTA}${t.tpe}${Console.RESET}
|of type:
|${Console.MAGENTA}${showRaw(f.tpe)}${Console.RESET}
|${Console.MAGENTA}${showRaw(t.tpe)}${Console.RESET}
|resolved as:
|${Console.MAGENTA}$t${Console.RESET}
|${Console.MAGENTA}$tpe${Console.RESET}
|""".stripMargin)
Right(new ExistentialCtor {

new ExistentialCtor {
type Underlying = runtime.ArgumentLists
val Underlying: WeakTypeTag[runtime.ArgumentLists] = t
})
case _ =>
Left(invalidConstructor(f))
implicit val Underlying: WeakTypeTag[runtime.ArgumentLists] = tpe
}
}
}

private def paramsToType(paramsLists: List[List[ValDef]]): WeakTypeTag[runtime.ArgumentLists] =
Expand All @@ -118,6 +128,7 @@ private[chimney] trait DslMacroUtils {
.foldRight[WeakTypeTag[? <: runtime.ArgumentLists]](weakTypeTag[runtime.ArgumentLists.Empty])(
constructArgumentListsType
)
.asInstanceOf[WeakTypeTag[runtime.ArgumentLists]]

private def constructArgumentListsType(
head: WeakTypeTag[? <: runtime.ArgumentList],
Expand Down Expand Up @@ -235,7 +246,7 @@ private[chimney] trait DslMacroUtils {

final def applyFromBody(f: Tree): Tree = {
val ctorType = extractCtorType(f)
ctorType.Underlying
apply(ctorType.Underlying)
}

private def extractCtorType(f: Tree): ExistentialCtor = ExistentialCtor.parse(f) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object IsFunction extends IsFunctionLowPriorityImplicits {
ev: Of[Mid, Out]
): Of[(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V) => Mid, Out] = cast(ev)

Check warning on line 75 in chimney/src/main/scala/io/scalaland/chimney/internal/runtime/IsFunction.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala/io/scalaland/chimney/internal/runtime/IsFunction.scala#L74-L75

Added lines #L74 - L75 were not covered by tests
}
private trait IsFunctionLowPriorityImplicits { this: IsFunction.type =>
private[runtime] trait IsFunctionLowPriorityImplicits { this: IsFunction.type =>

private val impl = new IsFunction[Any] {}
private def cast[Fn, Out]: Of[Fn, Out] = impl.asInstanceOf[Of[Fn, Out]]

Check warning on line 80 in chimney/src/main/scala/io/scalaland/chimney/internal/runtime/IsFunction.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala/io/scalaland/chimney/internal/runtime/IsFunction.scala#L80

Added line #L80 was not covered by tests
Expand Down

0 comments on commit 35a6faa

Please sign in to comment.