From 7569020f0672f910ce86f5b97696a3a571b499e3 Mon Sep 17 00:00:00 2001 From: HystericalDragon Date: Sat, 9 Nov 2024 07:26:25 +0800 Subject: [PATCH] fix(asMap): call asMap for the first item when using listable fix "fix(asMap): unfold child classes" --- app/src/main/java/io/nekohasekai/sagernet/ktx/Utils.kt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sagernet/ktx/Utils.kt b/app/src/main/java/io/nekohasekai/sagernet/ktx/Utils.kt index 7649b5b1..9d6b9d11 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ktx/Utils.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ktx/Utils.kt @@ -270,7 +270,7 @@ fun Continuation.tryResumeWithException(exception: Throwable) { } fun T.asMap(): MutableMap { - if (!shouldAsMap(this)) throw RuntimeException("invalid type to as map") + if (!shouldAsMap(this)) throw RuntimeException("invalid type to as map: " + javaClass.name) val map = mutableMapOf() @@ -294,7 +294,7 @@ fun T.asMap(): MutableMap { } private fun shouldAsMap(value: Any?): Boolean = when (value) { - null, is String, is Number, is Boolean, is Map<*, *> -> false + null, is String, is Number, is Boolean, is Map<*, *>, is List<*> -> false else -> true } @@ -305,11 +305,7 @@ private fun mappedValue(value: Any?): Any? = when (value) { 0 -> null // Listable - 1 -> if (shouldAsMap(value[0])) { - value.asMap() - } else { - value[0] - } + 1 -> mappedValue(value[0]) else -> { val needAsMap = shouldAsMap(value[0])