动态创建案例类

时间:2016-08-22 21:17:32

标签: scala slick scala-macros

我正在使用一个csv库,它接受一个case类并将其转换成行供我阅读。

语法非常接近File(path).asCsvReader[caseClass]。 链接到库here

但问题是我不能从我的数据库中的表中生成我的案例类。我可以在我的数据库中接收表和它们列的类型(Int,Long,Double,String等),但我不知道如何使用该数据动态创建一个case类,因为我在编译时不知道这些信息

正因为如此,我无法使用宏,因为我在宏的编译时不知道表数据。

那么,一旦收到表数据然后将该case类传递给csv库,我将如何动态创建这个case类?

3 个答案:

答案 0 :(得分:2)

根据dveim中的this answer提示,我添加了第二个解决方案,该解决方案在Scala 2.10和2.11中均有效,并使用Scala Toolbox。遗憾的是,生成的案例类在默认包中。

生成案例类

/**
  * Generate a case class and persist to file
  * Can't add package to the external class
  * @param file External file to write to
  * @param className Class name
  * @param header case class parameters
  */
def writeCaseClass(file: File, className: String, header: String): Unit = {
  val writer: PrintWriter = new PrintWriter(file)
  writer.println("case class " + className + "(")
  writer.println(header)
  writer.println(") {}")
  writer.println("\nscala.reflect.classTag[" + className + "].runtimeClass")
  writer.flush()
  writer.close()
}

使用Toolbox

编译外部类
/**
  * Helper method to search code files in directory
  * @param dir Directory to search in
  * @return
  */
def recursiveListFiles(dir: File): Array[File] = {
  val these = dir.listFiles
  these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)
}  

/**
  * Compile scala files and keep them loaded in memory
  * @param classDir Directory storing the generated scala files
  * @throws IOException if there is problem reading the source files
  * @return Map containing Class name -> Compiled Class Reference
  */
@throws[IOException]
def compileFiles(classDir: String): Map[String, Class[_]] = {
  val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox()

  val files = recursiveListFiles(new File(classDir))
    .filter(_.getName.endsWith("scala"))
  println("Loaded files: \n" + files.mkString("[", ",\n", "]"))

  files.map(f => {
    val src = Source.fromFile(f).mkString
    val clazz = tb.compile(tb.parse(src))().asInstanceOf[Class[_]]
    getClassName(f.getName) -> clazz
  }).toMap
}

实例化和使用已编译的类

编译的类可以从地图中获取并在必要时使用,例如

//Test Address class
def testAddress(map: Map[String, Class[_]]) = {
  val addressClass = map("AddressData")
  val ctor = addressClass.getDeclaredConstructors()(0)
  val instance = ctor.newInstance("123 abc str", "apt 1", "Hello world", "HW", "12345")
  //println("Instantiated class: " + instance.getClass.getName)
  println(instance.toString)
}

答案 1 :(得分:1)

如果您使用的是Scala 2.10,则可以使用scala.tools.nsc.interpreter包中的类来执行此操作。请注意,这在Scala 2.11中不再有效。我问new question,希望我们能得到答案。

在Scala 2.10中,使用解释器可以编译外部类文件并虚拟加载。

步骤如下:

  • 根据惯例计算出您班级的名称
  • 解析您的CSV标题以了解字段名称和数据类型
  • 使用上述信息生成案例类并写入磁盘上的文件
  • 加载生成的源文件并使用解释器
  • 进行编译
  • 类现在可以使用了。

我已经构建了一个应该有用的小型演示。

/**
  * Location to store temporary scala source files with generated case classes
  */
val classLocation: String = "/tmp/dynacode"

/**
  * Package name to store the case classes
  */
val dynaPackage: String = "com.example.dynacsv"

/**
  * Construct this data based on your data model e.g. see data type for Person and Address below.
  * Notice the format of header, it can be substituted directly in a case class definition.
  */
val personCsv: String = "PersonData.csv"
val personHeader: String = "title: String, firstName: String, lastName: String, age: Int, height: Int, gender: Int"

val addressCsv: String = "AddressData.csv"
val addressHeader: String = "street1: String, street2: String, city: String, state: String, zipcode: String"

/**
  * Utility method to extract class name from CSV file
  * @param filename CSV file
  * @return Class name extracted from csv file name stripping out ".ext"
  */
def getClassName(filename: String): String = filename.split("\\.")(0)

/**
  * Generate a case class and persist to file
  * @param file External file to write to
  * @param className Class name
  * @param header case class parameters
  */
