diff --git a/slick-additions-entity/src/main/scala/slick/additions/entity/Entity.scala b/slick-additions-entity/src/main/scala/slick/additions/entity/Entity.scala index 8d8a99b..a3f01b6 100644 --- a/slick-additions-entity/src/main/scala/slick/additions/entity/Entity.scala +++ b/slick-additions-entity/src/main/scala/slick/additions/entity/Entity.scala @@ -1,20 +1,29 @@ package slick.additions.entity -sealed trait EntityRef[K, +A] +sealed trait EntityRef[K, +A] { + def transform[B](f: A => B): EntityRef[K, B] + def widen[B >: A]: EntityRef[K, B] +} sealed trait Lookup[K, +A] extends EntityRef[K, A] { def key: K + override def transform[B](f: A => B): Lookup[K, B] + override def widen[B >: A]: Lookup[K, B] def foldLookup[X](key: EntityKey[K, A] => X, ent: KeyedEntity[K, A] => X): X = this match { case ek: EntityKey[K, A] => key(ek) case ke: KeyedEntity[K, A] => ent(ke) } def valueOption: Option[A] = foldLookup(_ => None, ke => Some(ke.value)) def sameKey(that: Lookup[K, _]) = this.key == that.key + def toEntityKey: EntityKey[K, A] } -case class EntityKey[K, +A](key: K) extends Lookup[K, A] - +case class EntityKey[K, +A](key: K) extends Lookup[K, A] { + override def transform[B](f: A => B): EntityKey[K, B] = copy() + override def widen[B >: A]: EntityKey[K, B] = transform(identity) + override def toEntityKey = this +} sealed trait Entity[K, +A] extends EntityRef[K, A] { def keyOption: Option[K] @@ -23,7 +32,8 @@ sealed trait Entity[K, +A] extends EntityRef[K, A] { def isSaved: Boolean - def transform[B](f: A => B): Entity[K, B] + override def transform[B](f: A => B): Entity[K, B] + override def widen[B >: A]: Entity[K, B] def modify[B](f: A => B): Entity[K, B] @deprecated("Use modify", "0.9.1") @@ -44,6 +54,7 @@ case class KeylessEntity[K, +A](value: A) extends Entity[K, A] { override def equals(that: Any) = this eq that.asInstanceOf[AnyRef] override def transform[B](f: A => B): KeylessEntity[K, B] = new KeylessEntity[K, B](f(value)) + override def widen[B >: A]: KeylessEntity[K, B] = transform(identity) override def modify[B](f: A => B) = transform(f) final override def map[B >: A](f: A => B): KeylessEntity[K, B] = modify(f) @@ -54,11 +65,13 @@ sealed trait KeyedEntity[K, +A] extends Entity[K, A] with Lookup[K, A] { override def keyOption = Some(key) override def transform[B](f: A => B): KeyedEntity[K, B] + override def widen[B >: A]: KeyedEntity[K, B] override def modify[B](f: A => B): ModifiedEntity[K, B] = ModifiedEntity[K, B](key, f(value)) final override def map[B >: A](f: A => B): ModifiedEntity[K, B] = modify(f) def toSaved: SavedEntity[K, A] = SavedEntity(key, value) def asLookup: Lookup[K, A] = this + override def toEntityKey = EntityKey(key) } object KeyedEntity { def apply[K, A](key: K, value: A): KeyedEntity[K, A] = SavedEntity[K, A](key, value) @@ -66,9 +79,11 @@ object KeyedEntity { } case class SavedEntity[K, +A](key: K, value: A) extends KeyedEntity[K, A] { final override def isSaved = true - override def transform[B](f: A => B) = SavedEntity(key, f(value)) + override def transform[B](f: A => B): SavedEntity[K, B] = SavedEntity(key, f(value)) + override def widen[B >: A]: SavedEntity[K, B] = transform(identity) } case class ModifiedEntity[K, +A](key: K, value: A) extends KeyedEntity[K, A] { final override def isSaved = false - override def transform[B](f: A => B) = ModifiedEntity(key, f(value)) + override def transform[B](f: A => B): ModifiedEntity[K, B] = ModifiedEntity(key, f(value)) + override def widen[B >: A]: ModifiedEntity[K, B] = transform(identity) }