Skip to content

Commit 7cc6014

Browse files
authored
fix(abg): generate unique enum value names (#1936)
1 parent 7622781 commit 7cc6014

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypingGeneration.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@ private fun EnumTyping.buildEnumCustomType(
7676
fieldName: String,
7777
className: String,
7878
): TypeSpec {
79-
val itemsNames = itemsNames ?: items.map { it.toPascalCase() }
80-
val itemsNameMap = items.zip(itemsNames).toMap()
79+
val sortedItems = items.sorted()
80+
val itemsNames =
81+
itemsNames ?: sortedItems
82+
.map { it.toPascalCase() }
83+
.groupBy { it }
84+
.values
85+
.flatMap { it.mapIndexed { index, name -> "$name${if (index > 0) "_${index + 1}" else ""}" } }
8186
val typeName = this.typeName?.toPascalCase() ?: fieldName.toPascalCase()
8287
val actionPackageName = coords.owner.toKotlinPackageName()
8388
val sealedClassName = this.getClassName(actionPackageName, className, fieldName)
@@ -92,11 +97,11 @@ private fun EnumTyping.buildEnumCustomType(
9297
.build(),
9398
).addProperty(PropertySpec.builder("stringValue", String::class).initializer("stringValue").build())
9499
.addTypes(
95-
this.items.map {
100+
sortedItems.mapIndexed { i, it ->
96101
val itemName =
97-
itemsNameMap[it]?.let {
102+
itemsNames[i].let {
98103
if (it == "Custom") "CustomEnum" else it
99-
} ?: error("FIXME: key=$it absent from $itemsNameMap")
104+
}
100105
TypeSpec
101106
.objectBuilder(itemName)
102107
.superclass(sealedClassName)

action-binding-generator/src/test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/bindingsfromunittests/ActionWithAllTypesOfInputs.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,17 @@ public data class ActionWithAllTypesOfInputs private constructor(
301301
public sealed class Bin(
302302
public val stringValue: String,
303303
) {
304-
public object Foo : ActionWithAllTypesOfInputs.Bin("foo")
304+
public object Bam123 : ActionWithAllTypesOfInputs.Bin("bam-123")
305305

306-
public object BooBar : ActionWithAllTypesOfInputs.Bin("boo-bar")
306+
public object Bam123_2 : ActionWithAllTypesOfInputs.Bin("bam/123")
307+
308+
public object Bam123_3 : ActionWithAllTypesOfInputs.Bin("bam:123")
307309

308310
public object Baz123 : ActionWithAllTypesOfInputs.Bin("baz123")
309311

310-
public object Bam123 : ActionWithAllTypesOfInputs.Bin("bam:123")
312+
public object BooBar : ActionWithAllTypesOfInputs.Bin("boo-bar")
313+
314+
public object Foo : ActionWithAllTypesOfInputs.Bin("foo")
311315

312316
public class Custom(
313317
customStringValue: String,
@@ -341,10 +345,10 @@ public data class ActionWithAllTypesOfInputs private constructor(
341345
) {
342346
public object One : ActionWithAllTypesOfInputs.MyEnum("one")
343347

344-
public object Two : ActionWithAllTypesOfInputs.MyEnum("two")
345-
346348
public object Three : ActionWithAllTypesOfInputs.MyEnum("three")
347349

350+
public object Two : ActionWithAllTypesOfInputs.MyEnum("two")
351+
348352
public class Custom(
349353
customStringValue: String,
350354
) : ActionWithAllTypesOfInputs.MyEnum(customStringValue)

action-binding-generator/src/test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/GenerationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class GenerationTest :
9696
"bin-kin" to BooleanTyping,
9797
"int-pint" to IntegerTyping,
9898
"flo-pint" to FloatTyping,
99-
"fin-bin" to EnumTyping("Bin", listOf("foo", "boo-bar", "baz123", "bam:123")),
99+
"fin-bin" to EnumTyping("Bin", listOf("foo", "boo-bar", "baz123", "bam:123", "bam-123", "bam/123")),
100100
"goo-zen" to IntegerWithSpecialValueTyping("Zen", mapOf("Special1" to 3, "Special2" to -1)),
101101
"bah-enum" to EnumTyping(null, listOf("helloworld"), listOf("HelloWorld")),
102102
"list-strings" to ListOfTypings(",", StringTyping),

0 commit comments

Comments
 (0)