Add support for ConfigMap-based commands to smnp.audio.synth module

This commit is contained in:
2020-04-06 12:48:03 +02:00
parent 4086b04b59
commit e7e76e9165
4 changed files with 52 additions and 33 deletions

View File

@@ -6,10 +6,17 @@ import io.smnp.type.model.Value
class ConfigMap(private val map: Map<Value, Value>) {
private val raw by lazy { map.map { (key, value) -> key.unwrap() to value }.toMap() as Map<String, Value> }
val entries: Set<Map.Entry<String, Value>>
get() = raw.entries
operator fun get(key: String): Value {
return raw[key] ?: throw ShouldNeverReachThisLineException()
}
fun getOrNull(key: String): Value? {
return raw[key]
}
fun <T> getUnwrappedOrDefault(key: String, default: T): T {
return raw[key]?.unwrap() as T ?: default
}

View File

@@ -20,11 +20,13 @@ class ConfigMapSchema {
return this
}
fun parse(config: Value): ConfigMap {
fun parse(config: Value, vararg cascade: ConfigMap): ConfigMap {
val configMap = config.value as Map<Value, Value>
return ConfigMap(parameters.mapNotNull { (name, parameter) ->
val value = configMap[Value.string(name)]
val key = Value.string(name)
val value = configMap[key]
?: cascade.flatMap { it.entries } .firstOrNull { it.key == name } ?.value
?: if (parameter.required) throw CustomException("The '$name' parameter of ${parameter.matcher} is required")
else parameter.default