Skip to content

Commit

Permalink
feat: improve enum companion object functions (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Mar 25, 2024
1 parent fc13905 commit f595ca1
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 30 deletions.
8 changes: 3 additions & 5 deletions src/definitions/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export function buildEnumTypeDefinition(
config,
definitionNode: node,
});
return `${annotations}enum class ${enumName}(val label: String) {
return `${annotations}enum class ${enumName}(val value: String) {
${indentMultiline(enumValues.join(",\n") + ";", 2)}
companion object {
@JvmStatic
fun valueOfLabel(label: String): ${enumName}? {
return values().find { it.label == label }
}
fun findByName(name: String): ${enumName}? = values().find { it.name == name }
fun findByValue(value: String): ${enumName}? = values().find { it.value == value }
}
}`;
}
Expand Down
8 changes: 3 additions & 5 deletions test/unit/should_generate_basic_enums_correctly/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package com.kotlin.generated

import com.expediagroup.graphql.generator.annotations.*

enum class UserRole(val label: String) {
enum class UserRole(val value: String) {
Admin("ADMIN"),
User("USER"),
Editor("EDITOR");

companion object {
@JvmStatic
fun valueOfLabel(label: String): UserRole? {
return values().find { it.label == label }
}
fun findByName(name: String): UserRole? = values().find { it.name == name }
fun findByValue(value: String): UserRole? = values().find { it.value == value }
}
}
8 changes: 3 additions & 5 deletions test/unit/should_generate_enums_properly/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package com.kotlin.generated
import com.expediagroup.graphql.generator.annotations.*

@GraphQLDescription("A description for MyEnum")
enum class MyEnum(val label: String) {
enum class MyEnum(val value: String) {
This("THIS"),
@GraphQLDescription("A description for THAT")
That("THAT");

companion object {
@JvmStatic
fun valueOfLabel(label: String): MyEnum? {
return values().find { it.label == label }
}
fun findByName(name: String): MyEnum? = values().find { it.name == name }
fun findByValue(value: String): MyEnum? = values().find { it.value == value }
}
}
8 changes: 3 additions & 5 deletions test/unit/should_honor_base_configs/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package com.kotlin.generated
import com.expediagroup.graphql.generator.annotations.*

@GraphQLDescription("A description for MyEnum")
enum class MyEnum(val label: String) {
enum class MyEnum(val value: String) {
THIS("THIS"),
@GraphQLDescription("A description for THAT")
THAT("THAT");

companion object {
@JvmStatic
fun valueOfLabel(label: String): MyEnum? {
return values().find { it.label == label }
}
fun findByName(name: String): MyEnum? = values().find { it.name == name }
fun findByValue(value: String): MyEnum? = values().find { it.value == value }
}
}
8 changes: 3 additions & 5 deletions test/unit/should_honor_onlyTypes_config/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ data class MyType(
)

@GraphQLDescription("A description for MyEnum")
enum class MyEnum(val label: String) {
enum class MyEnum(val value: String) {
This("THIS"),
@GraphQLDescription("A description for THAT")
That("THAT");

companion object {
@JvmStatic
fun valueOfLabel(label: String): MyEnum? {
return values().find { it.label == label }
}
fun findByName(name: String): MyEnum? = values().find { it.name == name }
fun findByValue(value: String): MyEnum? = values().find { it.value == value }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ data class NestedListType(
val field: String? = null
)

enum class MyEnum(val label: String) {
enum class MyEnum(val value: String) {
This("THIS"),
That("THAT");

companion object {
@JvmStatic
fun valueOfLabel(label: String): MyEnum? {
return values().find { it.label == label }
}
fun findByName(name: String): MyEnum? = values().find { it.name == name }
fun findByValue(value: String): MyEnum? = values().find { it.value == value }
}
}

Expand Down

0 comments on commit f595ca1

Please sign in to comment.