mirror of
https://github.com/ReVanced/revanced-patcher.git
synced 2024-11-10 01:02:22 +01:00
fix: handle option types and nulls properly
This commit is contained in:
parent
1d989abd55
commit
aff4968e6f
3 changed files with 24 additions and 2 deletions
|
@ -81,6 +81,12 @@ sealed class PatchOption<T>(
|
|||
val validator: (T?) -> Boolean
|
||||
) {
|
||||
var value: T? = default
|
||||
get() {
|
||||
if (field == null && required) {
|
||||
throw RequirementNotMetException
|
||||
}
|
||||
return field
|
||||
}
|
||||
set(value) {
|
||||
if (value == null && required) {
|
||||
throw RequirementNotMetException
|
||||
|
@ -95,7 +101,11 @@ sealed class PatchOption<T>(
|
|||
* Gets the value of the option.
|
||||
* Please note that using the wrong value type results in a runtime error.
|
||||
*/
|
||||
operator fun <T> getValue(thisRef: Any?, property: KProperty<*>) = value as T
|
||||
inline operator fun <reified V> getValue(thisRef: Any?, property: KProperty<*>) =
|
||||
value as? V ?: throw InvalidTypeException(
|
||||
V::class.java.canonicalName,
|
||||
value?.let { it::class.java.canonicalName } ?: "null"
|
||||
)
|
||||
|
||||
/**
|
||||
* Gets the value of the option.
|
||||
|
|
|
@ -85,9 +85,16 @@ internal class PatchOptionsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `should fail because of the requirement is not met`() {
|
||||
fun `should fail because the requirement is not met`() {
|
||||
assertThrows<RequirementNotMetException> {
|
||||
options.nullify("key1")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should fail because getting a non-initialized option is illegal`() {
|
||||
assertThrows<RequirementNotMetException> {
|
||||
println(options["key6"].value)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -196,5 +196,10 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
|
|||
"key5", File("test.txt").toPath(), "title", "description"
|
||||
)
|
||||
)
|
||||
private var key6: String by option(
|
||||
PatchOption.StringOption(
|
||||
"key6", null, "title", "description", true
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue