From 24f34aa27fa3d3d14f534b58334de34c26e938f1 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 19 May 2022 18:20:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=20checkJsonKey?= =?UTF-8?q?=20=E7=9A=84=E6=89=80=E5=9C=A8=E7=B1=BB,=20=E4=BB=A5=E4=BE=BF?= =?UTF-8?q?=E4=BA=8E=E7=BC=96=E5=86=99=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过调整所在类, 可更好的在单元测试中获取方法对象, 进行测试调用. --- .../src/main/kotlin/util/Serializers.kt | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/scalabot-app/src/main/kotlin/util/Serializers.kt b/scalabot-app/src/main/kotlin/util/Serializers.kt index c471f59..492f257 100644 --- a/scalabot-app/src/main/kotlin/util/Serializers.kt +++ b/scalabot-app/src/main/kotlin/util/Serializers.kt @@ -55,8 +55,8 @@ internal object ArtifactSerializer : JsonSerializer, JsonDeserializer< return JsonPrimitive(gavBuilder.append(':').append(src.version).toString()) } - override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): Artifact { - if (!json!!.isJsonPrimitive) { + override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Artifact { + if (!json.isJsonPrimitive) { throw JsonParseException("Wrong configuration value type.") } return DefaultArtifact(json.asString.trim()) @@ -115,23 +115,31 @@ internal object AuthenticationSerializer : JsonDeserializer { when (json.get(KEY_TYPE).asString.trim().lowercase()) { "string" -> { - builder.addString(checkJsonKey(json, "key"), checkJsonKey(json, "value")) + builder.addString( + SerializerUtils.checkJsonKey(json, "key"), + SerializerUtils.checkJsonKey(json, "value") + ) } "secret" -> { - builder.addSecret(checkJsonKey(json, "key"), checkJsonKey(json, "value")) + builder.addSecret( + SerializerUtils.checkJsonKey(json, "key"), + SerializerUtils.checkJsonKey(json, "value") + ) } } } } -private 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}") +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}") + } + return json.get(key).asString } - return json.get(key).asString } internal object MavenRepositoryConfigSerializer @@ -146,7 +154,7 @@ internal object MavenRepositoryConfigSerializer is JsonObject -> { MavenRepositoryConfig( id = json.get("id")?.asString, - url = URL(checkJsonKey(json, "url")), + url = URL(SerializerUtils.checkJsonKey(json, "url")), proxy = if (json.has("proxy") && json.get("proxy").isJsonObject) context.deserialize( json.getAsJsonObject("proxy"), Proxy::class.java