io.youi.persist.Persistence.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of youi-core_sjs0.6_2.13 Show documentation
Show all versions of youi-core_sjs0.6_2.13 Show documentation
Core functionality leveraged and shared by most other sub-projects of YouI.
The newest version!
package io.youi.persist
import io.circe.Json
trait Persistence[T] {
protected def identifier: String
protected def parentPersistence: Option[Persistence[_ >: T]]
protected def create(): T
protected def saveInfo(t: T): Json
protected def loadInfo(t: T, json: Json): Unit
def save(t: T): Json = {
val identifierJson = Json.obj("identifier" -> Json.fromString(identifier))
val parentJson = parentPersistence.map(_.save(t)).getOrElse(Json.obj())
val json = saveInfo(t)
parentJson.deepMerge(identifierJson).deepMerge(json)
}
def load(json: Json): T = {
val t: T = create()
parentPersistence.foreach(_.loadInfo(t, json))
loadInfo(t, json)
t
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy