Skip to content

Commit

Permalink
Merge branch 'migrator-context'
Browse files Browse the repository at this point in the history
  • Loading branch information
xian committed Jul 27, 2024
2 parents 70e0f21 + 3f2973c commit 7dfe341
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/baaahs/api/ws/WebSocketClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class WebSocketClient(
"loadSession",
json.encodeToJsonElement(String.serializer(), name)
)
return mappingSessionStore.decode(response)
return mappingSessionStore.decode(response, name)
}

private suspend fun sendCommand(command: String, vararg args: JsonElement): JsonElement {
Expand Down
14 changes: 7 additions & 7 deletions src/commonMain/kotlin/baaahs/client/document/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ class DataStore<T : Any>(
private val plugins: Plugins,
private val migrator: DataMigrator<T>
) {
fun decode(content: String) =
plugins.json.decodeFromString(migrator, content)
fun decode(content: String, fileName: String? = null) =
plugins.json.decodeFromString(migrator.Migrate(fileName), content)

fun decode(content: JsonElement) =
plugins.json.decodeFromJsonElement(migrator, content)
fun decode(content: JsonElement, fileName: String? = null) =
plugins.json.decodeFromJsonElement(migrator.Migrate(fileName), content)

suspend fun load(file: Fs.File): T? =
file.read()?.let { decode(it) }
file.read()?.let { decode(it, file.toString()) }

fun encode(content: T) =
plugins.json.encodeToString(migrator, content)
plugins.json.encodeToString(migrator.Migrate(), content)

fun encodeToJsonElement(content: T) =
plugins.json.encodeToJsonElement(migrator, content)
plugins.json.encodeToJsonElement(migrator.Migrate(), content)

suspend fun save(file: Fs.File, content: T, allowOverwrite: Boolean = false) =
file.write(
Expand Down
60 changes: 33 additions & 27 deletions src/commonMain/kotlin/baaahs/migrator/DataMigrator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.*

open class DataMigrator<T : Any>(
tSerializer: KSerializer<T>,
private val tSerializer: KSerializer<T>,
private val migrations: List<Migration> = emptyList(),
private val versionKey: String = "version"
) : JsonTransformingSerializer<T>(tSerializer) {
) {
init {
val versionDupes = migrations.groupBy { it.toVersion }
.filter { (_, matching) -> matching.size > 1 }
Expand All @@ -20,39 +20,45 @@ open class DataMigrator<T : Any>(
}
private val currentVersion = migrations.maxOfOrNull { it.toVersion } ?: 0

override fun transformDeserialize(element: JsonElement): JsonElement {
if (element !is JsonObject) return element
val fromVersion = element[versionKey]?.jsonPrimitive?.int ?: 0
if (fromVersion == currentVersion)
return element.toMutableMap()
.apply { remove(versionKey) }.toJsonObj()
inner class Migrate(
fileName: String? = null
) : JsonTransformingSerializer<T>(tSerializer) {
private val context = fileName?.let { "$it: " } ?: ""

var newJson = element
.toMutableMap().apply {
remove(versionKey)
}.toJsonObj()
override fun transformDeserialize(element: JsonElement): JsonElement {
if (element !is JsonObject) return element
val fromVersion = element[versionKey]?.jsonPrimitive?.int ?: 0
if (fromVersion == currentVersion)
return element.toMutableMap()
.apply { remove(versionKey) }.toJsonObj()

var newJson = element
.toMutableMap().apply {
remove(versionKey)
}.toJsonObj()

logger.debug { "Migrating from v$fromVersion:\n$newJson" }
logger.debug { "${context}Migrating from v$fromVersion:\n$newJson" }

migrations.forEach { migration ->
if (fromVersion < migration.toVersion) {
logger.info {
"Migrating from $fromVersion to ${migration.toVersion} (${migration::class.simpleName})."
migrations.forEach { migration ->
if (fromVersion < migration.toVersion) {
logger.info {
"${context}Migrating from $fromVersion to ${migration.toVersion} (${migration::class.simpleName})."
}
newJson = migration.migrate(newJson)
}
newJson = migration.migrate(newJson)
}
}

logger.debug { "Migrated to v$currentVersion:\n$newJson" }
logger.debug { "${context}Migrated to v$currentVersion:\n$newJson" }

return newJson.toJsonObj()
}
return newJson.toJsonObj()
}

override fun transformSerialize(element: JsonElement): JsonElement {
if (element !is JsonObject) return element
return element.toMutableMap().apply {
put(versionKey, JsonPrimitive(currentVersion))
}.toJsonObj()
override fun transformSerialize(element: JsonElement): JsonElement {
if (element !is JsonObject) return element
return element.toMutableMap().apply {
put(versionKey, JsonPrimitive(currentVersion))
}.toJsonObj()
}
}

abstract class Migration(val toVersion: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ object V10_RemoveVarPrefixFromXyPad : DataMigrator.Migration(10) {
override fun migrate(from: JsonObject): JsonObject {
return from.toMutableMap().apply {
mapObjsInDict("feeds") { _, control ->
println("from = $control")
if (control.type == "baaahs.Core:XyPad") {
control.remove("varPrefix")
}
Expand Down
4 changes: 2 additions & 2 deletions src/jsMain/kotlin/baaahs/app/ui/UiActions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ actual object UiActions {
val type = FileType.Show
val filename = "${show.title}${type.extension}"
val contentType = "application/json;charset=utf-8;"
doDownload(filename, show, ShowMigrator, contentType, plugins)
doDownload(filename, show, ShowMigrator.Migrate(filename), contentType, plugins)
}

actual fun downloadScene(scene: Scene, plugins: Plugins) {
val type = FileType.Scene
val filename = "${scene.title}${type.extension}"
val contentType = "application/json;charset=utf-8;"
doDownload(filename, scene, SceneMigrator, contentType, plugins)
doDownload(filename, scene, SceneMigrator.Migrate(filename), contentType, plugins)
}

private fun <T: Any> doDownload(
Expand Down

0 comments on commit 7dfe341

Please sign in to comment.