2
0
Эх сурвалжийг харах

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

Him188 5 жил өмнө
parent
commit
40dd786f99

+ 18 - 5
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/contentToString.kt

@@ -142,9 +142,19 @@ private val KProperty1<*, *>.isConst: Boolean
 private val KClass<*>.isData: Boolean
     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(
     prefix: String,
-    filter: ((name: String, value: Any?) -> Boolean)? = null
+    filter: ((name: String, value: Any?) -> Boolean)? = null,
 ): String {
     val newPrefix = "$prefix    "
     return (this::class.simpleName ?: "<UnnamedClass>") + "#" + this::class.hashCode() + " {\n" +
@@ -165,10 +175,13 @@ private fun Any.contentToStringReflectively(
                 .joinToStringPrefixed(
                     prefix = newPrefix
                 ) { (name: String, value: Any?) ->
-                    "$name=" + kotlin.runCatching {
-                        if (value == this) "<this>"
-                        else value._miraiContentToString(newPrefix)
-                    }.getOrElse { "<!>" }
+                    if (value.canBeIgnored()) ""
+                    else {
+                        "$name=" + kotlin.runCatching {
+                            if (value == this) "<this>"
+                            else value._miraiContentToString(newPrefix)
+                        }.getOrElse { "<!>" }
+                    }
                 } + "\n$prefix}"
 }