我想说您可能可以利用“拉皮条”模式在两者之间来回切换。这是我在应用程序中使用的一大块东西。我只转换一种方式,但您可以看到如何使用另一种隐式方式,您可以实现逻辑以返回原始/您的格式。
// implicits for pimping
implicit def pimpUser(user: User) = PimpedUser(user)
implicit def pimpLimitedViewUser(user: LimitedViewUser) = PimpedLimitedViewUser(user)
@entity
case class User(@serializer(classOf[UUIDOptionSerializer]) @id @column(name = "id") override val id: Option[UUID] = None,
@serializer(classOf[DateOptionSerializer]) @column(name = "created_on") override val createdOn: Option[Date] = None,
@serializer(classOf[DateOptionSerializer]) @column(name = "modified_on") override val modifiedOn: Option[Date] = None,
@serializer(classOf[StringOptionSerializer]) @column(name = "first_name") firstName: Option[String] = None,
@serializer(classOf[StringOptionSerializer]) @column(name = "last_name") lastName: Option[String] = None,
@serializer(classOf[StringOptionSerializer]) @column(name = "email_address") emailAddress: Option[String] = None,
@serializer(classOf[StringOptionSerializer]) @column(name = "password_salt") passwordSalt: Option[String] = None,
@serializer(classOf[StringOptionSerializer]) @column(name = "password_hash") passwordHash: Option[String] = None,
@serializer(classOf[IntOptionSerializer]) @column(name = "extension") extension: Option[Int] = None,
devices: Set[Device] = Set.empty,
@serializer(classOf[RingModeSerializer]) @column(name = "ring_mode") ringMode: RingMode = Waterfall) extends IdBaseEntity[UUID] {
def this() = this(None, None, None, None, None, None, None, None, None, Set.empty[Device], Waterfall)
override def equals(other: Any): Boolean =
other match {
case that: User => (that canEqual this) && (this.id match {
case Some(uuid) => that.id match {
case Some(thatUuid) => uuid == thatUuid
case None => false
}
case None => false
})
case _ => false
}
override def hashCode: Int = this.id match {
case Some(uuid) => 41 * (41 + uuid.hashCode())
case None => super.hashCode
}
override def canEqual(other: Any): Boolean = other.isInstanceOf[User]
def asAvailable: User = {
User.devices.set(this)(devices.map(_.asAvailable))
}
def asUnavailable: User = {
User.devices.set(this)(devices.map(_.asUnavailable))
}
}
object User {
implicit val userIso = Iso.hlist(User.apply _, User.unapply _)
val id = Lens[User] >> _0
val createdOn = Lens[User] >> _1
val modifiedOn = Lens[User] >> _2
val firstName = Lens[User] >> _3
val lastName = Lens[User] >> _4
val emailAddress = Lens[User] >> _5
val passwordSalt = Lens[User] >> _6
val passwordHash = Lens[User] >> _7
val extension = Lens[User] >> _8
val devices = Lens[User] >> _9
val ringMode = Lens[User] >> _10
}
case class LimitedViewUser(id: Option[UUID], createdOn: Option[Date], modifiedOn: Option[Date], firstName: Option[String], lastName: Option[String]) {
def this(user: User) = this(user.id, user.createdOn, user.modifiedOn, user.firstName, user.lastName)
}
case class PimpedUser(underlying: User) {
def limited = LimitedViewUser(underlying)
}
case class PimpedLimitedViewUser(underlying: LimitedViewUser) {
def normal = /* do something here to transform back to yours/the normal one */
}
// As long as those implicits are brought into scope, you will be able to do this:
val user: User = /* ... */
val limitedViewUser: LimitedViewUser = user.limited
val normalUser: User = limitedViewUser.normal