From c6f41842d12ef76b1b9be98b7806974657d692e4 Mon Sep 17 00:00:00 2001 From: Dan Adajian Date: Mon, 8 Apr 2024 10:56:20 -0500 Subject: [PATCH] feat: simplify generated enum classes (#29) --- codegen.yml | 2 ++ src/definitions/enum.ts | 7 +++---- .../should_generate_basic_enums_correctly/expected.kt | 11 +++++------ test/unit/should_generate_enums_properly/expected.kt | 9 ++++----- test/unit/should_honor_base_configs/expected.kt | 9 ++++----- test/unit/should_honor_onlyTypes_config/expected.kt | 9 ++++----- .../expected.kt | 9 ++++----- 7 files changed, 26 insertions(+), 30 deletions(-) diff --git a/codegen.yml b/codegen.yml index 513101a..755519f 100644 --- a/codegen.yml +++ b/codegen.yml @@ -1,6 +1,8 @@ overwrite: true schema: - "test/**/*.graphql" +config: + namingConvention: "keep" generates: test/integration/Types.kt: plugins: diff --git a/src/definitions/enum.ts b/src/definitions/enum.ts index 4640774..c4e7a7b 100644 --- a/src/definitions/enum.ts +++ b/src/definitions/enum.ts @@ -35,12 +35,11 @@ export function buildEnumTypeDefinition( config, definitionNode: node, }); - return `${annotations}enum class ${enumName}(val value: String) { + return `${annotations}enum class ${enumName} { ${indentMultiline(enumValues.join(",\n") + ";", 2)} companion object { - fun findByName(name: String): ${enumName}? = values().find { it.name == name } - fun findByValue(value: String): ${enumName}? = values().find { it.value == value } + fun findByName(name: String, ignoreCase: Boolean = false): ${enumName}? = values().find { it.name.equals(name, ignoreCase = ignoreCase) } } }`; } @@ -53,5 +52,5 @@ function buildEnumValueDefinition( config, definitionNode: node, }); - return `${annotations}${config.convert(node)}("${node.name.value}")`; + return `${annotations}${config.convert(node)}`; } diff --git a/test/unit/should_generate_basic_enums_correctly/expected.kt b/test/unit/should_generate_basic_enums_correctly/expected.kt index a5afc82..76524de 100644 --- a/test/unit/should_generate_basic_enums_correctly/expected.kt +++ b/test/unit/should_generate_basic_enums_correctly/expected.kt @@ -2,13 +2,12 @@ package com.kotlin.generated import com.expediagroup.graphql.generator.annotations.* -enum class UserRole(val value: String) { - Admin("ADMIN"), - User("USER"), - Editor("EDITOR"); +enum class UserRole { + Admin, + User, + Editor; companion object { - fun findByName(name: String): UserRole? = values().find { it.name == name } - fun findByValue(value: String): UserRole? = values().find { it.value == value } + fun findByName(name: String, ignoreCase: Boolean = false): UserRole? = values().find { it.name.equals(name, ignoreCase = ignoreCase) } } } diff --git a/test/unit/should_generate_enums_properly/expected.kt b/test/unit/should_generate_enums_properly/expected.kt index efe91b6..37c6c41 100644 --- a/test/unit/should_generate_enums_properly/expected.kt +++ b/test/unit/should_generate_enums_properly/expected.kt @@ -3,13 +3,12 @@ package com.kotlin.generated import com.expediagroup.graphql.generator.annotations.* @GraphQLDescription("A description for MyEnum") -enum class MyEnum(val value: String) { - This("THIS"), +enum class MyEnum { + This, @GraphQLDescription("A description for THAT") - That("THAT"); + That; companion object { - fun findByName(name: String): MyEnum? = values().find { it.name == name } - fun findByValue(value: String): MyEnum? = values().find { it.value == value } + fun findByName(name: String, ignoreCase: Boolean = false): MyEnum? = values().find { it.name.equals(name, ignoreCase = ignoreCase) } } } diff --git a/test/unit/should_honor_base_configs/expected.kt b/test/unit/should_honor_base_configs/expected.kt index 1a2d6d4..0666734 100644 --- a/test/unit/should_honor_base_configs/expected.kt +++ b/test/unit/should_honor_base_configs/expected.kt @@ -3,13 +3,12 @@ package com.kotlin.generated import com.expediagroup.graphql.generator.annotations.* @GraphQLDescription("A description for MyEnum") -enum class MyEnum(val value: String) { - THIS("THIS"), +enum class MyEnum { + THIS, @GraphQLDescription("A description for THAT") - THAT("THAT"); + THAT; companion object { - fun findByName(name: String): MyEnum? = values().find { it.name == name } - fun findByValue(value: String): MyEnum? = values().find { it.value == value } + fun findByName(name: String, ignoreCase: Boolean = false): MyEnum? = values().find { it.name.equals(name, ignoreCase = ignoreCase) } } } diff --git a/test/unit/should_honor_onlyTypes_config/expected.kt b/test/unit/should_honor_onlyTypes_config/expected.kt index 8061156..6bedc2f 100644 --- a/test/unit/should_honor_onlyTypes_config/expected.kt +++ b/test/unit/should_honor_onlyTypes_config/expected.kt @@ -11,13 +11,12 @@ data class MyType( ) @GraphQLDescription("A description for MyEnum") -enum class MyEnum(val value: String) { - This("THIS"), +enum class MyEnum { + This, @GraphQLDescription("A description for THAT") - That("THAT"); + That; companion object { - fun findByName(name: String): MyEnum? = values().find { it.name == name } - fun findByValue(value: String): MyEnum? = values().find { it.value == value } + fun findByName(name: String, ignoreCase: Boolean = false): MyEnum? = values().find { it.name.equals(name, ignoreCase = ignoreCase) } } } diff --git a/test/unit/should_include_dependent_types_in_onlyTypes_config/expected.kt b/test/unit/should_include_dependent_types_in_onlyTypes_config/expected.kt index ad89293..05c4797 100644 --- a/test/unit/should_include_dependent_types_in_onlyTypes_config/expected.kt +++ b/test/unit/should_include_dependent_types_in_onlyTypes_config/expected.kt @@ -25,13 +25,12 @@ data class NestedListType( val field: String? = null ) -enum class MyEnum(val value: String) { - This("THIS"), - That("THAT"); +enum class MyEnum { + This, + That; companion object { - fun findByName(name: String): MyEnum? = values().find { it.name == name } - fun findByValue(value: String): MyEnum? = values().find { it.value == value } + fun findByName(name: String, ignoreCase: Boolean = false): MyEnum? = values().find { it.name.equals(name, ignoreCase = ignoreCase) } } }