Skip to content

Commit

Permalink
chore: arg checking & pass to startRoot<TLayer>()
Browse files Browse the repository at this point in the history
  • Loading branch information
LizAinslie committed Nov 11, 2024
1 parent 5e10287 commit f6df411
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.coroutines.runBlocking
import sh.illumi.kraft.KraftException
import sh.illumi.kraft.engine.ApplicationEngine.Default
import sh.illumi.kraft.layer.ApplicationLayer
import sh.illumi.kraft.util.argsMatchParams

/**
* An ApplicationEngine is the entry point for a Kraft application. It is
Expand All @@ -22,10 +23,12 @@ abstract class ApplicationEngine {
rootLayer.start()
}

inline fun <reified TLayer : ApplicationLayer> startRoot() = startRoot<TLayer> {
inline fun <reified TLayer : ApplicationLayer> startRoot(vararg constructorArgs: Any) = startRoot<TLayer> {
TLayer::class.constructors.firstOrNull {
it.parameters.size == 1 && it.parameters[0].type.classifier == CoroutineScope::class
}?.call(this) ?: throw KraftException("Root layer has no suitable constructor")
it.parameters.size == 1 + constructorArgs.size &&
it.parameters[0].type.classifier == CoroutineScope::class &&
argsMatchParams(constructorArgs, it.parameters.drop(1).toTypedArray())
}?.call(this, *constructorArgs) ?: throw KraftException("Root layer has no suitable constructor")
}

companion object Default : ApplicationEngine()
Expand Down

0 comments on commit f6df411

Please sign in to comment.