Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Between Enumeration on Scala and Slick

I am newbie to Play Scala and Slick. I am trying to map between Enumeration on Scala to String/Int on Slick. I wrote the following code but I got an error. Could you please let me know how I can fix this.

I am using 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)
}

I got the following error.

[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]                              ^

Please see the below link for whole code.

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

--

Fix with this code.

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)
  )
}
like image 653
Yohei Onishi Avatar asked Dec 17 '15 13:12

Yohei Onishi


1 Answers

Here is the fix:

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)
  )
}
like image 101
Valy Dia Avatar answered Nov 05 '22 18:11

Valy Dia