From d33cbe29af2b0fdab72370f12b3c7ddfe15731bb Mon Sep 17 00:00:00 2001 From: Fabian Fritzsche Date: Mon, 15 Apr 2024 04:08:50 +0200 Subject: [PATCH] Add companion object generation for enum classes in kotlin codegen2 (#670) * Add companion object generation to kotlin codegen2 * Fix indentation * Fix outdated "expected" values of tests --- .../enum/expected/types/EmployeeTypes.kt | 3 ++ .../cases/enumDocs/expected/types/Color.kt | 3 ++ .../expected/types/EmployeeTypes.kt | 3 ++ .../expected/types/Color.kt | 3 ++ .../expected/types/Color.kt | 3 ++ .../projectionWithEnum/expected/types/E.kt | 3 ++ .../kotlin2/GenerateKotlin2EnumTypes.kt | 5 +++ .../dgs/codegen/Kotline2CodeGenTest.kt | 42 +++++++++++++++++++ 8 files changed, 65 insertions(+) diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enum/expected/types/EmployeeTypes.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enum/expected/types/EmployeeTypes.kt index 1bc384a8d..dd96f9cea 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enum/expected/types/EmployeeTypes.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enum/expected/types/EmployeeTypes.kt @@ -4,4 +4,7 @@ public enum class EmployeeTypes { ENGINEER, MANAGER, DIRECTOR, + ; + + public companion object } diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enumDocs/expected/types/Color.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enumDocs/expected/types/Color.kt index 3aea3b1fb..d0659fffc 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enumDocs/expected/types/Color.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enumDocs/expected/types/Color.kt @@ -7,4 +7,7 @@ public enum class Color { red, white, blue, + ; + + public companion object } diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enumWithExtendedType/expected/types/EmployeeTypes.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enumWithExtendedType/expected/types/EmployeeTypes.kt index 2208fc91e..a35b0b946 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enumWithExtendedType/expected/types/EmployeeTypes.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/enumWithExtendedType/expected/types/EmployeeTypes.kt @@ -5,4 +5,7 @@ public enum class EmployeeTypes { MANAGER, DIRECTOR, QA, + ; + + public companion object } diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/inputWithDefaultEnumValueForArray/expected/types/Color.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/inputWithDefaultEnumValueForArray/expected/types/Color.kt index 1d897afea..81dd3e940 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/inputWithDefaultEnumValueForArray/expected/types/Color.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/inputWithDefaultEnumValueForArray/expected/types/Color.kt @@ -3,4 +3,7 @@ package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultEnumValueForArray. public enum class Color { red, blue, + ; + + public companion object } diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/inputWithDefaultValueForEnum/expected/types/Color.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/inputWithDefaultValueForEnum/expected/types/Color.kt index 62c32f1ba..54a69ccb4 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/inputWithDefaultValueForEnum/expected/types/Color.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/inputWithDefaultValueForEnum/expected/types/Color.kt @@ -2,4 +2,7 @@ package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForEnum.expec public enum class Color { red, + ; + + public companion object } diff --git a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithEnum/expected/types/E.kt b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithEnum/expected/types/E.kt index 9de42a388..7b7d27d36 100644 --- a/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithEnum/expected/types/E.kt +++ b/graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/cases/projectionWithEnum/expected/types/E.kt @@ -2,4 +2,7 @@ package com.netflix.graphql.dgs.codegen.cases.projectionWithEnum.expected.types public enum class E { V, + ; + + public companion object } diff --git a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2EnumTypes.kt b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2EnumTypes.kt index 0cba5ad2f..d9a4a262a 100644 --- a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2EnumTypes.kt +++ b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2EnumTypes.kt @@ -54,6 +54,10 @@ fun generateKotlin2EnumTypes( .plus(extensionTypes) .flatMap { it.enumValueDefinitions } + val companionObject = TypeSpec.companionObjectBuilder() + .addOptionalGeneratedAnnotation(config) + .build() + // create the enum class val enumSpec = TypeSpec.classBuilder(enumDefinition.name) .addOptionalGeneratedAnnotation(config) @@ -80,6 +84,7 @@ fun generateKotlin2EnumTypes( .build() } ) + .addType(companionObject) .build() // return a file per enum diff --git a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/Kotline2CodeGenTest.kt b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/Kotline2CodeGenTest.kt index b794627f6..c17ab3874 100644 --- a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/Kotline2CodeGenTest.kt +++ b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/Kotline2CodeGenTest.kt @@ -85,6 +85,9 @@ class Kotline2CodeGenTest { |public enum class TownJobTypes { | @Deprecated(message = "town switched to electric lights") | LAMPLIGHTER, + | ; + | + | public companion object |} | """.trimMargin() @@ -92,4 +95,43 @@ class Kotline2CodeGenTest { ) assertCompilesKotlin(result.kotlinEnumTypes) } + + @Test + fun `Add companion object to enum class`() { + val schema = """ + enum MyEnum { + A + B + C + } + """.trimIndent() + + val result = CodeGen( + CodeGenConfig( + schemas = setOf(schema), + packageName = basePackageName, + language = Language.KOTLIN + ) + ).generate() + + val type = result.kotlinEnumTypes[0].members[0] as TypeSpec + + assertThat(FileSpec.get("$basePackageName.enums", type).toString()).isEqualTo( + """ + |package com.netflix.graphql.dgs.codegen.tests.generated.enums + | + |public enum class MyEnum { + | A, + | B, + | C, + | ; + | + | public companion object + |} + | + """.trimMargin() + ) + + assertCompilesKotlin(result.kotlinEnumTypes) + } }