Skip to content

Instantly share code, notes, and snippets.

@manojo
Created March 9, 2015 21:47
Show Gist options
  • Save manojo/6b807737bfbdaccc6755 to your computer and use it in GitHub Desktop.
Save manojo/6b807737bfbdaccc6755 to your computer and use it in GitHub Desktop.
Nested Options and Slick
package models
import slick.lifted.Tag
trait Test {
val profile: slick.driver.JdbcProfile
import profile.simple._
/**
* The wrapper class has an optional quantity field
* which itself has an optional size field
*/
case class Wrapper(id: Option[Long], quantity: Option[Quantity])
case class Quantity(size: Option[Int])
class Wrappers(_tableTag: Tag) extends Table[Wrapper](_tableTag, "wrappers") {
val id: Column[Long] = column[Long]("id", O.AutoInc, O.PrimaryKey)
val aField = column[Option[Int]]("aField")
def * = (
id.?,
aField
) <> ((columns2Wrapper _).tupled, wrapper2Columns)
def columns2Wrapper(id: Option[Long], a: Option[Int]): Wrapper =
Wrapper(id, for (size <- a) yield Quantity(a))
def wrapper2Columns(f: Wrapper) =
Some((f.id, f.quantity.map(_.size)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment