KotlinPoet's API mostly models language constructs - types, functions, properties, there's not a lot of special API to model bodies of functions, constructors, etc. That said, there are a few methods inside CodeBlock
that can help reduce the amount of manually-built strings, in addition to format modifiers. Here's what I came up with, hopefully some bits of it are helpful:
@Test fun dsl() {
val queryParam = "QUERY_PARAM"
val navArgument = MemberName(packageName = "", simpleName = "navArgument")
val stringType = ClassName(packageName = "", simpleNames = listOf("NavType", "StringType"))
val navArgumentConfiguration = listOf(
CodeBlock.of("type = %T", stringType),
CodeBlock.of("nullable = %L", true),
Companion.of("defaultValue = %L", null),
)
val navArgumentCall = CodeBlock.builder()
.beginControlFlow("%M(%L)", navArgument, queryParam)
.add(navArgumentConfiguration.joinToCode(separator = "\n", suffix = "\n"))
.endControlFlow()
.build()
.trim()
val navArgumentCalls = listOf(navArgumentCall)
.joinToCode(prefix = "listOf(⇥\n", separator = ",\n", suffix = ",⇤\n)")
assertThat(navArgumentCalls.toString()).isEqualTo(
"""
listOf(
navArgument(QUERY_PARAM) {
type = NavType.StringType
nullable = true
defaultValue = null
}
,
)
""".trimIndent()
)
}
Note the dangling ,
- this seems to be a bug which I just filed.