【问题标题】:Custom Slick Codegen generate mapped case classes outside the `${container} trait`?自定义 Slick Codegen 在“${container} 特征”之外生成映射案例类?
【发布时间】:2015-06-25 11:14:22
【问题描述】:

Slick Codegen 能否生成${container} trait 之外的所有映射案例类 ,这样他们就不会继承它的类型?也许完全在另一个文件中,即Models.scala

// SuppliersRowsDAA.scala
import persistence.Tables

object SuppliersRowsDAA {
  case class Save(sup: Tables.SuppliersRow)
}   

我得到这个编译错误:

[error] /app/src/main/scala/persistence/dal/SuppliersDAA.scala:5: type mismatch;
[error]  found   : persistence.Tables.SuppliersRow
[error]  required: SuppliersDAA.this.SuppliersRow
[error]     case Save(sup) ⇒ sender ! db.run(Suppliers += sup)

使用Tables#SuppliersRow import 会出现同样的错误。

如果我在自动生成的 trait Tables 之外手动剪切和粘贴 SuppliersRow 案例类,它可以工作!

....

trait Tables {

....
}
case class SuppliersRow(id: Int, userId: Int, name: String)
//EOF

【问题讨论】:

  • 你应该可以一成不变地做到这一点,但我现在没有时间研究它
  • mkString() 使用的是scala.collection.mutable.StringBuilder,它的附加复杂度为aCCC 来自ListBuffer/MutableList docs.scala-lang.org/overviews/collections/…
  • 当我们在编译时生成一些字符串时,我不认为渐近复杂性是一个重要的主题
  • 正确性和清晰度是更重要的问题。不变性在这里有所帮助。

标签: scala slick scala-2.11 slick-3.0 slick-codegen


【解决方案1】:

EntityType的原始docWithCode附加到super.packageCode()

import slick.codegen.SourceCodeGenerator
import slick.model.Model
import scala.collection.mutable

class CustomizedCodeGenerator(model: Model) extends SourceCodeGenerator(model) {
  val models = new mutable.MutableList[String]

  override def packageCode(profile: String, pkg: String, container: String, parentType: Option[String]): String = {
    super.packageCode(profile, pkg, container, parentType) + "\n" + outsideCode
  }

  def outsideCode = s"${indent(models.mkString("\n"))}"

  override def Table = new Table(_) {
    override def EntityType = new EntityTypeDef {
      override def docWithCode: String = {
        models += super.docWithCode.toString + "\n"
        ""
      }
    }
  }

}

【讨论】:

    猜你喜欢
    • 2017-01-22
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多