Custom Slick Codegen generate mapped case classes

2020-07-25 09:19发布

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

1条回答
ゆ 、 Hurt°
2楼-- · 2020-07-25 09:37

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"
        ""
      }
    }
  }

}
查看更多
登录 后发表回答