I'm trying to generate this Kotlin code that contains a DSL with a parameter:
listOf(
navArgument(QUERY_PARAM) {
type = NavType.StringType
nullable = true
defaultValue = null
},
)
Is there a better way to provide the parameters to the DSL than just build the string manually?
CodeBlock.builder()
.addStatement("listOf(")
.indent()
.beginControlFlow("%M(${queryParam})", MEMBER_NAME_NAV_ARGUMENT)
.addStatement([...])
.endControlFlow()
.unindent()
.add(")")
.build(),
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
CodeBlockthat 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:Note the dangling
,- this seems to be a bug which I just filed.