Skip to content

Commit c6f4184

Browse files
authored
feat: simplify generated enum classes (#29)
1 parent 1e8aa87 commit c6f4184

File tree

7 files changed

+26
-30
lines changed

7 files changed

+26
-30
lines changed

codegen.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
overwrite: true
22
schema:
33
- "test/**/*.graphql"
4+
config:
5+
namingConvention: "keep"
46
generates:
57
test/integration/Types.kt:
68
plugins:

src/definitions/enum.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ export function buildEnumTypeDefinition(
3535
config,
3636
definitionNode: node,
3737
});
38-
return `${annotations}enum class ${enumName}(val value: String) {
38+
return `${annotations}enum class ${enumName} {
3939
${indentMultiline(enumValues.join(",\n") + ";", 2)}
4040
4141
companion object {
42-
fun findByName(name: String): ${enumName}? = values().find { it.name == name }
43-
fun findByValue(value: String): ${enumName}? = values().find { it.value == value }
42+
fun findByName(name: String, ignoreCase: Boolean = false): ${enumName}? = values().find { it.name.equals(name, ignoreCase = ignoreCase) }
4443
}
4544
}`;
4645
}
@@ -53,5 +52,5 @@ function buildEnumValueDefinition(
5352
config,
5453
definitionNode: node,
5554
});
56-
return `${annotations}${config.convert(node)}("${node.name.value}")`;
55+
return `${annotations}${config.convert(node)}`;
5756
}

test/unit/should_generate_basic_enums_correctly/expected.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package com.kotlin.generated
22

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

5-
enum class UserRole(val value: String) {
6-
Admin("ADMIN"),
7-
User("USER"),
8-
Editor("EDITOR");
5+
enum class UserRole {
6+
Admin,
7+
User,
8+
Editor;
99

1010
companion object {
11-
fun findByName(name: String): UserRole? = values().find { it.name == name }
12-
fun findByValue(value: String): UserRole? = values().find { it.value == value }
11+
fun findByName(name: String, ignoreCase: Boolean = false): UserRole? = values().find { it.name.equals(name, ignoreCase = ignoreCase) }
1312
}
1413
}

test/unit/should_generate_enums_properly/expected.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package com.kotlin.generated
33
import com.expediagroup.graphql.generator.annotations.*
44

55
@GraphQLDescription("A description for MyEnum")
6-
enum class MyEnum(val value: String) {
7-
This("THIS"),
6+
enum class MyEnum {
7+
This,
88
@GraphQLDescription("A description for THAT")
9-
That("THAT");
9+
That;
1010

1111
companion object {
12-
fun findByName(name: String): MyEnum? = values().find { it.name == name }
13-
fun findByValue(value: String): MyEnum? = values().find { it.value == value }
12+
fun findByName(name: String, ignoreCase: Boolean = false): MyEnum? = values().find { it.name.equals(name, ignoreCase = ignoreCase) }
1413
}
1514
}

test/unit/should_honor_base_configs/expected.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package com.kotlin.generated
33
import com.expediagroup.graphql.generator.annotations.*
44

55
@GraphQLDescription("A description for MyEnum")
6-
enum class MyEnum(val value: String) {
7-
THIS("THIS"),
6+
enum class MyEnum {
7+
THIS,
88
@GraphQLDescription("A description for THAT")
9-
THAT("THAT");
9+
THAT;
1010

1111
companion object {
12-
fun findByName(name: String): MyEnum? = values().find { it.name == name }
13-
fun findByValue(value: String): MyEnum? = values().find { it.value == value }
12+
fun findByName(name: String, ignoreCase: Boolean = false): MyEnum? = values().find { it.name.equals(name, ignoreCase = ignoreCase) }
1413
}
1514
}

test/unit/should_honor_onlyTypes_config/expected.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ data class MyType(
1111
)
1212

1313
@GraphQLDescription("A description for MyEnum")
14-
enum class MyEnum(val value: String) {
15-
This("THIS"),
14+
enum class MyEnum {
15+
This,
1616
@GraphQLDescription("A description for THAT")
17-
That("THAT");
17+
That;
1818

1919
companion object {
20-
fun findByName(name: String): MyEnum? = values().find { it.name == name }
21-
fun findByValue(value: String): MyEnum? = values().find { it.value == value }
20+
fun findByName(name: String, ignoreCase: Boolean = false): MyEnum? = values().find { it.name.equals(name, ignoreCase = ignoreCase) }
2221
}
2322
}

test/unit/should_include_dependent_types_in_onlyTypes_config/expected.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ data class NestedListType(
2525
val field: String? = null
2626
)
2727

28-
enum class MyEnum(val value: String) {
29-
This("THIS"),
30-
That("THAT");
28+
enum class MyEnum {
29+
This,
30+
That;
3131

3232
companion object {
33-
fun findByName(name: String): MyEnum? = values().find { it.name == name }
34-
fun findByValue(value: String): MyEnum? = values().find { it.value == value }
33+
fun findByName(name: String, ignoreCase: Boolean = false): MyEnum? = values().find { it.name.equals(name, ignoreCase = ignoreCase) }
3534
}
3635
}
3736

0 commit comments

Comments
 (0)