From 7b776425828d27b3112ad5bddafaa7564c326536 Mon Sep 17 00:00:00 2001 From: Jon Pretty Date: Mon, 30 Oct 2017 19:25:07 +0100 Subject: Support for case objects --- core/src/main/scala/magnolia.scala | 26 +++++++-- examples/src/main/scala/typeclasses.scala | 3 +- tests/src/main/scala/main.scala | 88 ------------------------------ tests/src/main/scala/tests.scala | 91 +++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 93 deletions(-) delete mode 100644 tests/src/main/scala/main.scala create mode 100644 tests/src/main/scala/tests.scala diff --git a/core/src/main/scala/magnolia.scala b/core/src/main/scala/magnolia.scala index c4c8113..ae582e4 100644 --- a/core/src/main/scala/magnolia.scala +++ b/core/src/main/scala/magnolia.scala @@ -24,6 +24,7 @@ trait JoinContext[Tc[_], T] { def construct[R](param: ((Param[Tc, T]) => Any)): T def typeName: String def parameters: List[Param[Tc, T]] + def isObject: Boolean } object Magnolia { @@ -34,7 +35,7 @@ object Magnolia { def generic[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { import c.universe._ import scala.util.{Try, Success, Failure} - + def javaClassName(sym: Symbol): String = if(sym.owner.isPackage) sym.fullName else if(sym.owner.isModuleClass) s"${javaClassName(sym.owner)}$$${sym.name}" @@ -85,7 +86,6 @@ object Magnolia { genericType: Type, typeConstructor: Type, assignedName: TermName): Tree = { - val searchType = appliedType(typeConstructor, genericType) findType(genericType).map { methodName => val methodAsString = methodName.encodedName.toString @@ -123,6 +123,7 @@ object Magnolia { val typeSymbol = genericType.typeSymbol val classType = if(typeSymbol.isClass) Some(typeSymbol.asClass) else None val isCaseClass = classType.map(_.isCaseClass).getOrElse(false) + val isCaseObject = classType.map(_.isModuleClass).getOrElse(false) val isSealedTrait = classType.map(_.isSealed).getOrElse(false) val isValueClass = genericType <:< typeOf[AnyVal] @@ -130,7 +131,24 @@ object Magnolia { // FIXME: Handle AnyVals - if(isCaseClass) { + if(isCaseObject) { + val termSym = genericType.typeSymbol.companionSymbol + val obj = termSym.asTerm + val className = obj.name.toString + val impl = q""" + ${c.prefix}.join(new _root_.magnolia.JoinContext[$typeConstructor, $genericType] { + def construct[R](fn: ((Param[${typeConstructor}, $genericType]) => Any)): $genericType = $obj + def typeName: _root_.java.lang.String = $className + def parameters: _root_.scala.List[Param[$typeConstructor, $genericType]] = _root_.scala.List() + def isObject = true + }) + """ + + Some(q""" + def $assignedName: $resultType = $impl + $assignedName + """) + } else if(isCaseClass) { val caseClassParameters = genericType.decls.collect { case m: MethodSymbol if m.isCaseAccessor => m.asMethod } @@ -172,6 +190,7 @@ object Magnolia { def typeName: _root_.java.lang.String = $className def parameters: _root_.scala.List[Param[$typeConstructor, $genericType]] = _root_.scala.List(..$callables) + def isObject = false }) """ @@ -181,7 +200,6 @@ object Magnolia { """ } } else if(isSealedTrait) { - val subtypes = classType.get.knownDirectSubclasses.to[List] if(subtypes.isEmpty) { diff --git a/examples/src/main/scala/typeclasses.scala b/examples/src/main/scala/typeclasses.scala index d3017e5..ed59eda 100644 --- a/examples/src/main/scala/typeclasses.scala +++ b/examples/src/main/scala/typeclasses.scala @@ -14,7 +14,7 @@ import scala.annotation.unchecked.uncheckedVariance object Show { def join[T](context: JoinContext[Show, T])(value: T): String = context.parameters.map { param => s"${param.label}=${param.typeclass.show(param.dereference(value))}" - }.mkString(s"{", ",", "}") + }.mkString(s"${context.typeName.split("\\.").last}(", ",", ")") def split[T](subclasses: List[Subclass[Show, T]])(value: T): String = subclasses.map { sub => sub.cast.andThen { value => @@ -81,6 +81,7 @@ trait Decoder[T] { def decode(str: String): T } sealed trait Tree case class Leaf(value: String) extends Tree case class Branch(left: Tree, right: Tree) extends Tree +case object Bud extends Tree sealed trait Entity diff --git a/tests/src/main/scala/main.scala b/tests/src/main/scala/main.scala deleted file mode 100644 index 99ffe56..0000000 --- a/tests/src/main/scala/main.scala +++ /dev/null @@ -1,88 +0,0 @@ -package magnolia.tests - -import magnolia._ -import estrapade._ -import contextual.data.scalac._ -import contextual.data.fqt._ -import contextual.data.txt._ - -import scala.util._ - -object Tests extends TestApp { - - def tests() = { - import examples._ - - test("construct a Show product instance") { - import examples._ - Show.generic[Person].show(Person("John Smith", 34)) - }.assert(_ == """{name=John Smith,age=34}""") - - test("construct a Show coproduct instance") { - import examples._ - Show.generic[Person].show(Person("John Smith", 34)) - }.assert(_ == "{name=John Smith,age=34}") - - test("serialize a Branch") { - import magnolia.examples._ - implicitly[Show[Branch]].show(Branch(Leaf("LHS"), Leaf("RHS"))) - }.assert(_ == "{left={value=LHS},right={value=RHS}}") - - test("test equality false") { - import examples._ - Eq.generic[Entity].equal(Person("John Smith", 34), Person("", 0)) - }.assert(_ == false) - - test("test equality true") { - import examples._ - Eq.generic[Entity].equal(Person("John Smith", 34), Person("John Smith", 34)) - }.assert(_ == true) - - test("test branch equality true") { - import examples._ - Eq.generic[Tree].equal(Branch(Leaf("one"), Leaf("two")), Branch(Leaf("one"), Leaf("two"))) - }.assert(_ == true) - - test("construct a default value") { - Default.generic[Entity].default - }.assert(_ == (Company(""): Entity)) - - test("construction of Show instance for Leaf") { - scalac""" - import magnolia.examples._ - implicitly[Show[Leaf]] - """ - }.assert(_ == (Returns(fqt"magnolia.examples.Show[magnolia.examples.Leaf]"): Compilation)) - - test("construction of Show instance for Tree") { - scalac""" - import magnolia.examples._ - implicitly[Show[Tree]] - """ - }.assert(_ == (Returns(fqt"magnolia.examples.Show[magnolia.examples.Tree]"): Compilation)) - - test("serialize a Leaf") { - implicitly[Show[Leaf]].show(Leaf("testing")) - }.assert(_ == "{value=testing}") - - test("serialize a Branch as a Tree") { - implicitly[Show[Tree]].show(Branch(Leaf("LHS"), Leaf("RHS"))) - }.assert(_ == "{left={value=LHS},right={value=RHS}}") - - /*test("construct a decoder") { - Decoder.generic[Tree].decode("string") - }.assert(_ == (Leaf("something"): Tree))*/ - - test("show error stack") { - scalac""" - import magnolia.examples._ - case class Alpha(integer: Double) - case class Beta(alpha: Alpha) - Show.generic[Beta] - """ - }.assert(_ == (TypecheckError(txt"""magnolia: could not find typeclass for type Double - | in parameter 'integer' of product type Alpha - | in parameter 'alpha' of product type Beta - |"""): Compilation)) - } -} diff --git a/tests/src/main/scala/tests.scala b/tests/src/main/scala/tests.scala new file mode 100644 index 0000000..6bfa1bd --- /dev/null +++ b/tests/src/main/scala/tests.scala @@ -0,0 +1,91 @@ +package magnolia.tests + +import magnolia._ +import estrapade._ +import contextual.data.scalac._ +import contextual.data.fqt._ +import contextual.data.txt._ + +import scala.util._ + +object Tests extends TestApp { + + def tests() = { + import examples._ + + test("construct a Show product instance") { + import examples._ + Show.generic[Person].show(Person("John Smith", 34)) + }.assert(_ == """Person(name=John Smith,age=34)""") + + test("construct a Show coproduct instance") { + import examples._ + Show.generic[Person].show(Person("John Smith", 34)) + }.assert(_ == "Person(name=John Smith,age=34)") + + Show.generic[Tree] + + test("serialize a Branch") { + import magnolia.examples._ + implicitly[Show[Branch]].show(Branch(Leaf("LHS"), Leaf("RHS"))) + }.assert(_ == "Branch(left=Leaf(value=LHS),right=Leaf(value=RHS))") + + test("test equality false") { + import examples._ + Eq.generic[Entity].equal(Person("John Smith", 34), Person("", 0)) + }.assert(_ == false) + + test("test equality true") { + import examples._ + Eq.generic[Entity].equal(Person("John Smith", 34), Person("John Smith", 34)) + }.assert(_ == true) + + test("test branch equality true") { + import examples._ + Eq.generic[Tree].equal(Branch(Leaf("one"), Leaf("two")), Branch(Leaf("one"), Leaf("two"))) + }.assert(_ == true) + + test("construct a default value") { + Default.generic[Entity].default + }.assert(_ == (Company(""): Entity)) + + test("construction of Show instance for Leaf") { + scalac""" + import magnolia.examples._ + implicitly[Show[Leaf]] + """ + }.assert(_ == (Returns(fqt"magnolia.examples.Show[magnolia.examples.Leaf]"): Compilation)) + + test("construction of Show instance for Tree") { + scalac""" + import magnolia.examples._ + implicitly[Show[Tree]] + """ + }.assert(_ == (Returns(fqt"magnolia.examples.Show[magnolia.examples.Tree]"): Compilation)) + + test("serialize a Leaf") { + implicitly[Show[Leaf]].show(Leaf("testing")) + }.assert(_ == "Leaf(value=testing)") + + test("serialize a Branch as a Tree") { + implicitly[Show[Tree]].show(Branch(Leaf("LHS"), Leaf("RHS"))) + }.assert(_ == "Branch(left=Leaf(value=LHS),right=Leaf(value=RHS))") + + test("show error stack") { + scalac""" + import magnolia.examples._ + case class Alpha(integer: Double) + case class Beta(alpha: Alpha) + Show.generic[Beta] + """ + }.assert(_ == (TypecheckError(txt"""magnolia: could not find typeclass for type Double + | in parameter 'integer' of product type Alpha + | in parameter 'alpha' of product type Beta + |"""): Compilation)) + + //test("construct a decoder") { + //Decoder.generic[Tree].decode("string") + //}.assert(_ == (Leaf("something"): Tree)) + + } +} -- cgit v1.2.3