0.5.2 migrated to kotlin 2.1, optStorage fixed

This commit is contained in:
Sergey Chernov 2025-01-05 13:38:13 +03:00
parent 748f273fb9
commit ea6986dd07
4 changed files with 30 additions and 20 deletions

View File

@ -3,14 +3,14 @@ val kotlin_version: String by project
val logback_version: String by project
plugins {
kotlin("multiplatform") version "1.7.21"
kotlin("plugin.serialization") version "1.7.21"
id("org.jetbrains.dokka") version "1.7.20"
kotlin("multiplatform") version "2.1.0"
kotlin("plugin.serialization") version "2.1.0"
id("org.jetbrains.dokka") version "1.9.20"
`maven-publish`
}
group = "net.sergeych"
version = "0.4.6"
version = "0.5.2"
repositories {
mavenCentral()
@ -24,9 +24,9 @@ kotlin {
}
jvm {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
// compilations.all {
// kotlinOptions.jvmTarget = "1.8"
// }
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
@ -35,7 +35,7 @@ kotlin {
js(IR) {
browser {
commonWebpackConfig {
cssSupport.enabled = true
// cssSupport.enabled = true
}
}
}

View File

@ -1,6 +1,4 @@
kotlin.code.style=official
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false
kotlin.js.generate.executable.default=false
ktor_version=2.1.1
logback_version=1.2.10

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -2,6 +2,10 @@ package net.sergeych.parsec3
import net.sergeych.boss_serialization.BossDecoder
import net.sergeych.boss_serialization_mp.BossEncoder
import net.sergeych.mp_logger.LogTag
import net.sergeych.mp_logger.exception
import net.sergeych.mp_logger.info
import net.sergeych.mptools.encodeToHex
import net.sergeych.mptools.toDump
import kotlin.reflect.KProperty
import kotlin.reflect.KType
@ -58,20 +62,21 @@ interface KVStorage {
}
inline operator fun <reified T> KVStorage.invoke(defaultValue: T,overrideName: String? = null) =
inline operator fun <reified T> KVStorage.invoke(defaultValue: T, overrideName: String? = null) =
KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName)
inline fun <reified T> KVStorage.stored(defaultValue: T, overrideName: String? = null) =
KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName)
inline fun <reified T> KVStorage.optStored(defaultValue: T?=null, overrideName: String? = null) =
KVStorageDelegate<T?>(this, typeOf<T?>(), defaultValue, overrideName)
inline fun <reified T> KVStorage.optStored(defaultValue: T? = null, overrideName: String? = null) =
KVStorageDelegate<T?>(this, typeOf<T>(), defaultValue, overrideName)
class KVStorageDelegate<T>(
private val storage: KVStorage,
private val type: KType,
private val defaultValue: T,
private val overrideName: String? = null,
) {
): LogTag("KVSD") {
private fun name(property: KProperty<*>): String = overrideName ?: property.name
@ -84,17 +89,24 @@ class KVStorageDelegate<T>(
if (data == null)
cachedValue = defaultValue
else
cachedValue = BossDecoder.decodeFrom(type, data)
cachedValue = try {
info { " get ${name(property)}: ${type} [{${data.encodeToHex()}" }
BossDecoder.decodeFrom<T>(type, data)
.also { println("decoded as $it") }
} catch (e: Exception) {
exception { "failed to decode ${name(property)}: ${type}" to e }
defaultValue
}
cacheReady = true
return cachedValue
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
// if (!cacheReady || value != cachedValue) {
cachedValue = value
cacheReady = true
println("set ${name(property)} to ${BossEncoder.encode(type, value).toDump()}")
storage[name(property)] = BossEncoder.encode(type, value)
cachedValue = value
cacheReady = true
println("set ${name(property)} to ${BossEncoder.encode(type, value).toDump()}")
storage[name(property)] = BossEncoder.encode(type, value)
// }
}
}