mirror of
https://github.com/revanced/revanced-patcher.git
synced 2025-05-11 09:34:26 +02:00
parent
f30671ddd1
commit
b873228ef0
@ -101,11 +101,13 @@ sealed class PatchOption<T>(
|
|||||||
* Gets the value of the option.
|
* Gets the value of the option.
|
||||||
* Please note that using the wrong value type results in a runtime error.
|
* Please note that using the wrong value type results in a runtime error.
|
||||||
*/
|
*/
|
||||||
inline operator fun <reified V> getValue(thisRef: Any?, property: KProperty<*>) =
|
inline operator fun <reified V> getValue(thisRef: Any?, property: KProperty<*>): V? {
|
||||||
value as? V ?: throw InvalidTypeException(
|
if (value !is V?) throw InvalidTypeException(
|
||||||
V::class.java.canonicalName,
|
V::class.java.canonicalName,
|
||||||
value?.let { it::class.java.canonicalName } ?: "null"
|
value?.let { it::class.java.canonicalName } ?: "null"
|
||||||
)
|
)
|
||||||
|
return value as? V?
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the value of the option.
|
* Gets the value of the option.
|
||||||
|
18
src/test/kotlin/app/revanced/patcher/issues/Issue98.kt
Normal file
18
src/test/kotlin/app/revanced/patcher/issues/Issue98.kt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package app.revanced.patcher.issues
|
||||||
|
|
||||||
|
import app.revanced.patcher.patch.PatchOption
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import kotlin.test.assertNull
|
||||||
|
|
||||||
|
internal class Issue98 {
|
||||||
|
companion object {
|
||||||
|
var key1: String? by PatchOption.StringOption(
|
||||||
|
"key1", null, "title", "description"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should infer nullable type correctly`() {
|
||||||
|
assertNull(key1)
|
||||||
|
}
|
||||||
|
}
|
@ -48,7 +48,7 @@ internal class PatchOptionsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should return a different value when changed`() {
|
fun `should return a different value when changed`() {
|
||||||
var value: String by options["key1"]
|
var value: String? by options["key1"]
|
||||||
val current = value + "" // force a copy
|
val current = value + "" // force a copy
|
||||||
value = "Hello, world!"
|
value = "Hello, world!"
|
||||||
assertNotEquals(current, value)
|
assertNotEquals(current, value)
|
||||||
|
@ -171,32 +171,32 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object : OptionsContainer() {
|
companion object : OptionsContainer() {
|
||||||
private var key1: String by option(
|
private var key1: String? by option(
|
||||||
PatchOption.StringOption(
|
PatchOption.StringOption(
|
||||||
"key1", "default", "title", "description", true
|
"key1", "default", "title", "description", true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
private var key2: Boolean by option(
|
private var key2: Boolean? by option(
|
||||||
PatchOption.BooleanOption(
|
PatchOption.BooleanOption(
|
||||||
"key2", true, "title", "description" // required defaults to false
|
"key2", true, "title", "description" // required defaults to false
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
private var key3: String by option(
|
private var key3: String? by option(
|
||||||
PatchOption.StringListOption(
|
PatchOption.StringListOption(
|
||||||
"key3", "TEST", listOf("TEST", "TEST1", "TEST2"), "title", "description"
|
"key3", "TEST", listOf("TEST", "TEST1", "TEST2"), "title", "description"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
private var key4: Int by option(
|
private var key4: Int? by option(
|
||||||
PatchOption.IntListOption(
|
PatchOption.IntListOption(
|
||||||
"key4", 1, listOf(1, 2, 3), "title", "description"
|
"key4", 1, listOf(1, 2, 3), "title", "description"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
private var key5: Path by option(
|
private var key5: Path? by option(
|
||||||
PatchOption.PathOption(
|
PatchOption.PathOption(
|
||||||
"key5", File("test.txt").toPath(), "title", "description"
|
"key5", File("test.txt").toPath(), "title", "description"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
private var key6: String by option(
|
private var key6: String? by option(
|
||||||
PatchOption.StringOption(
|
PatchOption.StringOption(
|
||||||
"key6", null, "title", "description", true
|
"key6", null, "title", "description", true
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user