diff options
author | Shadaj Laddad <shadaj@users.noreply.github.com> | 2017-11-21 10:23:09 -0800 |
---|---|---|
committer | Shadaj Laddad <shadaj@users.noreply.github.com> | 2017-11-21 10:23:09 -0800 |
commit | ebeca9258d5e629327abbcf67e0066a4cdeac173 (patch) | |
tree | 87ded4ccaf2a05d5fc3ccfa8e795104812647520 /core/shared | |
parent | 5052e8c1a1a0c272c0406165e04bef3c89788d59 (diff) | |
download | magnolia-ebeca9258d5e629327abbcf67e0066a4cdeac173.tar.gz magnolia-ebeca9258d5e629327abbcf67e0066a4cdeac173.tar.bz2 magnolia-ebeca9258d5e629327abbcf67e0066a4cdeac173.zip |
Add unit tests and fix behavior with default parameters
Diffstat (limited to 'core/shared')
-rw-r--r-- | core/shared/src/main/scala/magnolia.scala | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/core/shared/src/main/scala/magnolia.scala b/core/shared/src/main/scala/magnolia.scala index 49b2744..2431e4f 100644 --- a/core/shared/src/main/scala/magnolia.scala +++ b/core/shared/src/main/scala/magnolia.scala @@ -278,16 +278,26 @@ object Magnolia { val preAssignments = caseParams.map(_.typeclass) val defaults = if (!isValueClass) { - val indexedConstructorParams = genericType.decls.collect { - case m: MethodSymbol if m.isCaseAccessor => m.asTerm - }.zipWithIndex - - indexedConstructorParams.map { - case (p, idx) => - if (p.isParamWithDefault) { - val method = TermName("apply$default$" + (idx + 1)) - q"$scalaPkg.Some(${genericType.typeSymbol.companion.asTerm}.$method)" - } else q"$scalaPkg.None" + val constructorParams = genericType.decls.collect { + case a: MethodSymbol if a.isConstructor => a + }.head.paramLists.head.map(_.asTerm) + val noDefaults = constructorParams.forall(!_.isParamWithDefault) + + if (noDefaults) { + constructorParams.map(_ => q"$scalaPkg.None") + } else { + val caseClassCompanion = genericType.companion + val constructorMethod = caseClassCompanion.decl(TermName("apply")).asMethod + val indexedConstructorParams = + constructorMethod.paramLists.head.map(_.asTerm).zipWithIndex + + indexedConstructorParams.map { + case (p, idx) => + if (p.isParamWithDefault) { + val method = TermName("apply$default$" + (idx + 1)) + q"$scalaPkg.Some(${genericType.typeSymbol.companion.asTerm}.$method)" + } else q"$scalaPkg.None" + } } } else List(q"$scalaPkg.None") |