Compare commits
6
Commits
40fe234070
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1a5873a0e | ||
|
|
895193ba1c | ||
|
|
ea6986dd07 | ||
|
|
748f273fb9 | ||
|
|
6dfc418cb0 | ||
|
|
8aa4b6bc3d |
No files matched your search
@@ -1,3 +1,5 @@
|
|||||||
.gradle
|
.gradle
|
||||||
.idea
|
.idea
|
||||||
build
|
build
|
||||||
|
.gigaide
|
||||||
|
.kotlin
|
||||||
+9
-9
@@ -3,14 +3,14 @@ val kotlin_version: String by project
|
|||||||
val logback_version: String by project
|
val logback_version: String by project
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
kotlin("multiplatform") version "1.7.21"
|
kotlin("multiplatform") version "2.1.0"
|
||||||
kotlin("plugin.serialization") version "1.7.21"
|
kotlin("plugin.serialization") version "2.1.0"
|
||||||
id("org.jetbrains.dokka") version "1.7.20"
|
id("org.jetbrains.dokka") version "1.9.20"
|
||||||
`maven-publish`
|
`maven-publish`
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "net.sergeych"
|
group = "net.sergeych"
|
||||||
version = "0.4.3-SNAPSHOT"
|
version = "0.5.3"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
@@ -24,9 +24,9 @@ kotlin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jvm {
|
jvm {
|
||||||
compilations.all {
|
// compilations.all {
|
||||||
kotlinOptions.jvmTarget = "1.8"
|
// kotlinOptions.jvmTarget = "1.8"
|
||||||
}
|
// }
|
||||||
withJava()
|
withJava()
|
||||||
testRuns["test"].executionTask.configure {
|
testRuns["test"].executionTask.configure {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
@@ -35,7 +35,7 @@ kotlin {
|
|||||||
js(IR) {
|
js(IR) {
|
||||||
browser {
|
browser {
|
||||||
commonWebpackConfig {
|
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-serialization-json:1.4.1")
|
||||||
|
|
||||||
api("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
|
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:unikrypto:1.2.2-SNAPSHOT")
|
||||||
api("net.sergeych:mp_stools:1.3.2-SNAPSHOT")
|
api("net.sergeych:mp_stools:1.3.2-SNAPSHOT")
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
kotlin.mpp.enableGranularSourceSetsMetadata=true
|
|
||||||
kotlin.native.enableDependencyPropagation=false
|
|
||||||
kotlin.js.generate.executable.default=false
|
kotlin.js.generate.executable.default=false
|
||||||
ktor_version=2.1.1
|
ktor_version=2.1.1
|
||||||
logback_version=1.2.10
|
logback_version=1.2.10
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
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
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
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) {
|
fun <A, R> on(ca: CommandDescriptor<A, R>,overwrite: Boolean=false, block: suspend S.(A) -> R) {
|
||||||
api.on(ca, block)
|
api.on(ca, overwrite, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
val onCancelHandlers = mutableListOf<suspend () -> Unit>()
|
val onCancelHandlers = mutableListOf<suspend () -> Unit>()
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ class CommandDescriptor<A, R>(
|
|||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
suspend operator fun invoke(adapter: Adapter<*>): R = adapter.invokeCommand(this,Unit as A)
|
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) {
|
operator fun <I: WithAdapter>invoke(commandHost: CommandHost<I>, overwrite: Boolean = false,block: suspend I.(A)->R) {
|
||||||
commandHost.on(this, block)
|
commandHost.on(this, overwrite, block)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,10 +34,16 @@ open class CommandHost<T: WithAdapter> {
|
|||||||
private val handlers = mutableMapOf<String, suspend T.(ByteArray) -> ByteArray>()
|
private val handlers = mutableMapOf<String, suspend T.(ByteArray) -> ByteArray>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide implementation for a specific command in type-safe compile-time checked manner. the command
|
* Provide implementation for a specific command in a type-safe compile-time checked manner.
|
||||||
* should be declared with [command] invocation.
|
* 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 ->
|
handlers[ca.name] = {args ->
|
||||||
val decodedArgs = BossDecoder.decodeFrom<A>(ca.ass, args)
|
val decodedArgs = BossDecoder.decodeFrom<A>(ca.ass, args)
|
||||||
BossEncoder.encode(ca.rss, block(decodedArgs))
|
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.BossDecoder
|
||||||
import net.sergeych.boss_serialization_mp.BossEncoder
|
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.KProperty
|
||||||
import kotlin.reflect.KType
|
import kotlin.reflect.KType
|
||||||
import kotlin.reflect.typeOf
|
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)
|
KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName)
|
||||||
|
|
||||||
inline fun <reified T> KVStorage.stored(defaultValue: T, overrideName: String? = null) =
|
inline fun <reified T> KVStorage.stored(defaultValue: T, overrideName: String? = null) =
|
||||||
KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName)
|
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>(
|
class KVStorageDelegate<T>(
|
||||||
private val storage: KVStorage,
|
private val storage: KVStorage,
|
||||||
private val type: KType,
|
private val type: KType,
|
||||||
private val defaultValue: T,
|
private val defaultValue: T,
|
||||||
private val overrideName: String? = null,
|
private val overrideName: String? = null,
|
||||||
) {
|
): LogTag("KVSD") {
|
||||||
|
|
||||||
private fun name(property: KProperty<*>): String = overrideName ?: property.name
|
private fun name(property: KProperty<*>): String = overrideName ?: property.name
|
||||||
|
|
||||||
@@ -84,17 +88,22 @@ class KVStorageDelegate<T>(
|
|||||||
if (data == null)
|
if (data == null)
|
||||||
cachedValue = defaultValue
|
cachedValue = defaultValue
|
||||||
else
|
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
|
cacheReady = true
|
||||||
return cachedValue
|
return cachedValue
|
||||||
}
|
}
|
||||||
|
|
||||||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
|
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
|
||||||
// if (!cacheReady || value != cachedValue) {
|
// if (!cacheReady || value != cachedValue) {
|
||||||
cachedValue = value
|
cachedValue = value
|
||||||
cacheReady = true
|
cacheReady = true
|
||||||
println("set ${name(property)} to ${BossEncoder.encode(type, value).toDump()}")
|
storage[name(property)] = BossEncoder.encode(type, value)
|
||||||
storage[name(property)] = BossEncoder.encode(type, value)
|
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,3 +17,10 @@ class InvalidFrameException(text: String): ParsecException(ExceptionsRegistry.in
|
|||||||
class UnknownCodeException(code: String,message: String?): ParsecException(code, message)
|
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)
|
||||||
@@ -58,6 +58,7 @@ internal class AdapterTest {
|
|||||||
val foo by command<String, String>()
|
val foo by command<String, String>()
|
||||||
val ex by command<String,Unit>()
|
val ex by command<String,Unit>()
|
||||||
val ex2 by command<String,Unit>()
|
val ex2 by command<String,Unit>()
|
||||||
|
val nullableTest by command<Int,String?>()
|
||||||
}
|
}
|
||||||
class ApiS2<T: WithAdapter> : CommandHost<T>() {
|
class ApiS2<T: WithAdapter> : CommandHost<T>() {
|
||||||
// create command `foo` that takes a string argument and
|
// create command `foo` that takes a string argument and
|
||||||
@@ -88,6 +89,9 @@ internal class AdapterTest {
|
|||||||
on(ApiS1.ex2) {
|
on(ApiS1.ex2) {
|
||||||
throw IllegalArgumentException()
|
throw IllegalArgumentException()
|
||||||
}
|
}
|
||||||
|
on(ApiS1.nullableTest) {
|
||||||
|
if( it == 1 ) "one" else null
|
||||||
|
}
|
||||||
onCancel {
|
onCancel {
|
||||||
b1Cancelled = true
|
b1Cancelled = true
|
||||||
}
|
}
|
||||||
@@ -120,6 +124,9 @@ internal class AdapterTest {
|
|||||||
assertEquals("%% loop-42foo %%", a1.invokeCommand(ApiS2<WithAdapter>().loopCall, "123"))
|
assertEquals("%% loop-42foo %%", a1.invokeCommand(ApiS2<WithAdapter>().loopCall, "123"))
|
||||||
assertEquals("32142foo", a2.invokeCommand(ApiS1.foo, "321"))
|
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, "---"))
|
assertEquals("---42foo", ApiS1.foo.invoke(a2, "---"))
|
||||||
val x = assertThrows<IllegalArgumentException> { ApiS1.ex.invoke(a2, "foobar") }
|
val x = assertThrows<IllegalArgumentException> { ApiS1.ex.invoke(a2, "foobar") }
|
||||||
assertEquals("foobar", x.message)
|
assertEquals("foobar", x.message)
|
||||||
@@ -133,5 +140,18 @@ internal class AdapterTest {
|
|||||||
assertTrue { b1Cancelled }
|
assertTrue { b1Cancelled }
|
||||||
assertTrue { b2Cancelled }
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user