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