diff options
author | Leandro Bolivar <leandrob131@gmail.com> | 2017-11-25 11:29:20 -0500 |
---|---|---|
committer | Leandro Bolivar <leandrob131@gmail.com> | 2017-11-25 11:29:20 -0500 |
commit | 2de22e7d27025f839ef70916d3bfb99e69973521 (patch) | |
tree | a44b296d5f5bf5c85f69f62c4f1723fdcb9bf868 | |
parent | f336d9593acf3e9a95ce5a96b0631dd00f993197 (diff) | |
download | magnolia-2de22e7d27025f839ef70916d3bfb99e69973521.tar.gz magnolia-2de22e7d27025f839ef70916d3bfb99e69973521.tar.bz2 magnolia-2de22e7d27025f839ef70916d3bfb99e69973521.zip |
Fixed error that ocurred when the companion object of a case class has alternative apply methods
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | core/shared/src/main/scala/magnolia.scala | 10 | ||||
-rw-r--r-- | tests/src/main/scala/tests.scala | 15 |
3 files changed, 24 insertions, 2 deletions
@@ -2,3 +2,4 @@ target .jvm .js .native +.idea diff --git a/core/shared/src/main/scala/magnolia.scala b/core/shared/src/main/scala/magnolia.scala index 6400002..337edc7 100644 --- a/core/shared/src/main/scala/magnolia.scala +++ b/core/shared/src/main/scala/magnolia.scala @@ -279,9 +279,15 @@ object Magnolia { val defaults = if (!isValueClass) { val caseClassCompanion = genericType.companion - val constructorMethod = caseClassCompanion.decl(TermName("apply")).asMethod + // If a companion object is defined with alternative apply methods + // it is needed get all the alternatives + val constructorMethod = + caseClassCompanion.decl(TermName("apply")).alternatives.map(_.asMethod) + + // The last apply method in the alternatives is the one that belongs + // to the case class, not the user defined companion object val indexedConstructorParams = - constructorMethod.paramLists.head.map(_.asTerm).zipWithIndex + constructorMethod.last.paramLists.head.map(_.asTerm).zipWithIndex indexedConstructorParams.map { case (p, idx) => diff --git a/tests/src/main/scala/tests.scala b/tests/src/main/scala/tests.scala index e453502..52dc1d0 100644 --- a/tests/src/main/scala/tests.scala +++ b/tests/src/main/scala/tests.scala @@ -37,11 +37,26 @@ case object Blue extends Color case class `%%`(`/`: Int, `#`: String) +case class Param(a: String, b: String) +case class Test(param: Param) +object Test { + def apply(): Test = Test(Param("", "")) + + def apply(a: String)(implicit b: Int): Test = Test(Param(a, b.toString)) + + def apply(a: String, b: String): Test = Test(Param(a, b)) +} + object Tests extends TestApp { def tests() = for (i <- 1 to 1000) { import examples._ + test("construct a Show product instance with alternative apply functions") { + import examples._ + Show.gen[Test].show(Test("a", "b")) + }.assert(_ == """Test(param=Param(a=a,b=b))""") + test("construct a Show product instance") { import examples._ Show.gen[Person].show(Person("John Smith", 34)) |