mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-29 22:27:31 +00:00
refactor: 调整 checkJsonKey 的所在类, 以便于编写测试用例.
通过调整所在类, 可更好的在单元测试中获取方法对象, 进行测试调用.
This commit is contained in:
parent
31366575a9
commit
24f34aa27f
@ -55,8 +55,8 @@ internal object ArtifactSerializer : JsonSerializer<Artifact>, JsonDeserializer<
|
|||||||
return JsonPrimitive(gavBuilder.append(':').append(src.version).toString())
|
return JsonPrimitive(gavBuilder.append(':').append(src.version).toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): Artifact {
|
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Artifact {
|
||||||
if (!json!!.isJsonPrimitive) {
|
if (!json.isJsonPrimitive) {
|
||||||
throw JsonParseException("Wrong configuration value type.")
|
throw JsonParseException("Wrong configuration value type.")
|
||||||
}
|
}
|
||||||
return DefaultArtifact(json.asString.trim())
|
return DefaultArtifact(json.asString.trim())
|
||||||
@ -115,23 +115,31 @@ internal object AuthenticationSerializer : JsonDeserializer<Authentication> {
|
|||||||
|
|
||||||
when (json.get(KEY_TYPE).asString.trim().lowercase()) {
|
when (json.get(KEY_TYPE).asString.trim().lowercase()) {
|
||||||
"string" -> {
|
"string" -> {
|
||||||
builder.addString(checkJsonKey(json, "key"), checkJsonKey(json, "value"))
|
builder.addString(
|
||||||
|
SerializerUtils.checkJsonKey(json, "key"),
|
||||||
|
SerializerUtils.checkJsonKey(json, "value")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
"secret" -> {
|
"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 {
|
private object SerializerUtils {
|
||||||
if (!json.has(key)) {
|
fun checkJsonKey(json: JsonObject, key: String): String {
|
||||||
throw JsonParseException("Required field does not exist: $key")
|
if (!json.has(key)) {
|
||||||
} else if (!json.get(key).isJsonPrimitive) {
|
throw JsonParseException("Required field does not exist: $key")
|
||||||
throw JsonParseException("Wrong field `$key` type: ${json.get(key)::class.java}")
|
} 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
|
internal object MavenRepositoryConfigSerializer
|
||||||
@ -146,7 +154,7 @@ internal object MavenRepositoryConfigSerializer
|
|||||||
is JsonObject -> {
|
is JsonObject -> {
|
||||||
MavenRepositoryConfig(
|
MavenRepositoryConfig(
|
||||||
id = json.get("id")?.asString,
|
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)
|
proxy = if (json.has("proxy") && json.get("proxy").isJsonObject)
|
||||||
context.deserialize<Proxy>(
|
context.deserialize<Proxy>(
|
||||||
json.getAsJsonObject("proxy"), Proxy::class.java
|
json.getAsJsonObject("proxy"), Proxy::class.java
|
||||||
|
Loading…
Reference in New Issue
Block a user