Search code examples
scalaslickscala-2.11slick-3.0slick-codegen

Custom Slick Codegen generate mapped case classes outside the `${container} trait`?


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

Solution

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