Skip to content

Commit

Permalink
slick-additions-entity: some more utility methods (widen, toEntityKey…
Browse files Browse the repository at this point in the history
…; transform and widen are defined on EntityRef)
  • Loading branch information
nafg committed Jun 18, 2020
1 parent f4f043e commit d4923eb
Showing 1 changed file with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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")
Expand All @@ -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)

Expand All @@ -54,21 +65,25 @@ 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)
def unapply[K, A](ke: KeyedEntity[K, A]): Option[(K, A)] = Some((ke.key, ke.value))
}
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)
}

0 comments on commit d4923eb

Please sign in to comment.