Add serializers for PermissionId

This commit is contained in:
Him188 2020-09-09 23:17:29 +08:00
parent cce8c2db16
commit a881235caa

View File

@ -9,13 +9,17 @@
package net.mamoe.mirai.console.permission
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.Serializer
import kotlinx.serialization.builtins.serializer
import net.mamoe.mirai.console.internal.data.map
/**
* [PermissionId] [Permission] 唯一对应.
*/
@Serializable
@Serializable(with = PermissionId.AsStringSerializer::class)
@ExperimentalPermission
public data class PermissionId(
public val namespace: String,
@ -30,6 +34,14 @@ public data class PermissionId(
}
}
@Serializer(forClass = PermissionId::class)
public object AsClassSerializer
public object AsStringSerializer : KSerializer<PermissionId> by String.serializer().map(
serializer = { it.namespace + ":" + it.id },
deserializer = { it.split(':').let { (namespace, id) -> PermissionId(namespace, id) } }
)
public override fun toString(): String {
return "$namespace:$id"
}