|
| 1 | +package com.kifi.macros |
| 2 | + |
| 3 | +import scala.reflect.macros._ |
| 4 | +import scala.language.experimental.macros |
| 5 | +import scala.annotation.StaticAnnotation |
| 6 | + |
| 7 | +import CrossVersionDefs._ |
| 8 | + |
| 9 | +/** |
| 10 | + * "@json" macro annotation for case classes |
| 11 | + * |
| 12 | + * This macro annotation automatically creates a JSON serializer for the annotated case class. |
| 13 | + * The companion object will be automatically created if it does not already exist. |
| 14 | + * |
| 15 | + * If the case class has more than one field, the default Play formatter is used. |
| 16 | + * If the case class has only one field, the field is directly serialized. For example, if A |
| 17 | + * is defined as: |
| 18 | + * |
| 19 | + * case class A(value: Int) |
| 20 | + * |
| 21 | + * then A(4) will be serialized as '4' instead of '{"value": 4}'. |
| 22 | + */ |
| 23 | +class json extends StaticAnnotation { |
| 24 | + def macroTransform(annottees: Any*): Any = macro jsonMacro.impl |
| 25 | +} |
| 26 | + |
| 27 | +object jsonMacro { |
| 28 | + def impl(c: CrossVersionContext)(annottees: c.Expr[Any]*): c.Expr[Any] = { |
| 29 | + import c.universe._ |
| 30 | + |
| 31 | + def extractClassNameAndFields(classDecl: ClassDef) = { |
| 32 | + try { |
| 33 | + val q"case class $className(..$fields) extends ..$bases { ..$body }" = classDecl |
| 34 | + (className, fields) |
| 35 | + } catch { |
| 36 | + case _: MatchError => c.abort(c.enclosingPosition, "Annotation is only supported on case class") |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + def jsonFormatter(className: TypeName, fields: List[ValDef]) = { |
| 41 | + fields.length match { |
| 42 | + case 0 => c.abort(c.enclosingPosition, "Cannot create json formatter for case class with no fields") |
| 43 | + case 1 => |
| 44 | + // Only one field, use the serializer for the field |
| 45 | + q""" |
| 46 | + implicit val jsonAnnotationFormat = { |
| 47 | + import play.api.libs.json._ |
| 48 | + Format( |
| 49 | + __.read[${fields.head.tpt}].map(s => ${className.toTermName}(s)), |
| 50 | + new Writes[$className] { def writes(o: $className) = Json.toJson(o.${fields.head.name}) } |
| 51 | + ) |
| 52 | + } |
| 53 | + """ |
| 54 | + case _ => |
| 55 | + // More than one field, use Play's macro |
| 56 | + q"implicit val jsonAnnotationFormat = play.api.libs.json.Json.format[$className]" |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + def modifiedCompanion(compDeclOpt: Option[ModuleDef], format: ValDef, className: TypeName) = { |
| 61 | + compDeclOpt map { compDecl => |
| 62 | + // Add the formatter to the existing companion object |
| 63 | + val q"object $obj extends ..$bases { ..$body }" = compDecl |
| 64 | + q""" |
| 65 | + object $obj extends ..$bases { |
| 66 | + ..$body |
| 67 | + $format |
| 68 | + } |
| 69 | + """ |
| 70 | + } getOrElse { |
| 71 | + // Create a companion object with the formatter |
| 72 | + q"object ${className.toTermName} { $format }" |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + def modifiedDeclaration(classDecl: ClassDef, compDeclOpt: Option[ModuleDef] = None) = { |
| 77 | + val (className, fields) = extractClassNameAndFields(classDecl) |
| 78 | + val format = jsonFormatter(className, fields) |
| 79 | + val compDecl = modifiedCompanion(compDeclOpt, format, className) |
| 80 | + |
| 81 | + // Return both the class and companion object declarations |
| 82 | + c.Expr(q""" |
| 83 | + $classDecl |
| 84 | + $compDecl |
| 85 | + """) |
| 86 | + } |
| 87 | + |
| 88 | + annottees.map(_.tree) match { |
| 89 | + case (classDecl: ClassDef) :: Nil => modifiedDeclaration(classDecl) |
| 90 | + case (classDecl: ClassDef) :: (compDecl: ModuleDef) :: Nil => modifiedDeclaration(classDecl, Some(compDecl)) |
| 91 | + case _ => c.abort(c.enclosingPosition, "Invalid annottee") |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments