Scala上的枚举与光滑之间的映射

时间:2015-12-17 13:13:37

标签: scala enums slick slick-3.0

我是Play Scala和Slick的新手。我试图在Scala上的Enumeration和Slick上的String / Int之间进行映射。我写了下面的代码但是我收到了一个错误。能告诉我如何解决这个问题。

我正在使用Scala 2.11.6,Slick 3.1.0,Play 2.4.4,play-slick 1.1.1。

package models

import javax.inject.{Inject, Singleton}

import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile

import scala.concurrent.ExecutionContext

class Color extends Enumeration {
  type Color = Value
  val Blue = Value("Blue")
  val Red = Value("Red")
  val Green = Value("Green")
}
object Color extends Color

case class Sample(name:String, id:Int, c:Color)

@Singleton
class ColorDao @Inject()(dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
  val dbConfig = dbConfigProvider.get[JdbcProfile]
  import dbConfig._
  import driver.api._

  class SampleTable(tag: Tag) extends Table[Sample](tag, "Sample") {
    def name  = column[String]("NAME")
    def id    = column[Int]("ID")
    def color = column[Color]("COLOR")
    def * = (name, id, color) <> (Sample.tupled, Sample.unapply)
  }

  def enumStringMapper(enum: Enumeration) = MappedColumnType.base[enum.Value, String](
    e => e.toString,
    s => enum.withName(s)
  )
  implicit val colorMapper = enumStringMapper(Color)
}

我收到了以下错误。

[error] /Users/yohei/IdeaProjects/slick3-enum-
mapping/app/models/Sample.scala:30:
could not find implicit value for parameter tt:
slick.ast.TypedType[models.Color]
[error]     def color = column[Color]("COLOR")
[error]                              ^

请参阅以下链接了解整个代码。

https://github.com/yohei1126/slick3-enum-mapping

-

修复此代码。

package models

import javax.inject.{Inject, Singleton}

import models.Bar.Bar
import models.Color.Color
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile

import scala.concurrent.ExecutionContext

object Color extends Enumeration {
  type Color = Value
  val Blue = Value("Blue")
  val Red = Value("Red")
  val Green = Value("Green")
}

case class Sample(name:String, id:Int, c:Color)

@Singleton
class ColorDao @Inject()(dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
  val dbConfig = dbConfigProvider.get[JdbcProfile]
  import dbConfig._
  import driver.api._

  class SampleTable(tag: Tag) extends Table[Sample](tag, "Sample") {
    def name  = column[String]("NAME")
    def id    = column[Int]("ID")
    def color = column[Color]("COLOR")
    def * = (name, id, color) <> (Sample.tupled, Sample.unapply)
  }
  implicit val colorMapper = MappedColumnType.base[Color, String](
    e => e.toString,
    s => Color.withName(s)
  )
}

1 个答案:

答案 0 :(得分:0)

这里是解决方法:

package models

import javax.inject.{Inject, Singleton}

import models.Bar.Bar
import models.Color.Color
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile

import scala.concurrent.ExecutionContext

object Color extends Enumeration {
  type Color = Value
  val Blue = Value("Blue")
  val Red = Value("Red")
  val Green = Value("Green")
}

case class Sample(name:String, id:Int, c:Color)

@Singleton
class ColorDao @Inject()(dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
  val dbConfig = dbConfigProvider.get[JdbcProfile]
  import dbConfig._
  import driver.api._

  class SampleTable(tag: Tag) extends Table[Sample](tag, "Sample") {
    def name  = column[String]("NAME")
    def id    = column[Int]("ID")
    def color = column[Color]("COLOR")
    def * = (name, id, color) <> (Sample.tupled, Sample.unapply)
  }
  implicit val colorMapper = MappedColumnType.base[Color, String](
    e => e.toString,
    s => Color.withName(s)
  )
}