2
votes

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
you should be able to do this immutably, but I don't have the time to look into it right now - cvogt
mkString() is using scala.collection.mutable.StringBuilder, which has an append complexity of aC vs. C from ListBuffer/MutableList docs.scala-lang.org/overviews/collections/… - phedoreanu
I don't see asymptotic complexity as an important subject when we are generating a few strings at compile time - cvogt
correctness and clarity are much more important concerns. immutability helps here. - cvogt

1 Answers

3
votes

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

}