Skip to content

Commit

Permalink
Merge pull request #27 from Astra-Interactive/update-klibs
Browse files Browse the repository at this point in the history
update klibs
  • Loading branch information
makeevrserg authored May 25, 2024
2 parents 38a1718 + 9b76dfb commit 74b2c04
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class KtsTestBenchmark {
fun protoBufKrateBenchmark(): SomeObject = withTempFolder { currentFolder ->
val initialValue = SomeObject()
val krate = BinaryFormatKrate(
default = initialValue,
factory = initialValue,
key = "proto_krate.proto",
folder = currentFolder,
binaryFormat = ProtoBuf,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ object BukkitMutableStorageValue {
path: String
): StateFlowMutableStorageValue<T?> {
return StateFlowMutableStorageValue(
default = null,
saveSettingsValue = { set(path, it) },
loadSettingsValue = { getObject(path, T::class.java).takeIf { contains(path) } }
factory = { null },
saver = { set(path, it) },
loader = { getObject(path, T::class.java).takeIf { contains(path) } }
)
}

inline fun <reified T> FileConfiguration.anyMutableStorageValue(
path: String
): MutableStorageValue<T?> {
return MutableStorageValue(
default = null,
saveSettingsValue = { set(path, it) },
loadSettingsValue = { getObject(path, T::class.java).takeIf { contains(path) } }
factory = { null },
saver = { set(path, it) },
loader = { getObject(path, T::class.java).takeIf { contains(path) } }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MutableStorageValueTest {
storageValue.reset()
assertNull(storageValue.load())
// Test withDefault value is as expected
assertEquals(expectedValue, storageValue.withDefault(expectedValue).load())
assertEquals(expectedValue, storageValue.withDefault { expectedValue }.load())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package ru.astrainteractive.astralibs.krate.core

import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer
import ru.astrainteractive.klibs.kstorage.api.value.ValueFactory
import java.io.File

class BinaryFormatKrate<T>(
binaryFormat: BinaryFormat,
kSerializer: KSerializer<T>,
default: T,
factory: ValueFactory<T>,
key: String,
folder: File,
) : FileKrate<T> by FileKrate.Default(
default = default,
factory = factory,
fileName = key,
folder = folder,
save = save@{ file, value ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.astrainteractive.astralibs.krate.core

import ru.astrainteractive.klibs.kstorage.MutableStorageValue
import ru.astrainteractive.klibs.kstorage.api.MutableStorageValue
import ru.astrainteractive.klibs.kstorage.api.value.ValueFactory
import java.io.File

/**
Expand All @@ -14,24 +15,24 @@ interface FileKrate<T> : MutableStorageValue<T> {
val folder: File

class Default<T>(
val default: T,
val factory: ValueFactory<T>,
override val fileName: String,
override val folder: File,
save: (file: File, value: T) -> Unit,
load: (file: File) -> T
) : FileKrate<T>,
MutableStorageValue<T> by MutableStorageValue(
default = default,
saveSettingsValue = saveSettingsValue@{ value ->
factory = factory,
saver = saver@{ value ->
val file = File(folder, fileName)
file.parentFile.mkdirs()
file.createNewFile()
save.invoke(file, value)
},
loadSettingsValue = loadSettingsValue@{
loader = loader@{
val file = File(folder, fileName)
if (!file.exists()) return@loadSettingsValue default
runCatching { load.invoke(file) }.getOrElse { default }
if (!file.exists()) return@loader factory.create()
runCatching { load.invoke(file) }.getOrElse { factory.create() }
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package ru.astrainteractive.astralibs.krate.core

import ru.astrainteractive.astralibs.encoding.encoder.ObjectEncoder
import ru.astrainteractive.astralibs.encoding.model.EncodedObject
import ru.astrainteractive.klibs.kstorage.api.value.ValueFactory
import java.io.File
import java.io.Serializable

class JBinaryKrate<T : Serializable>(
default: T,
factory: ValueFactory<T>,
objectEncoder: ObjectEncoder,
fileName: String,
folder: File,
) : FileKrate<T> by FileKrate.Default(
default = default,
factory = factory,
folder = folder,
fileName = fileName,
save = save@{ file, value ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.StringFormat
import ru.astrainteractive.astralibs.serialization.StringFormatExt.parse
import ru.astrainteractive.astralibs.serialization.StringFormatExt.writeIntoFile
import ru.astrainteractive.klibs.kstorage.api.value.ValueFactory
import java.io.File

class StringFormatKrate<T>(
stringFormat: StringFormat,
kSerializer: KSerializer<T>,
default: T,
factory: ValueFactory<T>,
fileName: String,
folder: File,
) : FileKrate<T> by FileKrate.Default(
default = default,
factory = factory,
fileName = fileName,
folder = folder,
save = save@{ file, value ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.junit.Test
import ru.astrainteractive.astralibs.krate.core.StringFormatKrate
import ru.astrainteractive.astralibs.krate.util.KrateExt.delete
import ru.astrainteractive.astralibs.serialization.YamlStringFormat
import ru.astrainteractive.klibs.kstorage.update
import java.io.File
import java.util.UUID
import kotlin.random.Random
Expand Down Expand Up @@ -36,7 +37,7 @@ class TextKrateTest {
val storage = StringFormatKrate(
stringFormat = serializer,
kSerializer = TestStorage.serializer(),
default = initial,
factory = { initial },
fileName = UUID.randomUUID().toString(),
folder = folder
)
Expand All @@ -55,7 +56,7 @@ class TextKrateTest {
val storage = StringFormatKrate(
stringFormat = YamlStringFormat(),
kSerializer = TestStorage.serializer(),
default = initial,
factory = { initial },
fileName = "yaml_test_file.yaml",
folder = folder
)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ makeevrserg.java.ktarget=21
# Project
makeevrserg.project.name=AstraLibs
makeevrserg.project.group=ru.astrainteractive.astralibs
makeevrserg.project.version.string=3.5.1
makeevrserg.project.version.string=3.6.0
makeevrserg.project.description=Core utilities for spigot development
makeevrserg.project.developers=makeevrserg|Makeev Roman|makeevrserg@gmail.com
makeevrserg.project.url=https://empireprojekt.ru
Expand Down
6 changes: 3 additions & 3 deletions gradle/klibs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[versions]
# klibs
klibs-gradleplugin = "1.0.0-alpha03" # https://github.com/makeevrserg/gradle-plugin
klibs-kstorage = "1.3.0" # https://github.com/makeevrserg/klibs.kstorage
klibs-mikro = "1.6.0-beta" # https://github.com/makeevrserg/klibs.mikro
klibs-gradleplugin = "1.1.1" # https://github.com/makeevrserg/gradle-plugin
klibs-kstorage = "1.4.0" # https://github.com/makeevrserg/klibs.kstorage
klibs-mikro = "1.7.0" # https://github.com/makeevrserg/klibs.mikro
klibs-kdi = "1.3.2" # https://github.com/makeevrserg/klibs.kdi

[libraries]
Expand Down

0 comments on commit 74b2c04

Please sign in to comment.