From a49efda337cbf885183715887e82eb286c23392f Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 18 Sep 2020 12:37:40 +0800 Subject: [PATCH] Add serializer for SemVersion --- .../kotlin/net/mamoe/mirai/console/util/SemVersion.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt index 9063f226f..783becc0c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt @@ -14,8 +14,11 @@ package net.mamoe.mirai.console.util +import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.Transient +import kotlinx.serialization.builtins.serializer +import net.mamoe.mirai.console.internal.data.map import net.mamoe.mirai.console.internal.util.SemVersionInternal import net.mamoe.mirai.console.util.SemVersion.Companion.equals @@ -44,7 +47,7 @@ import net.mamoe.mirai.console.util.SemVersion.Companion.equals * 但是不允许 0.0.0.0 之类的存在 * */ -@Serializable +@Serializable(with = SemVersion.SemVersionAsStringSerializer::class) public data class SemVersion internal constructor( /** 核心版本号, 由主版本号, 次版本号和修订号组成, 其中修订号不一定存在 */ public val mainVersion: IntArray, @@ -62,6 +65,11 @@ public data class SemVersion internal constructor( public fun check(version: SemVersion): Boolean } + public object SemVersionAsStringSerializer : KSerializer by String.serializer().map( + serializer = { it.toString() }, + deserializer = { parse(it) } + ) + public companion object { private val SEM_VERSION_REGEX = """^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$""".toRegex()