1

问题:

我想用 scala 宏使带注释的类成为另一个类的子类我有什么:

字段包装器:

class Field(fieldType: DbModelFieldType, fieldName: String) 

一个抽象类(所有注释类的基类):

abstract class DatabaseModel {
  def fields: Seq[Fields]
}

我有一个案例类:

Model(num: Int, sym: Char, descr: String)

如果用注释该类@GetFromDB

@GetFromDB
Model(num: Int, sym: Char, descr: String)
case class Model(num: Int, sym: Char, descr: String) extends DatabaseModel {
   override def fields: Seq[Fields] = 
       Seq(Field(IntFieldType(), "num"),
           Field(CharFieldType(), "sym"),
           Field(StringFieldType(), "descr")
          ) 
}

我想要的结果应该是这样的:

val m: DatabaseModel = Model(1, 'A', "First Name")

我看过类似的问题

使用方法(字段=方法)为案例类生成伴随对象

那么我如何扩展该解决方案以达到预期的结果?

import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.language.experimental.macros
import scala.reflect.macros.whitebox

object Macros {
  @compileTimeOnly("enable macro paradise")
  class GenerateCompanionWithFields extends StaticAnnotation {
    def macroTransform(annottees: Any*): Any = macro Macro.impl
  }

  object Macro {
    def impl(c: whitebox.Context)(annottees: c.Tree*): c.Tree = {
      import c.universe._
      annottees match {
        case (cls @ q"$_ class $tpname[..$_] $_(...$paramss) extends { ..$_ } with ..$_ { $_ => ..$_ }") :: Nil =>

          val newMethods = paramss.flatten.map {
            case q"$_ val $tname: $tpt = $_" =>
              q"def $tname(): String = ${tpt.toString}"
          }

          q"""
             $cls

             object ${tpname.toTermName} {
               ..$newMethods
             }
           """
      }
    }
  }
}
4

1 回答 1

2

尝试

import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.language.experimental.macros
import scala.reflect.macros.whitebox

object Macros {
  @compileTimeOnly("enable macro paradise")
  class GetFromDB extends StaticAnnotation {
    def macroTransform(annottees: Any*): Any = macro GetFromDBMacro.impl
  }

  object GetFromDBMacro {
    def impl(c: whitebox.Context)(annottees: c.Tree*): c.Tree = {
      import c.universe._
      annottees match {
        case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" :: tail =>
          val fields = paramss.flatten.map {
            case q"$_ val $tname: $tpt = $_" => q"Field(${TermName(tpt.toString + "FieldType")}.apply(), ${tname.toString})"
          }
          q"""
             $mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..${tq"DatabaseModel" :: parents} { $self =>
               override def fields: _root_.scala.collection.immutable.Seq[Field] = _root_.scala.collection.immutable.Seq.apply(..$fields)

               ..$stats
             }

             ..$tail
           """
      }
    }
  }
}

import Macros._

object App {
  sealed trait DbModelFieldType
  case class IntFieldType() extends DbModelFieldType
  case class CharFieldType() extends DbModelFieldType
  case class StringFieldType() extends DbModelFieldType

  case class Field(fieldType: DbModelFieldType, fieldName: String)

  abstract class DatabaseModel {
    def fields: Seq[Field]
  }

  @GetFromDB
  case class Model(num: Int, sym: Char, descr: String)

//Warning:scalac: {
//  case class Model extends DatabaseModel with scala.Product with scala.Serializable {
//    <caseaccessor> <paramaccessor> val num: Int = _;
//    <caseaccessor> <paramaccessor> val sym: Char = _;
//    <caseaccessor> <paramaccessor> val descr: String = _;
//    def <init>(num: Int, sym: Char, descr: String) = {
//      super.<init>();
//      ()
//    };
//    override def fields: _root_.scala.collection.immutable.Seq[Field] = _root_.scala.collection.immutable.Seq.apply(Field(IntFieldType.apply(), "num"), Field(CharFieldType.apply(), "sym"), Field(StringFieldType.apply(), "descr"))
//  };
//  ()
//}

  val m: DatabaseModel = Model(1, 'A', "First Name")

  def main(args: Array[String]): Unit = {
    println(
      m.fields //List(Field(IntFieldType(),num), Field(CharFieldType(),sym), Field(StringFieldType(),descr))
    )
  }
}
于 2019-10-06T14:17:41.443 回答