Created
March 9, 2015 21:47
-
-
Save manojo/6b807737bfbdaccc6755 to your computer and use it in GitHub Desktop.
Nested Options and Slick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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