Skip to content

Commit

Permalink
feat: improved Patch Options
Browse files Browse the repository at this point in the history
Removed a lot of the type mess. There's still some duplicated code PatchOption.kt, but I'm afraid there's nothing I can do about that. It's not a big deal anyway.
  • Loading branch information
Sculas committed Sep 9, 2022
1 parent c348c1f commit e722e3f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/app/revanced/patcher/patch/Patch.kt
Expand Up @@ -27,7 +27,7 @@ abstract class OptionsContainer {
@Suppress("MemberVisibilityCanBePrivate")
val options = PatchOptions()

protected fun option(opt: PatchOption<*>): PatchOption<*> {
protected fun <T> option(opt: PatchOption<T>): PatchOption<T> {
options.register(opt)
return opt
}
Expand Down
28 changes: 25 additions & 3 deletions src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt
Expand Up @@ -33,16 +33,30 @@ class PatchOptions(vararg options: PatchOption<*>) : Iterable<PatchOption<*>> {
* Get a [PatchOption] by its key.
* @param key The key of the [PatchOption].
*/
@JvmName("getUntyped")
operator fun get(key: String) = register[key] ?: throw NoSuchOptionException(key)

/**
* Get a [PatchOption] by its key.
* @param key The key of the [PatchOption].
*/
inline operator fun <reified T> get(key: String): PatchOption<T> {
val opt = get(key)
if (opt.value !is T) throw InvalidTypeException(
opt.value?.let { it::class.java.canonicalName } ?: "null",
T::class.java.canonicalName
)
return opt as PatchOption<T>
}

/**
* Set the value of a [PatchOption].
* @param key The key of the [PatchOption].
* @param value The value you want it to be.
* Please note that using the wrong value type results in a runtime error.
*/
inline operator fun <reified T> set(key: String, value: T) {
@Suppress("UNCHECKED_CAST") val opt = get(key) as PatchOption<T>
val opt = get<T>(key)
if (opt.value !is T) throw InvalidTypeException(
T::class.java.canonicalName,
opt.value?.let { it::class.java.canonicalName } ?: "null"
Expand Down Expand Up @@ -99,26 +113,34 @@ sealed class PatchOption<T>(
* Gets the value of the option.
* Please note that using the wrong value type results in a runtime error.
*/
inline operator fun <reified V> getValue(thisRef: Any?, property: KProperty<*>): V? {
@JvmName("getValueTyped")
inline operator fun <reified V> getValue(thisRef: Nothing?, property: KProperty<*>): V? {
if (value !is V?) throw InvalidTypeException(
V::class.java.canonicalName,
value?.let { it::class.java.canonicalName } ?: "null"
)
return value as? V?
}

operator fun getValue(thisRef: Any?, property: KProperty<*>) = value

/**
* Gets the value of the option.
* Please note that using the wrong value type results in a runtime error.
*/
inline operator fun <reified V> setValue(thisRef: Any?, property: KProperty<*>, new: V) {
@JvmName("setValueTyped")
inline operator fun <reified V> setValue(thisRef: Nothing?, property: KProperty<*>, new: V) {
if (value !is V) throw InvalidTypeException(
V::class.java.canonicalName,
value?.let { it::class.java.canonicalName } ?: "null"
)
value = new as T
}

operator fun setValue(thisRef: Any?, property: KProperty<*>, new: T?) {
value = new
}

/**
* A [PatchOption] representing a [String].
* @see PatchOption
Expand Down
16 changes: 14 additions & 2 deletions src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt
Expand Up @@ -35,7 +35,9 @@ internal class PatchOptionsTest {
}
}
}
val option = options["key1"]
val option = options.get<String>("key1")
// or: val option: String? by options["key1"]
// then you won't need `.value` every time
println(option.value)
options["key1"] = "Hello, world!"
println(option.value)
Expand All @@ -55,6 +57,9 @@ internal class PatchOptionsTest {
// > options["key2"] = null
// is not possible because Kotlin
// cannot reify the type "Nothing?".
// So we have to do this instead:
options["key2"] = null as Any?
// This is a cleaner replacement for the above:
options.nullify("key2")
}

Expand All @@ -66,12 +71,19 @@ internal class PatchOptionsTest {
}

@Test
fun `should fail because of invalid value type`() {
fun `should fail because of invalid value type when setting an option`() {
assertThrows<InvalidTypeException> {
options["key1"] = 123
}
}

@Test
fun `should fail because of invalid value type when getting an option`() {
assertThrows<InvalidTypeException> {
options.get<Int>("key1")
}
}

@Test
fun `should fail because of an illegal value`() {
assertThrows<IllegalValueException> {
Expand Down
Expand Up @@ -169,27 +169,27 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
}

companion object : OptionsContainer() {
private var key1: String? by option(
private var key1 by option(
PatchOption.StringOption(
"key1", "default", "title", "description", true
)
)
private var key2: Boolean? by option(
private var key2 by option(
PatchOption.BooleanOption(
"key2", true, "title", "description" // required defaults to false
)
)
private var key3: String? by option(
private var key3 by option(
PatchOption.StringListOption(
"key3", "TEST", listOf("TEST", "TEST1", "TEST2"), "title", "description"
)
)
private var key4: Int? by option(
private var key4 by option(
PatchOption.IntListOption(
"key4", 1, listOf(1, 2, 3), "title", "description"
)
)
private var key5: String? by option(
private var key5 by option(
PatchOption.StringOption(
"key5", null, "title", "description", true
)
Expand Down

0 comments on commit e722e3f

Please sign in to comment.