feat: add method

This commit is contained in:
lisonge
2024-07-02 21:25:28 +08:00
parent d1c592b685
commit 51801a8cb5
3 changed files with 16 additions and 1 deletions

View File

@@ -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<AccessibilityNodeInfo> {
is CharSequence -> getCharSequenceInvoke(target, name, args)
is Int -> getIntInvoke(target, name, args)
is Boolean -> getBooleanInvoke(target, name, args)
else -> null
}
},

View File

@@ -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?>): Any? {
return getCharSequenceInvoke(target, name, args)
}
fun getCharSequenceInvoke(target: CharSequence, name: String, args: List<Any?>): Any? {
@JsExport
fun getBooleanInvoke(target: Boolean, name: String, args: List<Any?>): Any? {
return when (name) {
"toInt" -> if (target) 1 else 0
else -> null
}
}
fun getCharSequenceInvoke(target: CharSequence, name: String, args: List<Any?>): Any? {
return when (name) {
"get" -> {
target.getOrNull(args.getIntOrNull() ?: return null).toString()

View File

@@ -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)