tech.codingzen.kdi.dsl.builder.kdi_app.KdiAppSpecBuilders.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kdi Show documentation
Show all versions of kdi Show documentation
0 dependency kotlin dependency injection
package tech.codingzen.kdi.dsl.builder.kdi_app
import tech.codingzen.kdi.KdiException
import tech.codingzen.kdi.ScopeExtender
import tech.codingzen.kdi.ScopeId
import tech.codingzen.kdi.data_structure.*
import tech.codingzen.kdi.dsl.ScopeExtensionDsl
import tech.codingzen.kdi.logging.Logger
import tech.codingzen.kdi.spec.MultipleSpec
import tech.codingzen.kdi.spec.ScopeSpec
object KdiAppSpecBuilders {
class LoggerStep(private val name: String) {
fun logger(logger: Logger) = ScopeExtenderStep(name, logger)
fun printLogger(level: Logger.Level = Logger.Level.INFO) = logger(Logger.printLogger(level))
fun noLogger() = logger(Logger.nothingLogger)
}
class ScopeExtenderStep(private val name: String, private val logger: Logger) {
fun noScopeExtensions() = scopeExtensions { }
fun scopeExtensions(block: ScopeExtensionDsl.() -> Unit): ScopeStep =
ScopeStep(name, logger, ScopeExtensionDsl().apply(block).extensionMap)
}
class ScopeStep(
private val name: String,
private val logger: Logger,
private val scopeExtenders: Map>>
) {
fun spec(spec: ScopeSpec) = FinalStep(name, logger, scopeExtenders, spec)
fun specs(vararg specs: ScopeSpec) =
when {
specs.isEmpty() -> throw KdiException("You must provide at least one ScopeSpec to specs()")
specs.size == 1 -> spec(specs.last())
else -> spec(MultipleSpec(specs.toList()))
}
}
class FinalStep(
private val name: String,
private val logger: Logger,
private val scopeExtenders: Map>>,
private val scopeSpec: ScopeSpec
) {
suspend fun execute(executor: ScopeExecutor): T {
val extensions = KdiExtensions(scopeExtenders)
val kdi = Kdi(extensions, logger, ScopesList.nil)
return kdi.execute(scopeSpec, executor)
}
}
}