I was wondering if there is a way to require configuration parameters when making custom plugins? My current hack around is to catch it at runtime
class PluginConfiguration {
var someConfig: String? = null
}
val MyPlugin =
createApplicationPlugin(name = "MyPlugin", createConfiguration = ::PluginConfiguration) {
val someConfig = pluginConfig.someConfig
pluginConfig.apply {
if (someConfig == null) { // catch here
throw java.lang.Exception("Must pass in someConfig")
}
onCallReceive { call ->
// do stuff
}
}
}
but it would be nice if there was a way for the compiler to catch.
My use case for not wanting defaults is that I want to pass in expensive objects that are managed with dependency injection
CodePudding user response:
I think it's not possible with PluginConfiguration
API.
But there should be no problem in converting MyPlugin
to a function, which will require a parameter to be specified:
fun MyPlugin(someRequiredConfig: String) =
createApplicationPlugin(name = "MyPlugin", createConfiguration = ::PluginConfiguration) {
val someConfig = someRequiredConfig
pluginConfig.apply {
onCallReceive { call ->
// do stuff
}
}
}
// ...
install(MyPlugin("config"))