Add support for ConfigMap-based commands to smnp.audio.synth module
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user