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
4 changed files with 45 additions and 11 deletions

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)
options["key1"] = "Hello, world!"
println(option.value)
@@ -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")
}
@@ -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> {

View File

@@ -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
)