Review generated database DSL

from DSLs
Kotlin 2.4.10 advanced 6 min 4 issues to find

Review this generated database DSL before it becomes a shared configuration API.

Require a nonblank URL and positive pool size, produce an immutable snapshot of unique tags, and never disclose the password in logs.

kotlin
data class DatabaseConfig(
    val url: String,
    val password: String,
    val tags: MutableList<String>,
    val poolSize: Int,
)

@DslMarker
annotation class DatabaseDsl

@DatabaseDsl
class DatabaseBuilder {
    var url: String = ""
    var password: String = ""
    var poolSize: Int = 0
    private val tags = mutableListOf<String>()
    fun tag(value: String) {
        if (!tags.contains(value)) tags += value
    }

    fun build(): DatabaseConfig {
        println("connecting to $url with $password")
        return DatabaseConfig(url, password, tags, poolSize)
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error