Implement simplified property bag dsl:

move it to the separate jar,
use property bag chaining
This commit is contained in:
Ilya Chernikov
2018-03-27 23:49:07 +02:00
parent 495c75c48e
commit 2ddcc280a9
20 changed files with 124 additions and 119 deletions
@@ -30,11 +30,5 @@ object ScriptCompileConfigurationParams {
val updateConfigurationOnAnnotations by typedKey<Iterable<KClass<out Annotation>>>()
val updateConfigurationOnSections by typedKey<Iterable<String>>()
open class Builder(parentBuilder: PropertyBagBuilder? = null) : PropertyBagBuilder(parentBuilder) {
inline fun <reified T> signature(providedDeclarations: ProvidedDeclarations = ProvidedDeclarations.Empty) {
add(scriptSignature to ScriptSignature(T::class, providedDeclarations))
}
}
}
@@ -7,12 +7,10 @@
package kotlin.script.experimental.api
object ScriptDefinitionProperties : PropertiesBase<ScriptDefinitionProperties>() {
object ScriptDefinitionProperties {
val name by typedKey<String>()
val fileExtension by typedKey<String>()
open class Builder(parentBuilder: PropertyBagBuilder? = null) : PropertyBagBuilder(parentBuilder)
}
@@ -7,21 +7,16 @@
package kotlin.script.experimental.api
open class ScriptEvaluationEnvironmentParams : PropertyBagBuilder() {
companion object {
val implicitReceivers by typedKey<List<Any>>()
object ScriptEvaluationEnvironmentParams {
val implicitReceivers by typedKey<List<Any>>()
val contextVariables by typedKey<Map<String, Any?>>() // external variables
val contextVariables by typedKey<Map<String, Any?>>() // external variables
val constructorArgs by typedKey<List<Any?>>()
val constructorArgs by typedKey<List<Any?>>()
val runArgs by typedKey<List<Any?>>()
}
val runArgs by typedKey<List<Any?>>()
}
inline fun scriptEvaluationEnvironment(from: ChainedPropertyBag = ChainedPropertyBag(), body: ScriptEvaluationEnvironmentParams.() -> Unit) =
ScriptEvaluationEnvironmentParams().build(from, body)
typealias ScriptEvaluationEnvironment = ChainedPropertyBag
data class EvaluationResult(val returnValue: Any?, val environment: ScriptEvaluationEnvironment)
@@ -5,13 +5,8 @@
package kotlin.script.experimental.api
class ProcessedScriptDataParams : PropertyBagBuilder() {
companion object {
val annotations by typedKey<Iterable<Annotation>>()
object ProcessedScriptDataParams {
val annotations by typedKey<Iterable<Annotation>>()
val fragments by typedKey<Iterable<ScriptSourceNamedFragment>>()
}
val fragments by typedKey<Iterable<ScriptSourceNamedFragment>>()
}
inline fun processedScriptData(from: ChainedPropertyBag = ChainedPropertyBag(), body: ProcessedScriptDataParams.() -> Unit) =
ProcessedScriptDataParams().build(from, body)
@@ -6,7 +6,6 @@
package kotlin.script.experimental.api
import kotlin.reflect.KProperty
import kotlin.reflect.full.primaryConstructor
data class TypedKey<T>(val name: String)
@@ -16,41 +15,23 @@ class TypedKeyDelegate<T> {
fun <T> typedKey() = TypedKeyDelegate<T>()
class ChainedPropertyBag(private val parent: ChainedPropertyBag? = null, private val data: Map<TypedKey<*>, Any?> = hashMapOf()) {
constructor(data: Map<TypedKey<*>, Any?>) : this(null, data)
constructor(parent: ChainedPropertyBag?, pairs: Iterable<Pair<TypedKey<*>, Any?>>) : this(
parent,
HashMap<TypedKey<*>, Any?>().also {
it.putAll(pairs)
})
class ChainedPropertyBag(private val parent: ChainedPropertyBag? = null, pairs: Iterable<Pair<TypedKey<*>, Any?>>) {
constructor(parent: ChainedPropertyBag, vararg pairs: Pair<TypedKey<*>, Any?>) : this(parent, pairs.asIterable())
constructor(vararg pairs: Pair<TypedKey<*>, Any?>) : this(null, pairs.asIterable())
constructor(parent: ChainedPropertyBag?, vararg pairs: Pair<TypedKey<*>, Any?>) : this(parent, hashMapOf(*pairs))
private val data = HashMap<TypedKey<*>, Any?>().also { it.putAll(pairs) }
operator fun <T> get(key: TypedKey<T>): T =
inline operator fun <reified T> get(key: TypedKey<T>): T = getUnchecked(key) as T
fun <T> getUnchecked(key: TypedKey<T>): Any? =
when {
data.containsKey(key) -> data[key] as T
parent != null -> parent[key]
data.containsKey(key) -> data[key]
parent != null -> parent.getUnchecked(key)
else -> throw IllegalArgumentException("Unknown key $key")
}
fun <T> getOrNull(key: TypedKey<T>): T? = data[key] as T? ?: parent?.getOrNull(key)
inline fun <reified T> getOrNull(key: TypedKey<T>): T? = getOrNullUnchecked(key)?.let { it as T }
fun <T> getOrNullUnchecked(key: TypedKey<T>): Any? = data[key] ?: parent?.getOrNullUnchecked(key)
}
open class PropertyBagBuilder(private val parentBuilder: PropertyBagBuilder? = null) {
val pairs: MutableList<Pair<TypedKey<*>, Any?>> = arrayListOf()
open operator fun <T> TypedKey<T>.invoke(v: T) {
pairs.add(this to v)
}
fun add(pair: Pair<TypedKey<*>, Any?>) {
pairs.add(pair)
}
fun getAllPairs() = if (parentBuilder == null) pairs else parentBuilder.pairs + pairs
}
inline fun <T : PropertyBagBuilder> T.build(parent: ChainedPropertyBag? = null, body: T.() -> Unit): ChainedPropertyBag {
body()
return ChainedPropertyBag(parent, getAllPairs())
}
@@ -11,7 +11,5 @@ object ScriptingEnvironmentProperties {
// required by definitions that extract data from script base class annotations
val baseClass by typedKey<KClass<*>>()
open class Builder(parentBuilder: PropertyBagBuilder? = null) : PropertyBagBuilder(parentBuilder)
}