From f4c604bcadf672c9e2252320262dcc348b8df6e4 Mon Sep 17 00:00:00 2001 From: Jan Tiedemann Date: Tue, 30 Apr 2024 21:44:53 +0200 Subject: [PATCH] Ensure version files are created at same path as store Addresses #99 --- kstore-file/api/android/kstore-file.api | 4 ++-- kstore-file/api/desktop/kstore-file.api | 4 ++-- .../xxfast/kstore/file/extensions/KVersionedStore.kt | 8 +++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/kstore-file/api/android/kstore-file.api b/kstore-file/api/android/kstore-file.api index 1112cbd..1d2836d 100644 --- a/kstore-file/api/android/kstore-file.api +++ b/kstore-file/api/android/kstore-file.api @@ -9,8 +9,8 @@ public final class io/github/xxfast/kstore/file/extensions/KVersionedStoreKt { } public final class io/github/xxfast/kstore/file/extensions/VersionedCodec : io/github/xxfast/kstore/Codec { - public fun (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;)V - public synthetic fun (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;Lokio/Path;)V + public synthetic fun (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;Lokio/Path;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun decode (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun encode (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } diff --git a/kstore-file/api/desktop/kstore-file.api b/kstore-file/api/desktop/kstore-file.api index 1112cbd..1d2836d 100644 --- a/kstore-file/api/desktop/kstore-file.api +++ b/kstore-file/api/desktop/kstore-file.api @@ -9,8 +9,8 @@ public final class io/github/xxfast/kstore/file/extensions/KVersionedStoreKt { } public final class io/github/xxfast/kstore/file/extensions/VersionedCodec : io/github/xxfast/kstore/Codec { - public fun (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;)V - public synthetic fun (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;Lokio/Path;)V + public synthetic fun (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;Lokio/Path;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun decode (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun encode (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } 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..83f6c47 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 @@ -24,10 +24,11 @@ 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 + * @param versionPath path to the file that contains the current version of the store * @param migration Migration strategy to use. Defaults * * @return store that contains a value of type [T] @@ -38,9 +39,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) } @@ -56,8 +58,8 @@ public class VersionedCodec( private val json: Json, private val serializer: KSerializer, private val migration: Migration, + private val versionPath: Path = "$file.version".toPath(), // TODO: Save to file metadata instead ): Codec { - private val versionPath: Path = "$${file.name}.version".toPath() // TODO: Save to file metadata instead override suspend fun decode(): T? = try {