Skip to content

Commit

Permalink
simplistic implementation of toMap
Browse files Browse the repository at this point in the history
  • Loading branch information
mpollmeier committed Dec 1, 2016
1 parent 141b544 commit 9d06bc3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Expand Up @@ -12,7 +12,7 @@ class MappableTest extends WordSpec with Matchers {
"simple case class" should {
"deserialise to map" in {
val testInstance = SimpleCaseClass(i = 42, s = "something")
testInstance.toMap shouldBe 'empty
testInstance.toMap shouldBe Map("i" -> 42, "s" -> "something")
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/scala/scala/meta/serialiser/mappable.scala
Expand Up @@ -8,9 +8,14 @@ class mappable extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
val q"..$mods class $tName (..$params) extends $template" = defn

val keyValues: Seq[Term] = params.map { param =>
val memberName = Term.Name(param.name.value)
q"${param.name.value} -> $memberName"
}

val res = q"""
..$mods class $tName(..$params) {
def toMap(): Map[String, Any] = Map.empty[String, Any]
def toMap(): Map[String, Any] = Map[String, Any](..$keyValues)
}
"""

Expand Down

0 comments on commit 9d06bc3

Please sign in to comment.