From 5312aa433cb0d5767c61f94c16d84b4c0f60f17b Mon Sep 17 00:00:00 2001 From: Mathias Date: Thu, 4 Jan 2018 17:45:02 +0100 Subject: Change `typeName: String` to `typeName: magnolia.TypeName` [BREAKING!] --- core/shared/src/main/scala/interface.scala | 11 +++++++++-- core/shared/src/main/scala/magnolia.scala | 21 +++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) (limited to 'core/shared/src/main/scala') diff --git a/core/shared/src/main/scala/interface.scala b/core/shared/src/main/scala/interface.scala index b06f350..f0f104c 100644 --- a/core/shared/src/main/scala/interface.scala +++ b/core/shared/src/main/scala/interface.scala @@ -104,7 +104,7 @@ trait Param[Typeclass[_], Type] { * @tparam Typeclass type constructor for the typeclass being derived * @tparam Type generic type of this parameter */ abstract class CaseClass[Typeclass[_], Type] private[magnolia] ( - val typeName: String, + val typeName: TypeName, val isObject: Boolean, val isValueClass: Boolean, parametersArray: Array[Param[Typeclass, Type]] @@ -157,7 +157,7 @@ abstract class CaseClass[Typeclass[_], Type] private[magnolia] ( * @param subtypesArray an array of [[Subtype]] instances for each subtype in the sealed trait * @tparam Typeclass type constructor for the typeclass being derived * @tparam Type generic type of this parameter */ -final class SealedTrait[Typeclass[_], Type](val typeName: String, +final class SealedTrait[Typeclass[_], Type](val typeName: TypeName, subtypesArray: Array[Subtype[Typeclass, Type]]) { /** a sequence of all the subtypes of this sealed trait */ @@ -182,3 +182,10 @@ final class SealedTrait[Typeclass[_], Type](val typeName: String, rec(0) } } + +/** + * Provides the different parts of a type's class name. + */ +final case class TypeName(ownerName: String, short: String) { + def full: String = s"$ownerName.$short" +} \ No newline at end of file diff --git a/core/shared/src/main/scala/magnolia.scala b/core/shared/src/main/scala/magnolia.scala index 200d38e..b4179f9 100644 --- a/core/shared/src/main/scala/magnolia.scala +++ b/core/shared/src/main/scala/magnolia.scala @@ -218,13 +218,18 @@ object Magnolia { val resultType = appliedType(typeConstructor, genericType) - val className = s"${genericType.typeSymbol.owner.fullName}.${genericType.typeSymbol.name.decodedName}" + val typeName = TermName(c.freshName("typeName")) + val typeNameDef = { + val ts = genericType.typeSymbol + q"val $typeName = $magnoliaPkg.TypeName(${ts.owner.fullName}, ${ts.name.decodedName.toString})" + } val result = if (isCaseObject) { val obj = GlobalUtil.patchedCompanionRef(c)(genericType) val impl = q""" + $typeNameDef ${c.prefix}.combine($magnoliaPkg.Magnolia.caseClass[$typeConstructor, $genericType]( - $className, true, false, new $scalaPkg.Array(0), _ => $obj) + $typeName, true, false, new $scalaPkg.Array(0), _ => $obj) ) """ Some(Typeclass(genericType, impl)) @@ -318,15 +323,17 @@ object Magnolia { val $paramsVal: $scalaPkg.Array[$magnoliaPkg.Param[$typeConstructor, $genericType]] = new $scalaPkg.Array(${assignments.length}) ..$assignments + + $typeNameDef ${c.prefix}.combine($magnoliaPkg.Magnolia.caseClass[$typeConstructor, $genericType]( - $className, + $typeName, false, $isValueClass, $paramsVal, ($fieldValues: $scalaPkg.Seq[Any]) => { if ($fieldValues.lengthCompare($paramsVal.length) != 0) { - val msg = "`" + $className + "` has " + $paramsVal.length + " fields, not " + $fieldValues.size + val msg = "`" + $typeName.full + "` has " + $paramsVal.length + " fields, not " + $fieldValues.size throw new java.lang.IllegalArgumentException(msg) } new $genericType(..${ @@ -385,9 +392,11 @@ object Magnolia { new $scalaPkg.Array(${assignments.size}) ..$assignments + + $typeNameDef ${c.prefix}.dispatch(new $magnoliaPkg.SealedTrait( - $className, + $typeName, $subtypesVal: $scalaPkg.Array[$magnoliaPkg.Subtype[$typeConstructor, $genericType]]) ): $resultType }""" @@ -482,7 +491,7 @@ object Magnolia { * * This method is intended to be called only from code generated by the Magnolia macro, and * should not be called directly from users' code. */ - def caseClass[Tc[_], T](name: String, + def caseClass[Tc[_], T](name: TypeName, obj: Boolean, valClass: Boolean, params: Array[Param[Tc, T]], -- cgit v1.2.3