Skip to content

Commit

Permalink
fix(asMap): call asMap for the first item when using listable
Browse files Browse the repository at this point in the history
fix "fix(asMap): unfold child classes"
  • Loading branch information
xchacha20-poly1305 committed Nov 8, 2024
1 parent 2e19ebc commit 7569020
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions app/src/main/java/io/nekohasekai/sagernet/ktx/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fun <T> Continuation<T>.tryResumeWithException(exception: Throwable) {
}

fun <T : Any> T.asMap(): MutableMap<String, Any> {
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<String, Any>()

Expand All @@ -294,7 +294,7 @@ fun <T : Any> T.asMap(): MutableMap<String, Any> {
}

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
}

Expand All @@ -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])
Expand Down

0 comments on commit 7569020

Please sign in to comment.