feat: improved Patch Options

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.
This commit is contained in:
Sculas 2022-09-09 16:10:30 +02:00
parent c348c1f0a0
commit e722e3f4f9
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89
4 changed files with 45 additions and 11 deletions

View File

@ -27,7 +27,7 @@ abstract class OptionsContainer {
@Suppress("MemberVisibilityCanBePrivate") @Suppress("MemberVisibilityCanBePrivate")
val options = PatchOptions() val options = PatchOptions()
protected fun option(opt: PatchOption<*>): PatchOption<*> { protected fun <T> option(opt: PatchOption<T>): PatchOption<T> {
options.register(opt) options.register(opt)
return opt return opt
} }

View File

@ -33,8 +33,22 @@ class PatchOptions(vararg options: PatchOption<*>) : Iterable<PatchOption<*>> {
* Get a [PatchOption] by its key. * Get a [PatchOption] by its key.
* @param key The key of the [PatchOption]. * @param key The key of the [PatchOption].
*/ */
@JvmName("getUntyped")
operator fun get(key: String) = register[key] ?: throw NoSuchOptionException(key) 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]. * Set the value of a [PatchOption].
* @param key The key of the [PatchOption]. * @param key The key of the [PatchOption].
@ -42,7 +56,7 @@ class PatchOptions(vararg options: PatchOption<*>) : Iterable<PatchOption<*>> {
* 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 T> set(key: String, value: T) { 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( if (opt.value !is T) throw InvalidTypeException(
T::class.java.canonicalName, T::class.java.canonicalName,
opt.value?.let { it::class.java.canonicalName } ?: "null" opt.value?.let { it::class.java.canonicalName } ?: "null"
@ -99,7 +113,8 @@ 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<*>): V? { @JvmName("getValueTyped")
inline operator fun <reified V> getValue(thisRef: Nothing?, property: KProperty<*>): V? {
if (value !is 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"
@ -107,11 +122,14 @@ sealed class PatchOption<T>(
return value as? V? return value as? V?
} }
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value
/** /**
* 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> 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( 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"
@ -119,6 +137,10 @@ sealed class PatchOption<T>(
value = new as T value = new as T
} }
operator fun setValue(thisRef: Any?, property: KProperty<*>, new: T?) {
value = new
}
/** /**
* A [PatchOption] representing a [String]. * A [PatchOption] representing a [String].
* @see PatchOption * @see PatchOption

View File

@ -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) println(option.value)
options["key1"] = "Hello, world!" options["key1"] = "Hello, world!"
println(option.value) println(option.value)
@ -55,6 +57,9 @@ internal class PatchOptionsTest {
// > options["key2"] = null // > options["key2"] = null
// is not possible because Kotlin // is not possible because Kotlin
// cannot reify the type "Nothing?". // 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") options.nullify("key2")
} }
@ -66,12 +71,19 @@ internal class PatchOptionsTest {
} }
@Test @Test
fun `should fail because of invalid value type`() { fun `should fail because of invalid value type when setting an option`() {
assertThrows<InvalidTypeException> { assertThrows<InvalidTypeException> {
options["key1"] = 123 options["key1"] = 123
} }
} }
@Test
fun `should fail because of invalid value type when getting an option`() {
assertThrows<InvalidTypeException> {
options.get<Int>("key1")
}
}
@Test @Test
fun `should fail because of an illegal value`() { fun `should fail because of an illegal value`() {
assertThrows<IllegalValueException> { assertThrows<IllegalValueException> {

View File

@ -169,27 +169,27 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
} }
companion object : OptionsContainer() { companion object : OptionsContainer() {
private var key1: String? by option( private var key1 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 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 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 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: String? by option( private var key5 by option(
PatchOption.StringOption( PatchOption.StringOption(
"key5", null, "title", "description", true "key5", null, "title", "description", true
) )