@@ -14,6 +14,7 @@ import com.squareup.kotlinpoet.PropertySpec
14
14
import com.squareup.kotlinpoet.TypeSpec
15
15
import com.squareup.kotlinpoet.asClassName
16
16
import com.squareup.kotlinpoet.asTypeName
17
+ import com.squareup.kotlinpoet.buildCodeBlock
17
18
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
18
19
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
19
20
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
@@ -338,39 +339,32 @@ private fun Metadata.buildToYamlArgumentsFunction(
338
339
private fun Metadata.linkedMapOfInputs (
339
340
inputTypings : Map <String , Typing >,
340
341
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(" )" )
374
368
}
375
369
}
376
370
@@ -471,15 +465,16 @@ private fun Metadata.buildCommonConstructorParameters(
471
465
.flatMap { (key, input) ->
472
466
val typedInput = inputTypings.containsKey(key)
473
467
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)
479
477
}
480
- }
481
- kdocBuilder.add(" %L" , description)
482
- val kdoc = kdocBuilder.build()
483
478
484
479
listOfNotNull(
485
480
untypedClass.takeIf { ! it && typedInput }?.let {
@@ -545,19 +540,18 @@ private fun TypeSpec.Builder.addInitializerBlockIfNecessary(
545
540
return this
546
541
}
547
542
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(
561
555
"""
562
556
require(!((%1N != null) && (%1L_Untyped != null))) {
563
557
%2S
@@ -567,9 +561,8 @@ private fun Metadata.initializerBlock(inputTypings: Map<String, Typing>): CodeBl
567
561
propertyName,
568
562
" Only $propertyName or ${propertyName} _Untyped must be set, but not both" ,
569
563
)
570
- if (input.shouldBeRequiredInBinding()) {
571
- codeBlockBuilder
572
- .add(
564
+ if (input.shouldBeRequiredInBinding()) {
565
+ add(
573
566
"""
574
567
require((%1N != null) || (%1L_Untyped != null)) {
575
568
%2S
@@ -579,10 +572,9 @@ private fun Metadata.initializerBlock(inputTypings: Map<String, Typing>): CodeBl
579
572
propertyName,
580
573
" Either $propertyName or ${propertyName} _Untyped must be set, one of them is required" ,
581
574
)
575
+ }
582
576
}
583
- }
584
- return codeBlockBuilder.build()
585
- }
577
+ }
586
578
587
579
private fun actionKdoc (
588
580
metadata : Metadata ,
0 commit comments