2

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
phedoreanu
  • 2,588
  • 3
  • 25
  • 29
  • you should be able to do this immutably, but I don't have the time to look into it right now – cvogt Jun 25 '15 at 23:09
  • `mkString()` is using `scala.collection.mutable.StringBuilder`, which has an __append__ complexity of `aC` vs. `C` from `ListBuffer/MutableList ` http://docs.scala-lang.org/overviews/collections/performance-characteristics.html – phedoreanu Jun 26 '15 at 00:16
  • I don't see asymptotic complexity as an important subject when we are generating a few strings at compile time – cvogt Jun 26 '15 at 12:53
  • correctness and clarity are much more important concerns. immutability helps here. – cvogt Jun 26 '15 at 12:54

1 Answers1

3

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

}
Minidevops
  • 46
  • 7