Since we use Slick for MySQL access in our services, I wanted to combine these several columns in one single (enumeration) field of the mapping case class:
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
case class User(id: Option[Int], nickname: String, | |
roles: Set[Role.Role]) | |
object Users extends Table[(User)]("ask_user") { | |
def id = column[Int]("id", O.PrimaryKey, O.AutoInc) | |
def nickname = column[String]("nickname") | |
def isEditor = column[Boolean]("is_editor") | |
def isModerator = column[Boolean]("is_moderator") | |
} |
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
class User extends Table[User](...){ | |
... | |
def * = id ~ nickname ~ isEditor ~ isModerator <> (constructUser, extractUser) | |
} | |
def constructUser( id: Option[Int], nickname: String, isEditor: Boolean, | |
isModerator: Boolean ) | |
= User(id, nickname, Roles(isEditor, isModerator)) | |
def extractUser( user: User ): Option[(Option[Int], String, Boolean, Boolean)] = | |
user match { | |
case User(Some(id), nickname, Roles(isEditor, isModerator)) => | |
Some(Option(id), nickname, isEditor, isModerator) | |
} | |
object Roles{ | |
def apply( isEditor: Boolean, isModerator: Boolean) : Set[Role] = ... | |
def unapply( roles: Set[Role] ) : Option[(Boolean, Boolean)] = ... | |
} |
overloaded method value <> with alternatives:Thanks Mr. Vogt from Typesafe for the hint.
[R(in method <>)...
userModeratorMajor ~ partner ~ premiumPartner ~ corporatePaid <> (constructUser _, extractUser _)
Keine Kommentare:
Kommentar veröffentlichen