From 7722a5a0b0c5be04573953d071276c184ebeaeca Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 4 Jul 2021 17:56:04 +0800 Subject: [PATCH] Move `KSerializer.map` to mirai-core-utils --- .../message/MessageSerializersImpl.kt | 40 +++---------------- .../kotlin/message/data/FileMessage.kt | 7 ++-- .../commonMain/kotlin/message/data/Image.kt | 12 ++---- .../kotlin/message/data/UnsupportedMessage.kt | 8 ++-- .../commonMain/kotlin/message/data/Voice.kt | 7 ++-- .../src/commonMain/kotlin/Serialization.kt | 36 +++++++++++++++-- 6 files changed, 52 insertions(+), 58 deletions(-) diff --git a/mirai-core-api/src/commonMain/kotlin/internal/message/MessageSerializersImpl.kt b/mirai-core-api/src/commonMain/kotlin/internal/message/MessageSerializersImpl.kt index 3627e2203..ac472e1fb 100644 --- a/mirai-core-api/src/commonMain/kotlin/internal/message/MessageSerializersImpl.kt +++ b/mirai-core-api/src/commonMain/kotlin/internal/message/MessageSerializersImpl.kt @@ -1,10 +1,10 @@ /* * Copyright 2019-2021 Mamoe Technologies and contributors. * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * - * https://github.com/mamoe/mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.internal.message @@ -12,9 +12,7 @@ package net.mamoe.mirai.internal.message import kotlinx.serialization.KSerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.descriptors.* -import kotlinx.serialization.encoding.Decoder -import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.descriptors.buildClassSerialDescriptor import kotlinx.serialization.modules.PolymorphicModuleBuilder import kotlinx.serialization.modules.SerializersModule import kotlinx.serialization.modules.overwriteWith @@ -23,6 +21,7 @@ import net.mamoe.mirai.Mirai import net.mamoe.mirai.message.MessageSerializers import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.MiraiInternalApi +import net.mamoe.mirai.utils.map import net.mamoe.mirai.utils.takeElementsFrom import kotlin.reflect.KClass import kotlin.reflect.full.allSuperclasses @@ -217,33 +216,4 @@ internal object MessageSerializersImpl : MessageSerializers { override fun registerSerializers(serializersModule: SerializersModule) { serializersModuleField = serializersModule.overwriteWith(serializersModule) } -} - -internal inline fun KSerializer.map( - resultantDescriptor: SerialDescriptor, - crossinline deserialize: T.(T) -> R, - crossinline serialize: R.(R) -> T, -): KSerializer { - return object : KSerializer { - override val descriptor: SerialDescriptor get() = resultantDescriptor - override fun deserialize(decoder: Decoder): R = this@map.deserialize(decoder).let { deserialize(it, it) } - override fun serialize(encoder: Encoder, value: R) = serialize(encoder, value.let { serialize(it, it) }) - } -} - -internal inline fun KSerializer.mapPrimitive( - serialName: String, - crossinline deserialize: (T) -> R, - crossinline serialize: R.(R) -> T, -): KSerializer { - val kind = this@mapPrimitive.descriptor.kind - check(kind is PrimitiveKind) { "kind must be PrimitiveKind but found $kind" } - return object : KSerializer { - override fun deserialize(decoder: Decoder): R = - this@mapPrimitive.deserialize(decoder).let(deserialize) - - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(serialName, kind) - override fun serialize(encoder: Encoder, value: R) = - this@mapPrimitive.serialize(encoder, value.let { serialize(it, it) }) - } } \ No newline at end of file diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/FileMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/FileMessage.kt index da701e987..cbc4ed78f 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/FileMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/FileMessage.kt @@ -1,10 +1,10 @@ /* * Copyright 2019-2021 Mamoe Technologies and contributors. * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * - * https://github.com/mamoe/mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress("NOTHING_TO_INLINE") @@ -20,7 +20,6 @@ import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.FileSupported import net.mamoe.mirai.event.events.MessageEvent -import net.mamoe.mirai.internal.message.map import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode import net.mamoe.mirai.utils.* diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt index d1dbdefef..47c050142 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt @@ -1,10 +1,10 @@ /* * Copyright 2019-2021 Mamoe Technologies and contributors. * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * - * https://github.com/mamoe/mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:JvmMultifileClass @@ -31,18 +31,14 @@ import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact.Companion.sendImage import net.mamoe.mirai.contact.Contact.Companion.uploadImage -import net.mamoe.mirai.internal.message.map -import net.mamoe.mirai.internal.message.mapPrimitive import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.data.Image.Key.IMAGE_ID_REGEX import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_1 import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_2 import net.mamoe.mirai.message.data.Image.Key.queryUrl -import net.mamoe.mirai.utils.ExternalResource +import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage -import net.mamoe.mirai.utils.MiraiInternalApi -import net.mamoe.mirai.utils.safeCast import kotlin.LazyThreadSafetyMode.NONE /** diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/UnsupportedMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/UnsupportedMessage.kt index 8db9647bd..11f63c0b0 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/UnsupportedMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/UnsupportedMessage.kt @@ -1,10 +1,10 @@ /* * Copyright 2019-2021 Mamoe Technologies and contributors. * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * - * https://github.com/mamoe/mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @@ -19,9 +19,9 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.IMirai import net.mamoe.mirai.Mirai -import net.mamoe.mirai.internal.message.map import net.mamoe.mirai.utils.chunkedHexToBytes import net.mamoe.mirai.utils.copy +import net.mamoe.mirai.utils.map import net.mamoe.mirai.utils.toUHexString /** diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt index e559e5128..563e5bcf8 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt @@ -1,10 +1,10 @@ /* * Copyright 2019-2021 Mamoe Technologies and contributors. * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * - * https://github.com/mamoe/mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.message.data @@ -15,7 +15,6 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.Group -import net.mamoe.mirai.internal.message.map import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsVoice diff --git a/mirai-core-utils/src/commonMain/kotlin/Serialization.kt b/mirai-core-utils/src/commonMain/kotlin/Serialization.kt index fb4f91234..e6c01d2ad 100644 --- a/mirai-core-utils/src/commonMain/kotlin/Serialization.kt +++ b/mirai-core-utils/src/commonMain/kotlin/Serialization.kt @@ -15,10 +15,11 @@ package net.mamoe.mirai.utils import kotlinx.serialization.BinaryFormat import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.KSerializer import kotlinx.serialization.StringFormat -import kotlinx.serialization.descriptors.ClassSerialDescriptorBuilder -import kotlinx.serialization.descriptors.SerialDescriptor -import kotlinx.serialization.descriptors.buildClassSerialDescriptor +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder import java.io.File public fun File.loadNotBlankAs( @@ -57,3 +58,32 @@ public fun ClassSerialDescriptorBuilder.takeElementsFrom(descriptor: SerialDescr } } } + +public inline fun KSerializer.map( + resultantDescriptor: SerialDescriptor, + crossinline deserialize: T.(T) -> R, + crossinline serialize: R.(R) -> T, +): KSerializer { + return object : KSerializer { + override val descriptor: SerialDescriptor get() = resultantDescriptor + override fun deserialize(decoder: Decoder): R = this@map.deserialize(decoder).let { deserialize(it, it) } + override fun serialize(encoder: Encoder, value: R) = serialize(encoder, value.let { serialize(it, it) }) + } +} + +public inline fun KSerializer.mapPrimitive( + serialName: String, + crossinline deserialize: (T) -> R, + crossinline serialize: R.(R) -> T, +): KSerializer { + val kind = this@mapPrimitive.descriptor.kind + check(kind is PrimitiveKind) { "kind must be PrimitiveKind but found $kind" } + return object : KSerializer { + override fun deserialize(decoder: Decoder): R = + this@mapPrimitive.deserialize(decoder).let(deserialize) + + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(serialName, kind) + override fun serialize(encoder: Encoder, value: R) = + this@mapPrimitive.serialize(encoder, value.let { serialize(it, it) }) + } +}