mirror of
https://github.com/ReVanced/revanced-patcher.git
synced 2026-01-27 21:21:03 +00:00
feat!: Add property PatchOption#values
This commit is contained in:
@@ -8,6 +8,7 @@ import kotlin.reflect.KProperty
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -18,10 +19,11 @@ import kotlin.reflect.KProperty
|
||||
open class PatchOption<T>(
|
||||
val key: String,
|
||||
val default: T?,
|
||||
val values: Set<T>,
|
||||
val title: String?,
|
||||
val description: String?,
|
||||
val required: Boolean,
|
||||
val validator: (T?) -> Boolean
|
||||
val validator: PatchOption<T>.(T?) -> Boolean
|
||||
) {
|
||||
/**
|
||||
* The value of the [PatchOption].
|
||||
@@ -90,6 +92,7 @@ open class PatchOption<T>(
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -102,17 +105,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.stringPatchOption(
|
||||
key: String,
|
||||
default: String? = null,
|
||||
values: Set<String> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (String?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<String>.(String?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with an integer value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -125,17 +130,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.intPatchOption(
|
||||
key: String,
|
||||
default: Int? = null,
|
||||
values: Set<Int> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Int?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Int?>.(Int?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with a boolean value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -148,17 +155,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.booleanPatchOption(
|
||||
key: String,
|
||||
default: Boolean? = null,
|
||||
values: Set<Boolean> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Boolean?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Boolean?>.(Boolean?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with a float value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -171,17 +180,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.floatPatchOption(
|
||||
key: String,
|
||||
default: Float? = null,
|
||||
values: Set<Float> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Float?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Float?>.(Float?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with a long value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -194,17 +205,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.longPatchOption(
|
||||
key: String,
|
||||
default: Long? = null,
|
||||
values: Set<Long> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Long?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Long?>.(Long?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with a string array value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -217,17 +230,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.stringArrayPatchOption(
|
||||
key: String,
|
||||
default: Array<String>? = null,
|
||||
values: Set<Array<String>> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Array<String>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Array<String>?>.(Array<String>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with an integer array value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -240,17 +255,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.intArrayPatchOption(
|
||||
key: String,
|
||||
default: Array<Int>? = null,
|
||||
values: Set<Array<Int>> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Array<Int>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Array<Int>?>.(Array<Int>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with a boolean array value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -263,17 +280,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.booleanArrayPatchOption(
|
||||
key: String,
|
||||
default: Array<Boolean>? = null,
|
||||
values: Set<Array<Boolean>> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Array<Boolean>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Array<Boolean>?>.(Array<Boolean>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with a float array value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -286,17 +305,19 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.floatArrayPatchOption(
|
||||
key: String,
|
||||
default: Array<Float>? = null,
|
||||
values: Set<Array<Float>> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Array<Float>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Array<Float>?>.(Array<Float>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
/**
|
||||
* Create a new [PatchOption] with a long array value and add it to the current [Patch].
|
||||
*
|
||||
* @param key The identifier.
|
||||
* @param default The default value.
|
||||
* @param values The set of guaranteed valid values.
|
||||
* @param title The title.
|
||||
* @param description A description.
|
||||
* @param required Whether the option is required.
|
||||
@@ -309,11 +330,12 @@ open class PatchOption<T>(
|
||||
fun <P : Patch<*>> P.longArrayPatchOption(
|
||||
key: String,
|
||||
default: Array<Long>? = null,
|
||||
values: Set<Array<Long>> = emptySet(),
|
||||
title: String? = null,
|
||||
description: String? = null,
|
||||
required: Boolean = false,
|
||||
validator: (Array<Long>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, title, description, required, validator).also { registerOption(it) }
|
||||
validator: PatchOption<Array<Long>?>.(Array<Long>?) -> Boolean = { true }
|
||||
) = PatchOption(key, default, values, title, description, required, validator).also { registerOption(it) }
|
||||
|
||||
private fun <P : Patch<*>> P.registerOption(option: PatchOption<*>) = option.also { options.register(it) }
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatc
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
internal class PatchOptionsTest {
|
||||
@Test
|
||||
@@ -46,6 +47,36 @@ internal class PatchOptionsTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@Test
|
||||
fun `should allow setting value from values`() =
|
||||
with(OptionsTestPatch.options["choices"] as PatchOption<String>) {
|
||||
value = values.last()
|
||||
assertTrue(value == "valid")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should allow setting custom value`() {
|
||||
assertDoesNotThrow {
|
||||
OptionsTestPatch.options["choices"] = "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should not allow setting custom value with validation`() =
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
with(OptionsTestPatch.options["validated"] as PatchOption<String>) {
|
||||
// Getter validation
|
||||
assertThrows<PatchOptionException.ValueValidationException> { value }
|
||||
|
||||
// setter validation on incorrect value
|
||||
assertThrows<PatchOptionException.ValueValidationException> { value = "invalid" }
|
||||
|
||||
// Setter validation on correct value
|
||||
assertDoesNotThrow { value = "valid" }
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
private object OptionsTestPatch : BytecodePatch() {
|
||||
private var stringOption by stringPatchOption("string", "default")
|
||||
private var booleanOption by booleanPatchOption("bool", true)
|
||||
@@ -53,6 +84,9 @@ internal class PatchOptionsTest {
|
||||
private var nullDefaultRequiredOption by stringPatchOption("null", null, required = true)
|
||||
|
||||
val stringArrayOption = stringArrayPatchOption("array", arrayOf("1", "2"))
|
||||
val stringOptionWithChoices = stringPatchOption("choices", "value", values = setOf("valid"))
|
||||
|
||||
val validatedOption = stringPatchOption("validated", "default") { it == "valid" }
|
||||
|
||||
override fun execute(context: BytecodeContext) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user