Skip to content

Commit 93bf081

Browse files
authored
chore(abg): use buildCodeBlock { ... } instead of CodeBlock.builder() (#1643)
1 parent 325bd88 commit 93bf081

File tree

1 file changed

+52
-60
lines changed
  • action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation

1 file changed

+52
-60
lines changed

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/Generation.kt

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.squareup.kotlinpoet.PropertySpec
1414
import com.squareup.kotlinpoet.TypeSpec
1515
import com.squareup.kotlinpoet.asClassName
1616
import com.squareup.kotlinpoet.asTypeName
17+
import com.squareup.kotlinpoet.buildCodeBlock
1718
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
1819
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
1920
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
@@ -338,39 +339,32 @@ private fun Metadata.buildToYamlArgumentsFunction(
338339
private fun Metadata.linkedMapOfInputs(
339340
inputTypings: Map<String, Typing>,
340341
untypedClass: Boolean,
341-
): CodeBlock {
342-
if (inputs.isEmpty()) {
343-
return CodeBlock
344-
.Builder()
345-
.add(CodeBlock.of("return %T($CUSTOM_INPUTS)", LinkedHashMap::class))
346-
.build()
347-
} else {
348-
return CodeBlock
349-
.Builder()
350-
.apply {
351-
add("return linkedMapOf(\n")
352-
indent()
353-
add("*listOfNotNull(\n")
354-
indent()
355-
inputs.forEach { (key, value) ->
356-
val propertyName = key.toCamelCase()
357-
if (!untypedClass && inputTypings.containsKey(key)) {
358-
val asStringCode = inputTypings.getInputTyping(key).asString()
359-
add("%N?.let { %S·to·it$asStringCode },\n", propertyName, key)
360-
}
361-
val asStringCode = null.getInputTyping(key).asString()
362-
if (value.shouldBeRequiredInBinding() && !value.shouldBeNullable(untypedClass, inputTypings.containsKey(key))) {
363-
add("%S·to·%N$asStringCode,\n", key, "${propertyName}_Untyped")
364-
} else {
365-
add("%N?.let { %S·to·it$asStringCode },\n", "${propertyName}_Untyped", key)
366-
}
367-
}
368-
add("*$CUSTOM_INPUTS.%M().%M(),\n", Types.mapToList, Types.listToArray)
369-
unindent()
370-
add(").toTypedArray()\n")
371-
unindent()
372-
add(")")
373-
}.build()
342+
) = if (inputs.isEmpty()) {
343+
CodeBlock.of("return %T($CUSTOM_INPUTS)", LinkedHashMap::class)
344+
} else {
345+
buildCodeBlock {
346+
add("return linkedMapOf(\n")
347+
indent()
348+
add("*listOfNotNull(\n")
349+
indent()
350+
inputs.forEach { (key, value) ->
351+
val propertyName = key.toCamelCase()
352+
if (!untypedClass && inputTypings.containsKey(key)) {
353+
val asStringCode = inputTypings.getInputTyping(key).asString()
354+
add("%N?.let { %S·to·it$asStringCode },\n", propertyName, key)
355+
}
356+
val asStringCode = null.getInputTyping(key).asString()
357+
if (value.shouldBeRequiredInBinding() && !value.shouldBeNullable(untypedClass, inputTypings.containsKey(key))) {
358+
add("%S·to·%N$asStringCode,\n", key, "${propertyName}_Untyped")
359+
} else {
360+
add("%N?.let { %S·to·it$asStringCode },\n", "${propertyName}_Untyped", key)
361+
}
362+
}
363+
add("*$CUSTOM_INPUTS.%M().%M(),\n", Types.mapToList, Types.listToArray)
364+
unindent()
365+
add(").toTypedArray()\n")
366+
unindent()
367+
add(")")
374368
}
375369
}
376370

@@ -471,15 +465,16 @@ private fun Metadata.buildCommonConstructorParameters(
471465
.flatMap { (key, input) ->
472466
val typedInput = inputTypings.containsKey(key)
473467
val description = input.description.escapedForComments.removeTrailingWhitespacesForEachLine()
474-
val kdocBuilder = CodeBlock.builder()
475-
if (typedInput && !untypedClass && input.shouldBeRequiredInBinding()) {
476-
kdocBuilder.add("%L", "<required>".escapedForComments)
477-
if (description.isNotEmpty()) {
478-
kdocBuilder.add(" ")
468+
val kdoc =
469+
buildCodeBlock {
470+
if (typedInput && !untypedClass && input.shouldBeRequiredInBinding()) {
471+
add("%L", "<required>".escapedForComments)
472+
if (description.isNotEmpty()) {
473+
add(" ")
474+
}
475+
}
476+
add("%L", description)
479477
}
480-
}
481-
kdocBuilder.add("%L", description)
482-
val kdoc = kdocBuilder.build()
483478

484479
listOfNotNull(
485480
untypedClass.takeIf { !it && typedInput }?.let {
@@ -545,19 +540,18 @@ private fun TypeSpec.Builder.addInitializerBlockIfNecessary(
545540
return this
546541
}
547542

548-
private fun Metadata.initializerBlock(inputTypings: Map<String, Typing>): CodeBlock {
549-
val codeBlockBuilder = CodeBlock.builder()
550-
var first = true
551-
inputs
552-
.filter { inputTypings.containsKey(it.key) }
553-
.forEach { (key, input) ->
554-
if (!first) {
555-
codeBlockBuilder.add("\n")
556-
}
557-
first = false
558-
val propertyName = key.toCamelCase()
559-
codeBlockBuilder
560-
.add(
543+
private fun Metadata.initializerBlock(inputTypings: Map<String, Typing>) =
544+
buildCodeBlock {
545+
var first = true
546+
inputs
547+
.filter { inputTypings.containsKey(it.key) }
548+
.forEach { (key, input) ->
549+
if (!first) {
550+
add("\n")
551+
}
552+
first = false
553+
val propertyName = key.toCamelCase()
554+
add(
561555
"""
562556
require(!((%1N != null) && (%1L_Untyped != null))) {
563557
%2S
@@ -567,9 +561,8 @@ private fun Metadata.initializerBlock(inputTypings: Map<String, Typing>): CodeBl
567561
propertyName,
568562
"Only $propertyName or ${propertyName}_Untyped must be set, but not both",
569563
)
570-
if (input.shouldBeRequiredInBinding()) {
571-
codeBlockBuilder
572-
.add(
564+
if (input.shouldBeRequiredInBinding()) {
565+
add(
573566
"""
574567
require((%1N != null) || (%1L_Untyped != null)) {
575568
%2S
@@ -579,10 +572,9 @@ private fun Metadata.initializerBlock(inputTypings: Map<String, Typing>): CodeBl
579572
propertyName,
580573
"Either $propertyName or ${propertyName}_Untyped must be set, one of them is required",
581574
)
575+
}
582576
}
583-
}
584-
return codeBlockBuilder.build()
585-
}
577+
}
586578

587579
private fun actionKdoc(
588580
metadata: Metadata,

0 commit comments

Comments
 (0)