diff --git a/app/src/main/kotlin/li/songe/gkd/service/AbExt.kt b/app/src/main/kotlin/li/songe/gkd/service/AbExt.kt index 0ca94f2f..2bba504f 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/AbExt.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/AbExt.kt @@ -15,6 +15,7 @@ import li.songe.selector.UnknownIdentifierException import li.songe.selector.UnknownIdentifierMethodException import li.songe.selector.UnknownMemberException import li.songe.selector.UnknownMemberMethodException +import li.songe.selector.getBooleanInvoke import li.songe.selector.getCharSequenceAttr import li.songe.selector.getCharSequenceInvoke import li.songe.selector.getIntInvoke @@ -345,6 +346,7 @@ fun createCacheTransform(): CacheTransform { is CharSequence -> getCharSequenceInvoke(target, name, args) is Int -> getIntInvoke(target, name, args) + is Boolean -> getBooleanInvoke(target, name, args) else -> null } @@ -520,6 +522,7 @@ fun createTransform(): Transform { is CharSequence -> getCharSequenceInvoke(target, name, args) is Int -> getIntInvoke(target, name, args) + is Boolean -> getBooleanInvoke(target, name, args) else -> null } }, diff --git a/selector/src/commonMain/kotlin/li/songe/selector/util.kt b/selector/src/commonMain/kotlin/li/songe/selector/util.kt index 8695517a..affbc500 100644 --- a/selector/src/commonMain/kotlin/li/songe/selector/util.kt +++ b/selector/src/commonMain/kotlin/li/songe/selector/util.kt @@ -78,6 +78,9 @@ fun initDefaultTypeInfo(): DefaultTypeInfo { val intType = TypeInfo(PrimitiveType.IntType) val stringType = TypeInfo(PrimitiveType.StringType) + booleanType.methods = arrayOf( + MethodInfo("toInt", intType), + ) intType.methods = arrayOf( MethodInfo("toString", stringType), MethodInfo("toString", stringType, arrayOf(intType)), @@ -93,6 +96,7 @@ fun initDefaultTypeInfo(): DefaultTypeInfo { stringType.methods = arrayOf( MethodInfo("get", stringType, arrayOf(intType)), MethodInfo("at", stringType, arrayOf(intType)), + MethodInfo("substring", stringType, arrayOf(intType)), MethodInfo("substring", stringType, arrayOf(intType, intType)), MethodInfo("toInt", intType), MethodInfo("toInt", intType, arrayOf(intType)), @@ -187,8 +191,15 @@ fun getStringInvoke(target: String, name: String, args: List): Any? { return getCharSequenceInvoke(target, name, args) } -fun getCharSequenceInvoke(target: CharSequence, name: String, args: List): Any? { +@JsExport +fun getBooleanInvoke(target: Boolean, name: String, args: List): Any? { + return when (name) { + "toInt" -> if (target) 1 else 0 + else -> null + } +} +fun getCharSequenceInvoke(target: CharSequence, name: String, args: List): Any? { return when (name) { "get" -> { target.getOrNull(args.getIntOrNull() ?: return null).toString() diff --git a/selector/src/jvmTest/kotlin/li/songe/selector/ParserTest.kt b/selector/src/jvmTest/kotlin/li/songe/selector/ParserTest.kt index 3abf533e..a5c1ff92 100644 --- a/selector/src/jvmTest/kotlin/li/songe/selector/ParserTest.kt +++ b/selector/src/jvmTest/kotlin/li/songe/selector/ParserTest.kt @@ -55,6 +55,7 @@ class ParserTest { }, getInvoke = { target, name, args -> when (target) { + is Boolean -> getBooleanInvoke(target, name, args) is Int -> getIntInvoke(target, name, args) is CharSequence -> getCharSequenceInvoke(target, name, args) is TestNode -> getNodeInvoke(target, name, args)