Add TextParameter annotation

This commit is contained in:
Edwin Jakobs
2020-02-02 21:35:39 +01:00
parent bba745bb4a
commit 86584f7fcf
3 changed files with 44 additions and 20 deletions

View File

@@ -7,7 +7,8 @@ Currently orx-parameters supplies the following annotations:
- `DoubleParameter` - `DoubleParameter`
- `IntParameter` - `IntParameter`
- `DoubleParameter` - `BooleanParameter`
- `TextParameter`
## Annotation application ## Annotation application

View File

@@ -11,7 +11,7 @@ import kotlin.reflect.full.findAnnotation
annotation class Description(val title: String, val description: String = "") annotation class Description(val title: String, val description: String = "")
/** /**
* DoubleParameter annotation for a double precision Filter parameter * DoubleParameter annotation for a double precision parameter
* @property label a short description of the parameter * @property label a short description of the parameter
* @property low the lowest value this parameter should be assigned * @property low the lowest value this parameter should be assigned
* @property high the highest value this parameter should be assigned * @property high the highest value this parameter should be assigned
@@ -23,7 +23,7 @@ annotation class Description(val title: String, val description: String = "")
annotation class DoubleParameter(val label: String, val low: Double, val high: Double, val precision: Int = 3, val order: Int = Integer.MAX_VALUE) annotation class DoubleParameter(val label: String, val low: Double, val high: Double, val precision: Int = 3, val order: Int = Integer.MAX_VALUE)
/** /**
* IntParameter annotation for an integer Filter parameter * IntParameter annotation for an integer parameter
* @property label a short description of the parameter * @property label a short description of the parameter
* @property low the lowest value this parameter should be assigned * @property low the lowest value this parameter should be assigned
* @property high the highest value this parameter should be assigned * @property high the highest value this parameter should be assigned
@@ -34,7 +34,7 @@ annotation class DoubleParameter(val label: String, val low: Double, val high: D
annotation class IntParameter(val label: String, val low: Int, val high: Int, val order: Int = Integer.MAX_VALUE) annotation class IntParameter(val label: String, val low: Int, val high: Int, val order: Int = Integer.MAX_VALUE)
/** /**
* BooleanParameter annotation for an integer Filter parameter * BooleanParameter annotation for a boolean parameter
* @property label a short description of the parameter * @property label a short description of the parameter
* @property order hint for where to place the parameter in user interfaces * @property order hint for where to place the parameter in user interfaces
*/ */
@@ -42,9 +42,18 @@ annotation class IntParameter(val label: String, val low: Int, val high: Int, va
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME)
annotation class BooleanParameter(val label: String, val order: Int = Integer.MAX_VALUE) annotation class BooleanParameter(val label: String, val order: Int = Integer.MAX_VALUE)
/**
* Text annotation for a text parameter
* @property label a short description of the parameter
* @property order hint for where to place the parameter in user interfaces
*/
@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
annotation class TextParameter(val label: String, val order: Int = Integer.MAX_VALUE)
/** /**
* ButtonParameter annotation for an integer Filter parameter * ButtonParameter annotation for button parameter
* @property label a short description of the parameter * @property label a short description of the parameter
* @property order hint for where to place the parameter in user interfaces * @property order hint for where to place the parameter in user interfaces
*/ */
@@ -56,16 +65,18 @@ enum class ParameterType(val annotation: KClass<out Annotation>) {
Double(DoubleParameter::class), Double(DoubleParameter::class),
Int(IntParameter::class), Int(IntParameter::class),
Boolean(BooleanParameter::class), Boolean(BooleanParameter::class),
Button(ButtonParameter::class) Button(ButtonParameter::class),
Text(TextParameter::class)
; ;
companion object { companion object {
fun forParameterAnnotationClass(annotation: Annotation) : ParameterType { fun forParameterAnnotationClass(annotation: Annotation): ParameterType {
return when(annotation) { return when (annotation) {
is DoubleParameter -> Double is DoubleParameter -> Double
is IntParameter -> Int is IntParameter -> Int
is BooleanParameter -> Boolean is BooleanParameter -> Boolean
is ButtonParameter -> Button is ButtonParameter -> Button
is TextParameter -> Text
else -> error("no type for $annotation") else -> error("no type for $annotation")
} }
} }
@@ -103,15 +114,20 @@ fun Any.listParameters(): List<Parameter> {
(it.findAnnotation<BooleanParameter>() != null || (it.findAnnotation<BooleanParameter>() != null ||
it.findAnnotation<IntParameter>() != null || it.findAnnotation<IntParameter>() != null ||
it.findAnnotation<DoubleParameter>() != null || it.findAnnotation<DoubleParameter>() != null ||
it.findAnnotation<ButtonParameter>() != null) it.findAnnotation<ButtonParameter>() != null) ||
it.findAnnotation<TextParameter>() != null
}.map { }.map {
val annotations = listOf(it.findAnnotation<BooleanParameter>(), it.findAnnotation<IntParameter>(), it.findAnnotation<DoubleParameter>(), it.findAnnotation<ButtonParameter>()).filterNotNull() val annotations = listOfNotNull(it.findAnnotation<BooleanParameter>(),
it.findAnnotation<IntParameter>(),
it.findAnnotation<DoubleParameter>(),
it.findAnnotation<ButtonParameter>(),
it.findAnnotation<TextParameter>())
var intRange: IntRange? = null var intRange: IntRange? = null
var doubleRange: ClosedRange<Double>? = null var doubleRange: ClosedRange<Double>? = null
var order: Int = Integer.MAX_VALUE var order: Int = Integer.MAX_VALUE
var label: String = "" var label = ""
var precision: Int? = null var precision: Int? = null
var type : ParameterType? = null var type: ParameterType? = null
annotations.forEach { annotations.forEach {
type = ParameterType.forParameterAnnotationClass(it) type = ParameterType.forParameterAnnotationClass(it)
@@ -135,16 +151,17 @@ fun Any.listParameters(): List<Parameter> {
label = it.label label = it.label
order = it.order order = it.order
} }
is TextParameter -> {
label = it.label
order = it.order
}
} }
} }
Parameter(type?:error("no type"), it as KMutableProperty1<out Any, Any?>, label, doubleRange, intRange, precision, order) Parameter(type
?: error("no type"), it as KMutableProperty1<out Any, Any?>, label, doubleRange, intRange, precision, order)
}.sortedBy { it.order } }.sortedBy { it.order }
} }
fun Any.title() = this::class.findAnnotation<Description>()?.let { fun Any.title() = this::class.findAnnotation<Description>()?.title
it.title
}
fun Any.description() = this::class.findAnnotation<Description>()?.let { fun Any.description() = this::class.findAnnotation<Description>()?.description
it.description
}

View File

@@ -16,13 +16,16 @@ val a = object {
@ButtonParameter("a button parameter", order = 3) @ButtonParameter("a button parameter", order = 3)
var f = {} var f = {}
@TextParameter("a text parameter", order = 4)
var t = "test"
} }
object TestAnnotations : Spek({ object TestAnnotations : Spek({
describe("an annotated object") { describe("an annotated object") {
it("has listable parameters") { it("has listable parameters") {
val list = a.listParameters() val list = a.listParameters()
list.size `should be equal to` 4 list.size `should be equal to` 5
list[0].property.name `should be equal to` "d" list[0].property.name `should be equal to` "d"
list[0].parameterType `should be equal to` ParameterType.Double list[0].parameterType `should be equal to` ParameterType.Double
list[0].label `should be equal to` "a double scalar" list[0].label `should be equal to` "a double scalar"
@@ -47,6 +50,9 @@ object TestAnnotations : Spek({
list[3].parameterType `should be equal to` ParameterType.Button list[3].parameterType `should be equal to` ParameterType.Button
list[3].property.name `should be equal to` "f" list[3].property.name `should be equal to` "f"
list[4].parameterType `should be equal to` ParameterType.Text
list[4].property.name `should be equal to` "t"
} }
} }
}) })