Utility methods to access Configurable properties
This commit is contained in:
parent
bc8878da48
commit
e835d81183
@ -1,12 +1,12 @@
|
||||
|
||||
plugins {
|
||||
val toolsVersion = "0.4.0-dev"
|
||||
val toolsVersion = "0.4.0"
|
||||
id("scientifik.mpp") version toolsVersion apply false
|
||||
id("scientifik.jvm") version toolsVersion apply false
|
||||
id("scientifik.publish") version toolsVersion apply false
|
||||
}
|
||||
|
||||
val dataforgeVersion by extra("0.1.5-dev-11")
|
||||
val dataforgeVersion by extra("0.1.5")
|
||||
|
||||
val bintrayRepo by extra("dataforge")
|
||||
val githubProject by extra("dataforge-core")
|
||||
|
@ -67,6 +67,7 @@ fun ItemDescriptor.validateItem(item: MetaItem<*>?): Boolean {
|
||||
*
|
||||
* @author Alexander Nozik
|
||||
*/
|
||||
@DFBuilder
|
||||
class NodeDescriptor : ItemDescriptor() {
|
||||
|
||||
/**
|
||||
|
@ -32,34 +32,37 @@ interface Configurable : Described {
|
||||
}
|
||||
|
||||
override val descriptor: NodeDescriptor? get() = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a property with default
|
||||
*/
|
||||
fun Configurable.getProperty(name: Name): MetaItem<*>? =
|
||||
config[name] ?: getDefaultItem(name) ?: descriptor?.get(name)?.defaultItem()
|
||||
/**
|
||||
* Get a property with default
|
||||
*/
|
||||
fun getProperty(name: Name): MetaItem<*>? =
|
||||
config[name] ?: getDefaultItem(name) ?: descriptor?.get(name)?.defaultItem()
|
||||
|
||||
fun Configurable.getProperty(key: String) = getProperty(key.toName())
|
||||
|
||||
/**
|
||||
* Set a configurable property
|
||||
*/
|
||||
fun Configurable.setProperty(name: Name, item: MetaItem<*>?) {
|
||||
if (validateItem(name, item)) {
|
||||
config[name] = item
|
||||
} else {
|
||||
error("Validation failed for property $name with value $item")
|
||||
/**
|
||||
* Set a configurable property
|
||||
*/
|
||||
fun setProperty(name: Name, item: MetaItem<*>?) {
|
||||
if (validateItem(name, item)) {
|
||||
config[name] = item
|
||||
} else {
|
||||
error("Validation failed for property $name with value $item")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Configurable.setProperty(name: Name, value: Value) = setProperty(name, MetaItem.ValueItem(value))
|
||||
fun Configurable.setProperty(name: Name, meta: Meta) = setProperty(name, MetaItem.NodeItem(meta))
|
||||
fun Configurable.getProperty(key: String) = getProperty(key.toName())
|
||||
|
||||
fun Configurable.setProperty(name: Name, value: Value?) = setProperty(name, value?.let { MetaItem.ValueItem(value) })
|
||||
fun Configurable.setProperty(name: Name, meta: Meta?) = setProperty(name, meta?.let { MetaItem.NodeItem(meta) })
|
||||
|
||||
fun Configurable.setProperty(key: String, item: MetaItem<*>?) {
|
||||
setProperty(key.toName(), item)
|
||||
}
|
||||
|
||||
fun Configurable.setProperty(key: String, value: Value?) = setProperty(key, value?.let { MetaItem.ValueItem(value) })
|
||||
fun Configurable.setProperty(key: String, meta: Meta?) = setProperty(key, meta?.let { MetaItem.NodeItem(meta) })
|
||||
|
||||
fun <T : Configurable> T.configure(meta: Meta): T = this.apply { config.update(meta) }
|
||||
|
||||
inline fun <T : Configurable> T.configure(action: Config.() -> Unit): T = apply { config.apply(action) }
|
||||
|
@ -170,7 +170,7 @@ fun Configurable.float(default: Float, key: Name? = null): ReadWriteProperty<Any
|
||||
*/
|
||||
fun <E : Enum<E>> Configurable.enum(
|
||||
default: E, key: Name? = null, resolve: MetaItem<*>.() -> E?
|
||||
): ReadWriteProperty<Any?, E> = item(default, key).transform {it?.resolve() ?: default }
|
||||
): ReadWriteProperty<Any?, E> = item(default, key).transform { it?.resolve() ?: default }
|
||||
|
||||
/*
|
||||
* Extra delegates for special cases
|
||||
@ -206,17 +206,31 @@ fun Configurable.node(key: Name? = null): ReadWriteProperty<Any?, Meta?> = item(
|
||||
writer = { it?.let { MetaItem.NodeItem(it) } }
|
||||
)
|
||||
|
||||
fun <T : Configurable> Configurable.spec(spec: Specification<T>, key: Name? = null): ReadWriteProperty<Any?, T?> =
|
||||
object : ReadWriteProperty<Any?, T?> {
|
||||
override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
|
||||
val name = key ?: property.name.asName()
|
||||
return config[name].node?.let { spec.wrap(it) }
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
|
||||
val name = key ?: property.name.asName()
|
||||
config[name] = value?.config
|
||||
}
|
||||
|
||||
fun <T : Configurable> Configurable.spec(
|
||||
spec: Specification<T>, key: Name? = null
|
||||
): ReadWriteProperty<Any?, T?> = object : ReadWriteProperty<Any?, T?> {
|
||||
override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
|
||||
val name = key ?: property.name.asName()
|
||||
return config[name].node?.let { spec.wrap(it) }
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
|
||||
val name = key ?: property.name.asName()
|
||||
config[name] = value?.config
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : Configurable> Configurable.spec(
|
||||
spec: Specification<T>, default: T, key: Name? = null
|
||||
): ReadWriteProperty<Any?, T> = object : ReadWriteProperty<Any?, T> {
|
||||
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
|
||||
val name = key ?: property.name.asName()
|
||||
return config[name].node?.let { spec.wrap(it) }?:default
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
|
||||
val name = key ?: property.name.asName()
|
||||
config[name] = value.config
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,11 +18,10 @@ open class Scheme() : Configurable, Described, MetaRepr {
|
||||
//constructor(config: Config, default: Meta) : this(config, { default[it] })
|
||||
constructor(config: Config) : this(config, { null })
|
||||
|
||||
final override var config: Config =
|
||||
Config()
|
||||
final override var config: Config = Config()
|
||||
internal set
|
||||
|
||||
lateinit var defaultProvider: (Name) -> MetaItem<*>?
|
||||
var defaultProvider: (Name) -> MetaItem<*>? = { null }
|
||||
internal set
|
||||
|
||||
final override var descriptor: NodeDescriptor? = null
|
||||
@ -53,9 +52,10 @@ open class Scheme() : Configurable, Described, MetaRepr {
|
||||
token to item
|
||||
} ?: emptyMap()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline operator fun <T : Scheme> T.invoke(block: T.() -> Unit) = apply(block)
|
||||
|
||||
/**
|
||||
* A specification for simplified generation of wrappers
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user