Compare commits

...
6 Commits
Author SHA1 Message Date
sergeych e1a5873a0e less noise 2025-01-06 21:09:48 +03:00
sergeych 895193ba1c more ignores 2025-01-05 13:39:30 +03:00
sergeych ea6986dd07 0.5.2 migrated to kotlin 2.1, optStorage fixed 2025-01-05 13:38:13 +03:00
sergeych 748f273fb9 version bump 2023-07-23 20:39:19 +01:00
sergeych 6dfc418cb0 version bump 2023-07-21 16:06:41 +01:00
sergeych 8aa4b6bc3d 0.4.3-SNAPSHOT: disallow unintentionally overriding command implementations 2023-07-21 11:59:23 +01:00
10 changed files with 77 additions and 32 deletions

No files matched your search

+2
View File
@@ -1,3 +1,5 @@
.gradle
.idea
build
.gigaide
.kotlin
+9 -9
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.3-SNAPSHOT"
version = "0.5.3"
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
}
}
}
@@ -53,7 +53,7 @@ kotlin {
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1")
api("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
api("net.sergeych:boss-serialization-mp:0.2.4-SNAPSHOT")
api("net.sergeych:boss-serialization-mp:0.2.9")
api("net.sergeych:unikrypto:1.2.2-SNAPSHOT")
api("net.sergeych:mp_stools:1.3.2-SNAPSHOT")
-2
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
+1 -1
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
@@ -25,10 +25,13 @@ class AdapterBuilder<S : WithAdapter, H : CommandHost<S>>(
}
/**
* Register command implementation
* Register command implementation.
* @param ca command to implement
* @param overwrite allow replacing existing command implementation with a new block
* @param block command handler that implements the command
*/
fun <A, R> on(ca: CommandDescriptor<A, R>, block: suspend S.(A) -> R) {
api.on(ca, block)
fun <A, R> on(ca: CommandDescriptor<A, R>,overwrite: Boolean=false, block: suspend S.(A) -> R) {
api.on(ca, overwrite, block)
}
val onCancelHandlers = mutableListOf<suspend () -> Unit>()
@@ -13,8 +13,8 @@ class CommandDescriptor<A, R>(
@Suppress("UNCHECKED_CAST")
suspend operator fun invoke(adapter: Adapter<*>): R = adapter.invokeCommand(this,Unit as A)
operator fun <I: WithAdapter>invoke(commandHost: CommandHost<I>, block: suspend I.(A)->R) {
commandHost.on(this, block)
operator fun <I: WithAdapter>invoke(commandHost: CommandHost<I>, overwrite: Boolean = false,block: suspend I.(A)->R) {
commandHost.on(this, overwrite, block)
}
}
@@ -34,10 +34,16 @@ open class CommandHost<T: WithAdapter> {
private val handlers = mutableMapOf<String, suspend T.(ByteArray) -> ByteArray>()
/**
* Provide implementation for a specific command in type-safe compile-time checked manner. the command
* should be declared with [command] invocation.
* Provide implementation for a specific command in a type-safe compile-time checked manner.
* The command should be declared with [command] invocation.
*
* Normally, existing command can't be replaced by new handlers and attempt to do so will throw
* [DuplicateCommandDefinition] at runtime. If it is done intentionally, set [overwrite] to
* true.
*/
fun <A, R> on(ca: CommandDescriptor<A, R>, block: suspend T.(A) -> R) {
fun <A, R> on(ca: CommandDescriptor<A, R>, overwrite: Boolean = false, block: suspend T.(A) -> R) {
if( ca.name in handlers && !overwrite )
throw DuplicateCommandDefinition("command already defined: ${ca.name}")
handlers[ca.name] = {args ->
val decodedArgs = BossDecoder.decodeFrom<A>(ca.ass, args)
BossEncoder.encode(ca.rss, block(decodedArgs))
@@ -2,7 +2,10 @@ package net.sergeych.parsec3
import net.sergeych.boss_serialization.BossDecoder
import net.sergeych.boss_serialization_mp.BossEncoder
import net.sergeych.mptools.toDump
import net.sergeych.mp_logger.LogTag
import net.sergeych.mp_logger.exception
import net.sergeych.mp_logger.info
import net.sergeych.mptools.encodeToHex
import kotlin.reflect.KProperty
import kotlin.reflect.KType
import kotlin.reflect.typeOf
@@ -58,20 +61,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 +88,22 @@ 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)
} 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
storage[name(property)] = BossEncoder.encode(type, value)
// }
}
}
@@ -16,4 +16,11 @@ class InvalidFrameException(text: String): ParsecException(ExceptionsRegistry.in
class UnknownCodeException(code: String,message: String?): ParsecException(code, message)
class UnknownException(message: String?): ParsecException(ExceptionsRegistry.unknownErrorCode, message)
class UnknownException(message: String?): ParsecException(ExceptionsRegistry.unknownErrorCode, message)
/**
* This exception is server-side, not intended to arise while processing commands, so it is not
* a parsec exception. It means some adapter is trying to redefine existing command, which is
* generally a fault.
*/
class DuplicateCommandDefinition(message: String) : RuntimeException(message)
+21 -1
View File
@@ -58,6 +58,7 @@ internal class AdapterTest {
val foo by command<String, String>()
val ex by command<String,Unit>()
val ex2 by command<String,Unit>()
val nullableTest by command<Int,String?>()
}
class ApiS2<T: WithAdapter> : CommandHost<T>() {
// create command `foo` that takes a string argument and
@@ -88,6 +89,9 @@ internal class AdapterTest {
on(ApiS1.ex2) {
throw IllegalArgumentException()
}
on(ApiS1.nullableTest) {
if( it == 1 ) "one" else null
}
onCancel {
b1Cancelled = true
}
@@ -120,6 +124,9 @@ internal class AdapterTest {
assertEquals("%% loop-42foo %%", a1.invokeCommand(ApiS2<WithAdapter>().loopCall, "123"))
assertEquals("32142foo", a2.invokeCommand(ApiS1.foo, "321"))
assertEquals("one", a2.invokeCommand(ApiS1.nullableTest,1))
assertEquals(null, a2.invokeCommand(ApiS1.nullableTest,2))
assertEquals("---42foo", ApiS1.foo.invoke(a2, "---"))
val x = assertThrows<IllegalArgumentException> { ApiS1.ex.invoke(a2, "foobar") }
assertEquals("foobar", x.message)
@@ -133,5 +140,18 @@ internal class AdapterTest {
assertTrue { b1Cancelled }
assertTrue { b2Cancelled }
}
@Test
fun builderDupeErrorTest() = runTest {
assertThrows<DuplicateCommandDefinition> {
AdapterBuilder(ApiS1) {
newSession { TestSession("42") }
on(api.foo) {
it + buzz + "foo"
}
on(api.foo) {
it + buzz + "foo"
}
}
}
}
}