def writeCaseClass(file: File, className: String, header: String): Unit = {
  val writer: PrintWriter = new PrintWriter(file)
  writer.println("package " + dynaPackage)
  writer.println("case class " + className + "(")
  writer.println(header)
  writer.println(") {}")
  writer.flush()
  writer.close()
}

/**
  * Generate case class and write to file
  * @param filename CSV File name (should be named ClassName.csv)
  * @param header Case class parameters. Format is comma separated: name: DataType
  * @throws IOException if there is problem writing the file
  */
@throws[IOException]
private def generateClass(filename: String, header: String) {
  val className: String = getClassName(filename)
  val fileDir: String = classLocation + File.separator + dynaPackage.replace('.', File.separatorChar)
  new File(fileDir).mkdirs
  val classFile: String = fileDir + File.separator + className + ".scala"
  val file: File = new File(classFile)

  writeCaseClass(file, className, header)
}

/**
  * Helper method to search code files in directory
  * @param dir Directory to search in
  * @return
  */
def recursiveListFiles(dir: File): Array[File] = {
  val these = dir.listFiles
  these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)
}

/**
  * Compile scala files and keep them loaded in memory
  * @param classDir Directory storing the generated scala files
  * @throws IOException if there is problem reading the source files
  * @return Classloader that contains the compiled external classes
  */
@throws[IOException]
def compileFiles(classDir: String): AbstractFileClassLoader = {
  val files = recursiveListFiles(new File(classDir))
                  .filter(_.getName.endsWith("scala"))
  println("Loaded files: \n" + files.mkString("[", ",\n", "]"))

  val settings: GenericRunnerSettings = new GenericRunnerSettings(err => println("Interpretor error: " + err))
  settings.usejavacp.value = true
  val interpreter: IMain = new IMain(settings)
  files.foreach(f => {
    interpreter.compileSources(new BatchSourceFile(AbstractFile.getFile(f)))
  })

  interpreter.getInterpreterClassLoader()
}

//Test Address class
def testAddress(classLoader: AbstractFileClassLoader) = {
  val addressClass = classLoader.findClass(dynaPackage + "." + getClassName(addressCsv))
  val ctor = addressClass.getDeclaredConstructors()(0)
  val instance = ctor.newInstance("123 abc str", "apt 1", "Hello world", "HW", "12345")
  println("Instantiated class: " + instance.getClass.getCanonicalName)
  println(instance.toString)
}

//Test person class
def testPerson(classLoader: AbstractFileClassLoader) = {
  val personClass = classLoader.findClass(dynaPackage + "." + getClassName(personCsv))
  val ctor = personClass.getDeclaredConstructors()(0)
  val instance = ctor.newInstance("Mr", "John", "Doe", 25: java.lang.Integer, 165: java.lang.Integer, 1: java.lang.Integer)
  println("Instantiated class: " + instance.getClass.getCanonicalName)
  println(instance.toString)
}

//Test generated classes
def testClasses(classLoader: AbstractFileClassLoader) = {
  testAddress(classLoader)
  testPerson(classLoader)
}

//Main method
def main(args: Array[String]) {
  try {
    generateClass(personCsv, personHeader)
    generateClass(addressCsv, addressHeader)
    val classLoader = compileFiles(classLocation)
    testClasses(classLoader)
  }
  catch {
    case e: Exception => e.printStackTrace()
  }
}

}

输出:

Loaded files: 
[/tmp/dynacode/com/example/dynacsv/AddressData.scala,
 /tmp/dynacode/com/example/dynacsv/PersonData.scala]
Instantiated class: com.example.dynacsv.AddressData
AddressData(123 abc str,apt 1,Hello world,HW,12345)
Instantiated class: com.example.dynacsv.PersonData
PersonData(Mr,John,Doe,25,165,1)

答案 2 :(得分:0)

所以基本上你需要一个关于你的case类的运行时信息?我想你应该使用.hm-our-products-main-showcase .accordion-list-items > li.active > a { position: relative; } .hm-our-products-main-showcase .accordion-list-items > li.active > a:after { background: url(active-accordion-tab.jpg) no-repeat; content: ''; margin-left: -7px; /* Half the width to center the arrow. */ position: absolute; left: 50%; bottom: -1px; /* Compensating menu's border-bottom. */ width: 14px; height: 8px; }

ClassTag

这将允许您在运行时实例化您的案例类。

由于您可以找出CSV列的类型,因此您可以在import scala.reflect._ def asCsvReader[T: ClassTag]: T = { classTag[T].runtimeClass.getDeclaredConstructor(...).newInstance(...) ... } getDeclaredConstructor方法中提供相应的类型。