自定义 Slick Codegen 在`${container} trait` 之外生成映射的案例类? [英] Custom Slick Codegen generate mapped case classes outside the `${container} trait`?

查看:48
本文介绍了自定义 Slick Codegen 在`${container} trait` 之外生成映射的案例类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以 Slick Codegen 生成 ${container} trait 之外的所有映射案例类,这样他们就不会继承它的类型?也许在另一个文件中,即 Models.scala?

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

我收到此编译错误:

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

使用 Tables#SuppliersRow 导入会出现相同的错误.

Using Tables#SuppliersRow import gives the same error.

如果我手动剪切 &将 SuppliersRow 案例类粘贴到自动生成的 trait Tables 之外,它起作用了!

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

推荐答案

EntityType的原始docWithCode追加到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"
        ""
      }
    }
  }

}

这篇关于自定义 Slick Codegen 在`${container} trait` 之外生成映射的案例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