diff --git a/kstore-file/src/commonMain/kotlin/io/github/xxfast/kstore/file/extensions/KVersionedStore.kt b/kstore-file/src/commonMain/kotlin/io/github/xxfast/kstore/file/extensions/KVersionedStore.kt index 81628e1..8ba6091 100644 --- a/kstore-file/src/commonMain/kotlin/io/github/xxfast/kstore/file/extensions/KVersionedStore.kt +++ b/kstore-file/src/commonMain/kotlin/io/github/xxfast/kstore/file/extensions/KVersionedStore.kt @@ -1,7 +1,7 @@ package io.github.xxfast.kstore.file.extensions -import io.github.xxfast.kstore.KStore import io.github.xxfast.kstore.Codec +import io.github.xxfast.kstore.KStore import io.github.xxfast.kstore.file.utils.FILE_SYSTEM import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.KSerializer @@ -16,7 +16,6 @@ import okio.Path import okio.Path.Companion.toPath import okio.buffer import okio.use - import kotlinx.serialization.json.okio.decodeFromBufferedSource as decode import kotlinx.serialization.json.okio.encodeToBufferedSink as encode @@ -24,7 +23,7 @@ import kotlinx.serialization.json.okio.encodeToBufferedSink as encode * Creates a store with a versioned encoder and decoder * Note: An additional file will be written to manage metadata on the same path with `.version` suffix * - * @param filePath path to the file that is managed by this store + * @param file path to the file that is managed by this store * @param default returns this value if the file is not found. defaults to null * @param enableCache maintain a cache. If set to false, it always reads from disk * @param json Serializer to use. Defaults serializer ignores unknown keys and encodes the defaults @@ -38,9 +37,10 @@ public inline fun storeOf( default: T? = null, enableCache: Boolean = true, json: Json = Json { ignoreUnknownKeys = true; encodeDefaults = true }, + versionPath: Path = "$file.version".toPath(), // TODO: Save to file metadata instead noinline migration: Migration = DefaultMigration(default), ): KStore { - val codec: Codec = VersionedCodec(file, version, json, json.serializersModule.serializer(), migration) + val codec: Codec = VersionedCodec(file, version, json, json.serializersModule.serializer(), migration, versionPath) return KStore(default, enableCache, codec) } @@ -50,14 +50,14 @@ public fun DefaultMigration(default: T?): Migration = { _, _ -> default } public typealias Migration = (version: Int?, JsonElement?) -> T? @OptIn(ExperimentalSerializationApi::class) -public class VersionedCodec( +public class VersionedCodec( private val file: Path, private val version: Int = 0, private val json: Json, private val serializer: KSerializer, private val migration: Migration, -): Codec { - private val versionPath: Path = "$${file.name}.version".toPath() // TODO: Save to file metadata instead + private val versionPath: Path = "$file.version".toPath(), // TODO: Save to file metadata instead +) : Codec { override suspend fun decode(): T? = try {