From 16655b14cf1e87547b6a0a707f21b19fb5b917ce Mon Sep 17 00:00:00 2001 From: FiLKoNd Date: Sun, 10 Nov 2024 01:03:00 +0300 Subject: [PATCH] 1.0.1 > 1.0.2 --- build.gradle.kts | 6 +- .../config/serializers/AnySerializer.kt | 90 +++++++++++++++++++ .../src/main/resources/plugin.yml | 2 +- 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 paperktlib-config/src/main/kotlin/com/filkond/paperktlib/config/serializers/AnySerializer.kt diff --git a/build.gradle.kts b/build.gradle.kts index 08a7f5e..de237e8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,7 +15,7 @@ subprojects { apply(plugin = "maven-publish") group = "com.filkond" - version = "1.0.1" + version = "1.0.2" repositories { mavenCentral() @@ -78,8 +78,8 @@ subprojects { name = "GitHubPackages" url = uri("https://maven.pkg.github.com/FiLKoNd/paperKtLib") credentials { - username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") - password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN") + username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_USERNAME") + password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") } } } diff --git a/paperktlib-config/src/main/kotlin/com/filkond/paperktlib/config/serializers/AnySerializer.kt b/paperktlib-config/src/main/kotlin/com/filkond/paperktlib/config/serializers/AnySerializer.kt new file mode 100644 index 0000000..23747ba --- /dev/null +++ b/paperktlib-config/src/main/kotlin/com/filkond/paperktlib/config/serializers/AnySerializer.kt @@ -0,0 +1,90 @@ +package com.filkond.paperktlib.config.serializers + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* + +private fun Any?.toJsonPrimitive(): JsonPrimitive { + return when (this) { + null -> JsonNull + is JsonPrimitive -> this + is Boolean -> JsonPrimitive(this) + is Number -> JsonPrimitive(this) + is String -> JsonPrimitive(this) + else -> throw Exception("Unsupported type: ${this::class}") + } +} + +private fun JsonPrimitive.toAnyValue(): Any? { + val content = this.content + if (this.isString) { + return content + } + if (content.equals("null", ignoreCase = true)) { + return null + } + if (content.equals("true", ignoreCase = true)) { + return true + } + if (content.equals("false", ignoreCase = true)) { + return false + } + content.toIntOrNull()?.let { return it } + content.toLongOrNull()?.let { return it } + content.toDoubleOrNull()?.let { return it } + throw Exception("Unknown value:${content}") +} + +object AnyValueSerializer : KSerializer { + private val delegateSerializer = JsonPrimitive.serializer() + override val descriptor = delegateSerializer.descriptor + override fun serialize(encoder: Encoder, value: Any?) { + encoder.encodeSerializableValue(delegateSerializer, value.toJsonPrimitive()) + } + + override fun deserialize(decoder: Decoder): Any? { + val jsonPrimitive = decoder.decodeSerializableValue(delegateSerializer) + return jsonPrimitive.toAnyValue() + } +} + +/** + * Convert Any? to JsonElement + */ +private fun Any?.toJsonElement(): JsonElement { + return when (this) { + null -> JsonNull + is JsonElement -> this + is Boolean -> JsonPrimitive(this) + is Number -> JsonPrimitive(this) + is String -> JsonPrimitive(this) + is Iterable<*> -> JsonArray(this.map { it.toJsonElement() }) + // !!! key simply converted to string + is Map<*, *> -> JsonObject(this.map { it.key.toString() to it.value.toJsonElement() }.toMap()) + // add custom convert + else -> throw Exception("Unknown value ${this::class}=${this}}") + } +} + +private fun JsonElement.toAnyOrNull(): Any? { + return when (this) { + is JsonNull -> null + is JsonPrimitive -> toAnyValue() + is JsonObject -> this.map { it.key to it.value.toAnyOrNull() }.toMap() + is JsonArray -> this.map { it.toAnyOrNull() } + } +} + +object AnySerializer : KSerializer { + private val delegateSerializer = JsonElement.serializer() + override val descriptor = delegateSerializer.descriptor + override fun serialize(encoder: Encoder, value: Any?) { + encoder.encodeSerializableValue(delegateSerializer, value.toJsonElement()) + } + + override fun deserialize(decoder: Decoder): Any? { + val jsonPrimitive = decoder.decodeSerializableValue(delegateSerializer) + return jsonPrimitive.toAnyOrNull() + } +} \ No newline at end of file diff --git a/paperktlib-plugin/src/main/resources/plugin.yml b/paperktlib-plugin/src/main/resources/plugin.yml index 8e15206..1d90ddf 100644 --- a/paperktlib-plugin/src/main/resources/plugin.yml +++ b/paperktlib-plugin/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ -name: PaperKtLib +name: paperKtLib version: "${version}" main: com.filkond.paperktlib.plugin.PaperKtLib author: FiLKoNd