Skip to content

Commit

Permalink
feature: (backup) cqcode serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sgpublic committed Nov 16, 2023
1 parent 160fec1 commit aeef470
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.mystere.serialization.cqcode

import io.github.mystere.util.logger
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.EmptySerializersModule
Expand All @@ -13,6 +14,8 @@ internal val CQCodeJson = Json {
sealed class CQCode(
override val serializersModule: SerializersModule
): StringFormat {
private val log by logger()

@Deprecated(
message = "Use decodeFromString instead.",
replaceWith = ReplaceWith("decodeFromString(string)"),
Expand Down Expand Up @@ -104,10 +107,11 @@ sealed class CQCode(
string.length - 4
})
.takeIf { it != "text" }
?.asCQCodeMessageItemType
?.lowercase()
?: throw SerializationException("Not a valid CQCode: $string")
val args = with(string.substring(type.name.length + 4, string.length - 1)) {
val args = with(string.substring(type.length + 5, string.length - 1)) {
HashMap<String, String>().also {
it["type"] = type
for (item in split(",")) {
if (!item.contains("=")) {
continue
Expand All @@ -117,7 +121,7 @@ sealed class CQCode(
}
}
}
return type.deserialize(CQCodeMessageItemDecoder(
return type.asCQCodeMessageItemType.deserialize(CQCodeMessageItemDecoder(
string, args, serializersModule
))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import io.github.mystere.util.logger
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.*
import kotlinx.serialization.encoding.CompositeDecoder
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.modules.SerializersModule

class CQCodeMessageItemDecoder(
Expand Down Expand Up @@ -39,10 +40,16 @@ class CQCodeMessageItemDecoder(
if (elementIndex < 0) {
throw IllegalStateException("Call beginStructure() first")
}
elementIndex += 1
if (elementIndex >= descriptor.elementsCount) {
return CompositeDecoder.DECODE_DONE
}
name = descriptor.getElementName(elementIndex)
} while (args.containsKey(name))
return elementIndex
if (args.containsKey(name)) {
break
}
elementIndex += 1
} while (true)
return elementIndex++
}

override fun decodeStringElement(descriptor: SerialDescriptor, index: Int): String {
Expand All @@ -56,7 +63,6 @@ class CQCodeMessageItemDecoder(
previousValue: T?
): T {
val name = descriptor.getElementName(index)
log.warn { "decodeSerializableElement: $name" }
return deserializer.deserialize(CQCodeMessageItemDecoder(
args[name]!!, mapOf(), serializersModule
))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package io.github.mystere.serialization.cqcode

import kotlin.test.Test
import kotlin.test.assertIs
import kotlin.test.assertTrue
import kotlin.test.assertEquals

class CQCodeTest {
@Test
fun singleFaceTest() {
val faceId = 142L
val message = fromString("singleFaceTest", "[CQ:face,id=$faceId]")
assertTrue("items not only one") { message.size == 1 }
assertTrue("item not a Face") { message[0].type == CQCodeMessageItem.Type.Face }
assertTrue("Face id not $faceId") { (message[0] as CQCodeMessageItem.Face).id == faceId }
}

private fun fromString(callFrom: String, string: String): CQCodeMessage {
return CQCode.decodeFromString(string).also {
println("fromString<$callFrom>: $it")
fromString("[CQ:face,id=142]") {
assertSize(1)
assertEquals(0, CQCodeMessageItem.Face(
id = 142L
))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.mystere.serialization.cqcode

import kotlin.test.assertTrue


fun fromString(string: String, block: CQCodeMessage.() -> Unit) {
with(CQCode.decodeFromString(string), block)
}

fun CQCodeMessage.assertSize(size: Int) {
kotlin.with(this@assertSize.size) {
assertTrue("items size is not $size, but $this") { this == size }
}
}
fun CQCodeMessage.with(index: Int, block: CQCodeMessageItem.() -> Unit) {
with(this[index], block)
}
inline fun <reified T: CQCodeMessageItem> CQCodeMessageItem.assertEquals(item: T) {
kotlin.test.assertIs<T>(this, "item is not ${T::class.simpleName}, but ${this::class.simpleName}")
kotlin.test.assertEquals(this, item, "expect item is $item, but $this")
}
inline fun <reified T: CQCodeMessageItem> CQCodeMessage.assertEquals(index: Int, item: T) {
with(index) {
assertEquals(item)
}
}
1 change: 1 addition & 0 deletions mystere-util/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kotlin {
val jvmMain by getting {
dependencies {
implementation(mystere.slf4j.api)
implementation(mystere.logback.classic)
}
}

Expand Down
2 changes: 1 addition & 1 deletion mystere/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ kotlin {
// jvm
val jvmMain by getting {
dependencies {
implementation(mystere.logback.classic)

}
}

Expand Down

0 comments on commit aeef470

Please sign in to comment.