From 9ed55204c0f57f742f3f846518cd6e7b2135a391 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sun, 3 Jul 2022 01:35:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(config):=20=E8=B0=83=E6=95=B4=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E5=B7=A5=E5=85=B7=E7=B1=BB,=20=E4=BE=BF?= =?UTF-8?q?=E4=BA=8E=E8=BF=9B=E8=A1=8C=E6=B5=8B=E8=AF=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正序列化工具类的类名, 并调整访问权为 internal. 同时将 `checkJsonKey` 改成更方便的 `JsonObject.getPrimitiveValueOrThrow`. --- .../src/main/kotlin/serializer/Serializer.kt | 21 ++++--- ...alizersKtTest.kt => SerializeUtilsTest.kt} | 62 +++++++------------ 2 files changed, 35 insertions(+), 48 deletions(-) rename scalabot-meta/src/test/kotlin/serializer/{SerializersKtTest.kt => SerializeUtilsTest.kt} (95%) diff --git a/scalabot-meta/src/main/kotlin/serializer/Serializer.kt b/scalabot-meta/src/main/kotlin/serializer/Serializer.kt index 9899b96..9b55257 100644 --- a/scalabot-meta/src/main/kotlin/serializer/Serializer.kt +++ b/scalabot-meta/src/main/kotlin/serializer/Serializer.kt @@ -3,6 +3,7 @@ package net.lamgc.scalabot.config.serializer import com.google.gson.* import com.google.gson.reflect.TypeToken import net.lamgc.scalabot.config.* +import net.lamgc.scalabot.config.serializer.SerializeUtils.getPrimitiveValueOrThrow import org.eclipse.aether.artifact.AbstractArtifact import org.eclipse.aether.artifact.Artifact import org.eclipse.aether.artifact.DefaultArtifact @@ -81,8 +82,8 @@ object AuthenticationSerializer : JsonDeserializer { if (json !is JsonObject) { throw JsonParseException("Unsupported JSON type.") } - val username = SerializerUtils.checkJsonKey(json, "username") - val password = SerializerUtils.checkJsonKey(json, "password") + val username = json.getPrimitiveValueOrThrow("username").asString + val password = json.getPrimitiveValueOrThrow("password").asString val builder = AuthenticationBuilder() builder.addUsername(username) builder.addPassword(password) @@ -91,14 +92,14 @@ object AuthenticationSerializer : JsonDeserializer { } -private object SerializerUtils { - fun checkJsonKey(json: JsonObject, key: String): String { - if (!json.has(key)) { - throw JsonParseException("Required field does not exist: $key") - } else if (!json.get(key).isJsonPrimitive) { - throw JsonParseException("Wrong field `$key` type: ${json.get(key)::class.java}") +internal object SerializeUtils { + + fun JsonObject.getPrimitiveValueOrThrow(fieldName: String): JsonPrimitive { + val value = get(fieldName) ?: throw JsonParseException("Missing `$fieldName` field.") + if (value !is JsonPrimitive) { + throw JsonParseException("Invalid `account` field type.") } - return json.get(key).asString + return value } } @@ -114,7 +115,7 @@ object MavenRepositoryConfigSerializer is JsonObject -> { MavenRepositoryConfig( id = json.get("id")?.asString, - url = URL(SerializerUtils.checkJsonKey(json, "url")), + url = URL(json.getPrimitiveValueOrThrow("url").asString), proxy = if (json.has("proxy")) context.deserialize( json.get("proxy"), Proxy::class.java diff --git a/scalabot-meta/src/test/kotlin/serializer/SerializersKtTest.kt b/scalabot-meta/src/test/kotlin/serializer/SerializeUtilsTest.kt similarity index 95% rename from scalabot-meta/src/test/kotlin/serializer/SerializersKtTest.kt rename to scalabot-meta/src/test/kotlin/serializer/SerializeUtilsTest.kt index c5cd5dd..6029775 100644 --- a/scalabot-meta/src/test/kotlin/serializer/SerializersKtTest.kt +++ b/scalabot-meta/src/test/kotlin/serializer/SerializeUtilsTest.kt @@ -5,6 +5,7 @@ import io.mockk.every import io.mockk.mockk import io.mockk.verify import net.lamgc.scalabot.config.* +import net.lamgc.scalabot.config.serializer.SerializeUtils.getPrimitiveValueOrThrow import org.eclipse.aether.artifact.Artifact import org.eclipse.aether.artifact.DefaultArtifact import org.eclipse.aether.repository.Authentication @@ -12,54 +13,39 @@ import org.eclipse.aether.repository.AuthenticationContext import org.eclipse.aether.repository.Proxy import org.intellij.lang.annotations.Language import org.junit.jupiter.api.Assertions.assertThrows -import java.lang.reflect.InvocationTargetException -import java.lang.reflect.Method import java.lang.reflect.Type import java.net.URL import kotlin.test.* -internal class SerializersKtTest { - - private val instance: Any - private val method: Method - - init { - val clazz = Class.forName("net.lamgc.scalabot.config.serializer.SerializerUtils") - method = clazz.getDeclaredMethod("checkJsonKey", JsonObject::class.java, String::class.java) - method.isAccessible = true - instance = clazz.getDeclaredField("INSTANCE").apply { - isAccessible = true - }.get(null) - } - - private fun invoke(json: JsonObject, key: String): String { - try { - return method.invoke(instance, json, key) as String - } catch (e: InvocationTargetException) { - throw e.targetException - } - } +internal class SerializeUtilsTest { @Test - fun `Json key checker test`() { + fun `getPrimitiveValueOrThrow test`() { assertThrows(JsonParseException::class.java) { - invoke(JsonObject(), "NOT_EXIST_KEY") - } - assertThrows(JsonParseException::class.java) { - invoke(JsonObject().apply { add("NULL_KEY", JsonNull.INSTANCE) }, "NULL_KEY") - } - assertThrows(JsonParseException::class.java) { - invoke(JsonObject().apply { add("ARRAY_KEY", JsonArray()) }, "ARRAY_KEY") - } - assertThrows(JsonParseException::class.java) { - invoke(JsonObject().apply { add("OBJECT_KEY", JsonObject()) }, "OBJECT_KEY") + JsonObject().getPrimitiveValueOrThrow("NOT_EXIST_KEY") } - val expectKey = "TEST" - val expectString = "testString" - val json = JsonObject().apply { addProperty(expectKey, expectString) } + assertThrows(JsonParseException::class.java) { + JsonObject().apply { + add("testKey", JsonArray()) + }.getPrimitiveValueOrThrow("testKey") + } + assertThrows(JsonParseException::class.java) { + JsonObject().apply { + add("testKey", JsonObject()) + }.getPrimitiveValueOrThrow("testKey") + } + assertThrows(JsonParseException::class.java) { + JsonObject().apply { + add("testKey", JsonNull.INSTANCE) + }.getPrimitiveValueOrThrow("testKey") + } - assertEquals(expectString, invoke(json, expectKey)) + val expectKey = "STRING_KEY" + val expectValue = JsonPrimitive("A STRING") + assertEquals(expectValue, JsonObject() + .apply { add(expectKey, expectValue) } + .getPrimitiveValueOrThrow(expectKey)) } }