Commit 40dd786f authored by Him188's avatar Him188

Ignore empty string and arrays and 0s in `_miraiContentToString`

parent 28f48e5c
...@@ -142,9 +142,19 @@ private val KProperty1<*, *>.isConst: Boolean ...@@ -142,9 +142,19 @@ private val KProperty1<*, *>.isConst: Boolean
private val KClass<*>.isData: Boolean private val KClass<*>.isData: Boolean
get() = false // on JVM, it will be resolved to member function get() = false // on JVM, it will be resolved to member function
private fun Any.canBeIgnored(): Boolean {
return when (this) {
is String -> this.isEmpty()
is ByteArray -> this.isEmpty()
is Array<*> -> this.isEmpty()
is Number -> this == 0
else -> false
}
}
private fun Any.contentToStringReflectively( private fun Any.contentToStringReflectively(
prefix: String, prefix: String,
filter: ((name: String, value: Any?) -> Boolean)? = null filter: ((name: String, value: Any?) -> Boolean)? = null,
): String { ): String {
val newPrefix = "$prefix " val newPrefix = "$prefix "
return (this::class.simpleName ?: "<UnnamedClass>") + "#" + this::class.hashCode() + " {\n" + return (this::class.simpleName ?: "<UnnamedClass>") + "#" + this::class.hashCode() + " {\n" +
...@@ -165,10 +175,13 @@ private fun Any.contentToStringReflectively( ...@@ -165,10 +175,13 @@ private fun Any.contentToStringReflectively(
.joinToStringPrefixed( .joinToStringPrefixed(
prefix = newPrefix prefix = newPrefix
) { (name: String, value: Any?) -> ) { (name: String, value: Any?) ->
"$name=" + kotlin.runCatching { if (value.canBeIgnored()) ""
if (value == this) "<this>" else {
else value._miraiContentToString(newPrefix) "$name=" + kotlin.runCatching {
}.getOrElse { "<!>" } if (value == this) "<this>"
else value._miraiContentToString(newPrefix)
}.getOrElse { "<!>" }
}
} + "\n$prefix}" } + "\n$prefix}"
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment