Skip to content

Commit

Permalink
fix: handle option types and nulls properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Sculas committed Sep 8, 2022
1 parent 1d989ab commit aff4968
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Expand Up @@ -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)
}
}
}
Expand Up @@ -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
)
)
}
}

0 comments on commit aff4968

Please sign in to comment.