Custom Slick Code Generator 3.0.0

自闭症网瘾萝莉.ら 提交于 2019-12-06 09:49:30

问题


Can slick codegen generate all the mapped case classes outside of the ${container} trait, so that they don't inherit its type? Maybe in another file altogether i.e. Models.scala?

// SuppliersRowsDAA.scala
import persistence.Tables

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

I get this compilation error:

[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)

Using Tables#SuppliersRow import gives the same error.

If I manually cut & paste the SuppliersRow case class outside of the auto-generated trait Tables it works!

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

Any ideas?


回答1:


Appending the original docWithCode of EntityType to 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"
        ""
      }
    }
  }

}


来源:https://stackoverflow.com/questions/31048780/custom-slick-code-generator-3-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!