Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure version files are created at same path as store #101

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kstore-file/api/android/kstore-file.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;)V
public synthetic fun <init> (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;Lokio/Path;)V
public synthetic fun <init> (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;
}
Expand Down
4 changes: 2 additions & 2 deletions kstore-file/api/desktop/kstore-file.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;)V
public synthetic fun <init> (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lokio/Path;ILkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function2;Lokio/Path;)V
public synthetic fun <init> (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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -38,9 +39,10 @@ public inline fun <reified T : @Serializable Any> 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<T> = DefaultMigration(default),
): KStore<T> {
val codec: Codec<T> = VersionedCodec(file, version, json, json.serializersModule.serializer(), migration)
val codec: Codec<T> = VersionedCodec(file, version, json, json.serializersModule.serializer(), migration, versionPath)
return KStore(default, enableCache, codec)
}

Expand All @@ -56,8 +58,8 @@ public class VersionedCodec<T: @Serializable Any>(
private val json: Json,
private val serializer: KSerializer<T>,
private val migration: Migration<T>,
private val versionPath: Path = "$file.version".toPath(), // TODO: Save to file metadata instead
): Codec<T> {
private val versionPath: Path = "$${file.name}.version".toPath() // TODO: Save to file metadata instead

override suspend fun decode(): T? =
try {
Expand Down
Loading