mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-21 06:07:01 +08:00
Move KSerializer.map
to mirai-core-utils
This commit is contained in:
parent
4c810ee3ee
commit
7722a5a0b0
@ -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 <T, R> KSerializer<T>.map(
|
||||
resultantDescriptor: SerialDescriptor,
|
||||
crossinline deserialize: T.(T) -> R,
|
||||
crossinline serialize: R.(R) -> T,
|
||||
): KSerializer<R> {
|
||||
return object : KSerializer<R> {
|
||||
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 <T, R> KSerializer<T>.mapPrimitive(
|
||||
serialName: String,
|
||||
crossinline deserialize: (T) -> R,
|
||||
crossinline serialize: R.(R) -> T,
|
||||
): KSerializer<R> {
|
||||
val kind = this@mapPrimitive.descriptor.kind
|
||||
check(kind is PrimitiveKind) { "kind must be PrimitiveKind but found $kind" }
|
||||
return object : KSerializer<R> {
|
||||
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) })
|
||||
}
|
||||
}
|
@ -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.*
|
||||
|
@ -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
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 <T> File.loadNotBlankAs(
|
||||
@ -57,3 +58,32 @@ public fun ClassSerialDescriptorBuilder.takeElementsFrom(descriptor: SerialDescr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public inline fun <T, R> KSerializer<T>.map(
|
||||
resultantDescriptor: SerialDescriptor,
|
||||
crossinline deserialize: T.(T) -> R,
|
||||
crossinline serialize: R.(R) -> T,
|
||||
): KSerializer<R> {
|
||||
return object : KSerializer<R> {
|
||||
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 <T, R> KSerializer<T>.mapPrimitive(
|
||||
serialName: String,
|
||||
crossinline deserialize: (T) -> R,
|
||||
crossinline serialize: R.(R) -> T,
|
||||
): KSerializer<R> {
|
||||
val kind = this@mapPrimitive.descriptor.kind
|
||||
check(kind is PrimitiveKind) { "kind must be PrimitiveKind but found $kind" }
|
||||
return object : KSerializer<R> {
|
||||
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) })
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user