This is in continuation to the following post:
How to combine multiple columns in one case class field when using lifted embedding?
I am a great fan of bit fields, wants to use this logic in a project where I am using slick-macros. Unfortunately slick-additions is not fully updated to slick 2.0, there is a branch dedicated which is not stable, in fact won't even compile. I wanted to test above logic directly but ended up with errors below:
As per the post above:
trait Bitmasked {
type Value
def bitFor: Value => Int
def forBit: Int => Value
def values: Iterable[Value]
def longToSet: Long => Set[Value] =
bm => values.toSeq.filter(v => 0 != (bm & (1 << bitFor(v)))).toSet
def setToLong: Set[Value] => Long =
_.foldLeft(0L){ (bm, v) => bm + (1L << bitFor(v)) }
implicit lazy val enumTypeMapper: BaseTypeMapper[Value] =
MappedTypeMapper.base[Value, Int](bitFor, forBit)
implicit lazy val enumSetTypeMapper: BaseTypeMapper[Set[Value]] =
MappedTypeMapper.base[Set[Value], Long](setToLong, longToSet)
implicit lazy val getResult: GetResult[Value] =
GetResult(r => forBit(r.nextInt))
implicit lazy val getSetResult: GetResult[Set[Value]] =
GetResult(r => longToSet(r.nextLong))
}
/** Mix this class into a subclass of Enumeration to have it usable as a
* column type in a Slick Table. */
trait BitmaskedEnumeration extends Bitmasked { this: Enumeration =>
def bitFor = _.id
def forBit = apply(_)
}
object Role extends Enumeration with BitmaskedEnumeration {
type Role = Value
val Editor, Moderator, Administrator, Usermoderator, Usermoderator2, Partner, PremiumPartner, CorporatePaid = Value
}
.
[error] C:\git\slick-macros\database\src\main\scala\model\Database.scala:9: object BaseTypeMapper is not a member of package scala.slick.lifted
[error] import slick.lifted.{BaseTypeMapper, MappedTypeMapper}
[error] ^
[error] C:\git\slick-macros\database\src\main\scala\model\Database.scala:56: not found: value Role
[error] import Role._
[error] ^
[error] C:\git\slick-macros\database\src\main\scala\model\Database.scala:61: not found: type Role
[error] case class Member(login: String, rights: UserRights, roles: Role, addr: Address, company: Company, manager: Option[Member]) {
[error] ^
[error] three errors found
[error] (database/compile:compile) Compilation failed
[error] Total time: 17 s, completed Mar 18, 2014 1:18:30 PM
Is there a way to bit-mapped database columns deflated and mapped?
say, a user is an Editor when the value is 1 (binary 0001), Moderator when the value is 2 (binary 0010) and both Editor and Moderator when the value is 3 (binary 0011). I want a way to deflate this and set the flags in Scala.
Thank You, shashi