Skip to content

Commit 4709a21

Browse files
authored
[#61] feat: allow customizing enum type name (#67)
1 parent d015ae3 commit 4709a21

File tree

8 files changed

+58
-0
lines changed

8 files changed

+58
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ inputs:
182182

183183
Multiple possible values.
184184

185+
You can also optionally define `name` which is a hint for code generators what name can be used as enum type name.
186+
185187
Example:
186188
```yaml
187189
...
@@ -192,5 +194,11 @@ inputs:
192194
- user
193195
- admin
194196
- guest
197+
if_mention:
198+
type: enum
199+
name: MentionStatus
200+
allowed-values:
201+
- success
202+
- failure
195203
...
196204
```

src/main/kotlin/it/krzeminski/githubactionstyping/parsing/TypesManifestParsing.kt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ data class TypesManifest(
1414
@Serializable
1515
data class ApiItem(
1616
val type: String? = null,
17+
val name: String? = null,
1718
@SerialName("allowed-values")
1819
val allowedValues: List<String>? = null,
1920
val separator: String? = null,

src/main/kotlin/it/krzeminski/githubactionstyping/validation/types/Boolean.kt

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import it.krzeminski.githubactionstyping.parsing.ApiItem
44
import it.krzeminski.githubactionstyping.validation.ItemValidationResult
55

66
fun ApiItem.validateBoolean(): ItemValidationResult {
7+
if (this.name != null) {
8+
return ItemValidationResult.Invalid("'name' is not allowed for this type.")
9+
}
710
if (this.allowedValues != null) {
811
return ItemValidationResult.Invalid("'allowed-values' is not allowed for this type.")
912
}

src/main/kotlin/it/krzeminski/githubactionstyping/validation/types/Float.kt

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import it.krzeminski.githubactionstyping.parsing.ApiItem
44
import it.krzeminski.githubactionstyping.validation.ItemValidationResult
55

66
fun ApiItem.validateFloat(): ItemValidationResult {
7+
if (this.name != null) {
8+
return ItemValidationResult.Invalid("'name' is not allowed for this type.")
9+
}
710
if (this.allowedValues != null) {
811
return ItemValidationResult.Invalid("'allowed-values' is not allowed for this type.")
912
}

src/main/kotlin/it/krzeminski/githubactionstyping/validation/types/Integer.kt

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import it.krzeminski.githubactionstyping.parsing.ApiItem
44
import it.krzeminski.githubactionstyping.validation.ItemValidationResult
55

66
fun ApiItem.validateInteger(): ItemValidationResult {
7+
if (this.name != null) {
8+
return ItemValidationResult.Invalid("'name' is not allowed for this type.")
9+
}
710
if (this.allowedValues != null) {
811
return ItemValidationResult.Invalid("'allowed-values' is not allowed for this type.")
912
}

src/main/kotlin/it/krzeminski/githubactionstyping/validation/types/List.kt

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import it.krzeminski.githubactionstyping.validation.ItemValidationResult
55
import it.krzeminski.githubactionstyping.validation.validate
66

77
fun ApiItem.validateList(): ItemValidationResult {
8+
if (this.name != null) {
9+
return ItemValidationResult.Invalid("'name' is not allowed for this type.")
10+
}
811
if (this.listItem == null) {
912
return ItemValidationResult.Invalid("List item information must be specified.")
1013
}

src/main/kotlin/it/krzeminski/githubactionstyping/validation/types/String.kt

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import it.krzeminski.githubactionstyping.parsing.ApiItem
44
import it.krzeminski.githubactionstyping.validation.ItemValidationResult
55

66
fun ApiItem.validateString(): ItemValidationResult {
7+
if (this.name != null) {
8+
return ItemValidationResult.Invalid("'name' is not allowed for this type.")
9+
}
710
if (this.allowedValues != null) {
811
return ItemValidationResult.Invalid("'allowed-values' is not allowed for this type.")
912
}

src/test/kotlin/it/krzeminski/githubactionstyping/validation/ManifestValidationTest.kt

+34
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class ManifestValidationTest : FunSpec({
4040
val manifest = TypesManifest(
4141
inputs = mapOf(
4242
"enum-input" to ApiItem(type = "enum", allowedValues = listOf("foo", "bar", "baz")),
43+
"enum-input-with-custom-item-name" to ApiItem(
44+
type = "enum",
45+
name = "SomeItemName",
46+
allowedValues = listOf("foo", "bar", "baz"),
47+
),
4348
),
4449
)
4550

@@ -51,6 +56,7 @@ class ManifestValidationTest : FunSpec({
5156
overallResult = ItemValidationResult.Valid,
5257
inputs = mapOf(
5358
"enum-input" to ItemValidationResult.Valid,
59+
"enum-input-with-custom-item-name" to ItemValidationResult.Valid,
5460
),
5561
)
5662
}
@@ -468,5 +474,33 @@ class ManifestValidationTest : FunSpec({
468474
),
469475
)
470476
}
477+
478+
test("non-enum types with 'name' attribute") {
479+
// given
480+
val manifest = TypesManifest(
481+
inputs = mapOf(
482+
"string-input" to ApiItem(type = "string", name = "SomeName"),
483+
"boolean-input" to ApiItem(type = "boolean", name = "SomeName"),
484+
"integer-input" to ApiItem(type = "integer", name = "SomeName"),
485+
"float-input" to ApiItem(type = "float", name = "SomeName"),
486+
"list-input" to ApiItem(type = "list", name = "SomeName", separator = ",", listItem = ApiItem(type = "string")),
487+
),
488+
)
489+
490+
// when
491+
val result = manifest.validate()
492+
493+
// then
494+
result shouldBe ActionValidationResult(
495+
overallResult = ItemValidationResult.Invalid("Some typing is invalid."),
496+
inputs = mapOf(
497+
"string-input" to ItemValidationResult.Invalid("'name' is not allowed for this type."),
498+
"boolean-input" to ItemValidationResult.Invalid("'name' is not allowed for this type."),
499+
"integer-input" to ItemValidationResult.Invalid("'name' is not allowed for this type."),
500+
"float-input" to ItemValidationResult.Invalid("'name' is not allowed for this type."),
501+
"list-input" to ItemValidationResult.Invalid("'name' is not allowed for this type."),
502+
),
503+
)
504+
}
471505
}
472506
})

0 commit comments

Comments
 (0)