Skip to content

Commit

Permalink
Ensure version files are created at same path as store (#101)
Browse files Browse the repository at this point in the history
Addresses #99
  • Loading branch information
JanTie authored May 2, 2024
1 parent ccbe447 commit bafbbf1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
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

0 comments on commit bafbbf1

Please sign in to comment.