refactor: Reduce duplicate code by using a common function to register options

This commit is contained in:
oSumAtrIX 2023-12-22 03:09:42 +01:00
parent e7dacfba8c
commit 0a482f8c9a
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4

View File

@ -80,6 +80,7 @@ open class PatchOption<T>(
if (!validator(value)) throw PatchOptionException.ValueValidationException(value, this) if (!validator(value)) throw PatchOptionException.ValueValidationException(value, this)
} }
override fun toString() = value.toString() override fun toString() = value.toString()
operator fun getValue( operator fun getValue(
@ -120,7 +121,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<String>.(String?) -> Boolean = { true }, validator: PatchOption<String>.(String?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -129,7 +130,7 @@ open class PatchOption<T>(
required, required,
"String", "String",
validator, validator,
).also { registerOption(it) } )
/** /**
* Create a new [PatchOption] with an integer value and add it to the current [Patch]. * Create a new [PatchOption] with an integer value and add it to the current [Patch].
@ -154,7 +155,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Int?>.(Int?) -> Boolean = { true }, validator: PatchOption<Int?>.(Int?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -163,7 +164,7 @@ open class PatchOption<T>(
required, required,
"Int", "Int",
validator, validator,
).also { registerOption(it) } )
/** /**
* Create a new [PatchOption] with a boolean value and add it to the current [Patch]. * Create a new [PatchOption] with a boolean value and add it to the current [Patch].
@ -188,9 +189,16 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Boolean?>.(Boolean?) -> Boolean = { true }, validator: PatchOption<Boolean?>.(Boolean?) -> Boolean = { true },
) = PatchOption(key, default, values, title, description, required, "Boolean", validator).also { ) = registerNewPatchOption(
registerOption(it) key,
} default,
values,
title,
description,
required,
"Boolean",
validator,
)
/** /**
* Create a new [PatchOption] with a float value and add it to the current [Patch]. * Create a new [PatchOption] with a float value and add it to the current [Patch].
@ -215,7 +223,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Float?>.(Float?) -> Boolean = { true }, validator: PatchOption<Float?>.(Float?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -224,7 +232,7 @@ open class PatchOption<T>(
required, required,
"Float", "Float",
validator, validator,
).also { registerOption(it) } )
/** /**
* Create a new [PatchOption] with a long value and add it to the current [Patch]. * Create a new [PatchOption] with a long value and add it to the current [Patch].
@ -249,7 +257,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Long?>.(Long?) -> Boolean = { true }, validator: PatchOption<Long?>.(Long?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -258,7 +266,7 @@ open class PatchOption<T>(
required, required,
"Long", "Long",
validator, validator,
).also { registerOption(it) } )
/** /**
* Create a new [PatchOption] with a string array value and add it to the current [Patch]. * Create a new [PatchOption] with a string array value and add it to the current [Patch].
@ -283,7 +291,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Array<String>?>.(Array<String>?) -> Boolean = { true }, validator: PatchOption<Array<String>?>.(Array<String>?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -292,7 +300,7 @@ open class PatchOption<T>(
required, required,
"StringArray", "StringArray",
validator, validator,
).also { registerOption(it) } )
/** /**
* Create a new [PatchOption] with an integer array value and add it to the current [Patch]. * Create a new [PatchOption] with an integer array value and add it to the current [Patch].
@ -317,7 +325,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Array<Int>?>.(Array<Int>?) -> Boolean = { true }, validator: PatchOption<Array<Int>?>.(Array<Int>?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -326,7 +334,7 @@ open class PatchOption<T>(
required, required,
"IntArray", "IntArray",
validator, validator,
).also { registerOption(it) } )
/** /**
* Create a new [PatchOption] with a boolean array value and add it to the current [Patch]. * Create a new [PatchOption] with a boolean array value and add it to the current [Patch].
@ -351,7 +359,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Array<Boolean>?>.(Array<Boolean>?) -> Boolean = { true }, validator: PatchOption<Array<Boolean>?>.(Array<Boolean>?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -360,7 +368,7 @@ open class PatchOption<T>(
required, required,
"BooleanArray", "BooleanArray",
validator, validator,
).also { registerOption(it) } )
/** /**
* Create a new [PatchOption] with a float array value and add it to the current [Patch]. * Create a new [PatchOption] with a float array value and add it to the current [Patch].
@ -385,7 +393,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Array<Float>?>.(Array<Float>?) -> Boolean = { true }, validator: PatchOption<Array<Float>?>.(Array<Float>?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -394,7 +402,7 @@ open class PatchOption<T>(
required, required,
"FloatArray", "FloatArray",
validator, validator,
).also { registerOption(it) } )
/** /**
* Create a new [PatchOption] with a long array value and add it to the current [Patch]. * Create a new [PatchOption] with a long array value and add it to the current [Patch].
@ -419,7 +427,7 @@ open class PatchOption<T>(
description: String? = null, description: String? = null,
required: Boolean = false, required: Boolean = false,
validator: PatchOption<Array<Long>?>.(Array<Long>?) -> Boolean = { true }, validator: PatchOption<Array<Long>?>.(Array<Long>?) -> Boolean = { true },
) = PatchOption( ) = registerNewPatchOption(
key, key,
default, default,
values, values,
@ -428,8 +436,42 @@ open class PatchOption<T>(
required, required,
"LongArray", "LongArray",
validator, validator,
).also { registerOption(it) } )
private fun <P : Patch<*>> P.registerOption(option: PatchOption<*>) = option.also { options.register(it) } /**
* Create a new [PatchOption] with a string set 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 identified by their string representation.
* @param title The title.
* @param description A description.
* @param required Whether the option is required.
* @param valueType The type of the option value (to handle type erasure).
* @param validator The function to validate the option value.
*
* @return The created [PatchOption].
*
* @see PatchOption
*/
private fun <P : Patch<*>, T> P.registerNewPatchOption(
key: String,
default: T? = null,
values: Map<String, T?>? = null,
title: String? = null,
description: String? = null,
required: Boolean = false,
valueType: String,
validator: PatchOption<T>.(T?) -> Boolean = { true },
) = PatchOption(
key,
default,
values,
title,
description,
required,
valueType,
validator,
).also(options::register)
} }
} }