Skip to content

Commit

Permalink
Revert "Add Boolean fields to Data classes for supporting sparse upda…
Browse files Browse the repository at this point in the history
…te (#664)"

This reverts commit 89ed33a.
  • Loading branch information
srinivasankavitha committed May 16, 2024
1 parent 467ba8a commit 08eb4ad
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CodeGenCli : CliktCommand("Generate Java sources for SCHEMA file(s)") {
)
private val language by option("--language", "-l", help = "Output language").choice("java", "kotlin", ignoreCase = true)
.default("java")
private val generateClient by option("--generate-client", "-c", help = "Generate client api").flag(default = false)
private val generateClient by option("--generate-client", "-c", help = "Genereate client api").flag(default = false)
private val generateDataTypes by option(
"--generate-data-types",
help = "Generate data types. Not needed when only generating an API"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
package com.netflix.graphql.dgs.codegen.generators.java

import com.netflix.graphql.dgs.codegen.*
import com.netflix.graphql.dgs.codegen.generators.shared.CodeGeneratorUtils.capitalized
import com.netflix.graphql.dgs.codegen.generators.shared.SiteTarget
import com.netflix.graphql.dgs.codegen.generators.shared.applyDirectivesJava
import com.squareup.javapoet.*
import graphql.language.*
import graphql.language.TypeName
import graphql.schema.idl.TypeUtil
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.Serializable
Expand Down Expand Up @@ -83,15 +81,13 @@ class DataTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTyp
if (config.generateDataTypes) {
val fieldDefinitions = definition.fieldDefinitions
.filterSkipped()
.map { fieldDefinition ->
val isNullable = !TypeUtil.isNonNull(fieldDefinition.type)
.map {
Field(
fieldDefinition.name,
typeUtils.findReturnType(fieldDefinition.type, useInterfaceType, true),
it.name,
typeUtils.findReturnType(it.type, useInterfaceType, true),
overrideGetter = overrideGetter,
description = fieldDefinition.description,
directives = fieldDefinition.directives,
isNullable = isNullable
description = it.description,
directives = it.directives
)
}
.plus(
Expand Down Expand Up @@ -126,7 +122,6 @@ class InputTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTy

val name = definition.name
val fieldDefinitions = definition.inputValueDefinitions.map {
val isNullable = !TypeUtil.isNonNull(it.type)
val defaultValue = it.defaultValue?.let { defVal ->
when (defVal) {
is BooleanValue -> CodeBlock.of("\$L", defVal.isValue)
Expand Down Expand Up @@ -160,8 +155,7 @@ class InputTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTy
type = typeUtils.findReturnType(it.type),
initialValue = defaultValue,
description = it.description,
directives = it.directives,
isNullable = isNullable
directives = it.directives
)
}.plus(extensions.flatMap { it.inputValueDefinitions }.map { Field(it.name, typeUtils.findReturnType(it.type)) })
return generate(name, emptyList(), fieldDefinitions, definition.description, definition.directives)
Expand All @@ -173,7 +167,7 @@ class InputTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTy
}
}

internal data class Field(val name: String, val type: com.squareup.javapoet.TypeName, val initialValue: CodeBlock? = null, val overrideGetter: Boolean = false, val interfaceType: com.squareup.javapoet.TypeName? = null, val description: Description? = null, val directives: List<Directive> = listOf(), val isNullable: Boolean = true)
internal data class Field(val name: String, val type: com.squareup.javapoet.TypeName, val initialValue: CodeBlock? = null, val overrideGetter: Boolean = false, val interfaceType: com.squareup.javapoet.TypeName? = null, val description: Description? = null, val directives: List<Directive> = listOf())

abstract class BaseDataTypeGenerator(
internal val packageName: String,
Expand Down Expand Up @@ -233,7 +227,7 @@ abstract class BaseDataTypeGenerator(

addEquals(javaType)
addHashcode(javaType)
addBuilder(fields, javaType)
addBuilder(javaType)

val javaFile = JavaFile.builder(packageName, javaType.build()).build()

Expand Down Expand Up @@ -347,22 +341,7 @@ abstract class BaseDataTypeGenerator(
constructorBuilder
.addParameter(parameterBuilder.build())
.addModifiers(Modifier.PUBLIC)
}

fieldDefinitions.forEach {
val constructor = constructorBuilder
.addStatement(
"this.\$N = \$N",
ReservedKeywordSanitizer.sanitize(it.name),
ReservedKeywordSanitizer.sanitize(it.name)
)
if (it.isNullable && it.initialValue == null) {
constructorBuilder
.addStatement(
"this.\$N = true",
generateBooleanFieldName(ReservedKeywordSanitizer.sanitize(it.name))
)
}
.addStatement("this.\$N = \$N", ReservedKeywordSanitizer.sanitize(it.name), ReservedKeywordSanitizer.sanitize(it.name))
}

javaType.addMethod(constructorBuilder.build())
Expand All @@ -381,35 +360,6 @@ abstract class BaseDataTypeGenerator(

private fun addField(fieldDefinition: Field, javaType: TypeSpec.Builder) {
addFieldWithGetterAndSetter(fieldDefinition.type, fieldDefinition, javaType)
// Generate for all nullable fields without any defaults
if (fieldDefinition.isNullable && fieldDefinition.initialValue == null) {
addIsDefinedFieldWithGetters(fieldDefinition, javaType)
}
}

private fun addIsDefinedFieldWithGetters(fieldDefinition: Field, javaType: TypeSpec.Builder) {
val fieldName = generateBooleanFieldName(ReservedKeywordSanitizer.sanitize(fieldDefinition.name))
val field = FieldSpec
.builder(com.squareup.javapoet.TypeName.BOOLEAN, fieldName)
.addModifiers(Modifier.PRIVATE)
.initializer("false")
.build()
val getterName = "${fieldName}Defined"

val getter = MethodSpec
.methodBuilder(getterName)
.addModifiers(Modifier.PUBLIC)
.returns(com.squareup.javapoet.TypeName.BOOLEAN)
.addStatement(
"return \$N",
fieldName
).build()
javaType.addField(field)
javaType.addMethod(getter)
}

private fun generateBooleanFieldName(name: String): String {
return "is${name.capitalized()}"
}

private fun addFieldWithGetterAndSetter(returnType: com.squareup.javapoet.TypeName?, fieldDefinition: Field, javaType: TypeSpec.Builder) {
Expand Down Expand Up @@ -440,21 +390,13 @@ abstract class BaseDataTypeGenerator(

val setterName = typeUtils.transformIfDefaultClassMethodExists("set${fieldDefinition.name[0].uppercase()}${fieldDefinition.name.substring(1)}", TypeUtils.Companion.setClass)
val parameterBuilder = ParameterSpec.builder(returnType, ReservedKeywordSanitizer.sanitize(fieldDefinition.name))

val setterMethodBuilder = MethodSpec.methodBuilder(setterName)
.addModifiers(Modifier.PUBLIC)
.addStatement(
"this.\$N = \$N",
ReservedKeywordSanitizer.sanitize(fieldDefinition.name),
ReservedKeywordSanitizer.sanitize(fieldDefinition.name)
)
if (fieldDefinition.isNullable && fieldDefinition.initialValue == null) {
setterMethodBuilder
.addStatement(
"this.\$N = true",
generateBooleanFieldName(ReservedKeywordSanitizer.sanitize(fieldDefinition.name))
)
}

if (fieldDefinition.directives.isNotEmpty()) {
val (annotations, comments) = applyDirectivesJava(fieldDefinition.directives, config)
Expand Down Expand Up @@ -489,13 +431,13 @@ abstract class BaseDataTypeGenerator(
)
}

private fun addBuilder(fields: List<Field>, javaType: TypeSpec.Builder) {
private fun addBuilder(javaType: TypeSpec.Builder) {
val className = ClassName.get(packageName, javaType.build().name)
val buildMethod = MethodSpec.methodBuilder("build").returns(className).addStatement(
"""
$className result = new $className();
${javaType.build().fieldSpecs.joinToString("\n") { "result.${it.name} = this.${it.name};".trimIndent() }}
return result
$className result = new $className();
${javaType.build().fieldSpecs.joinToString("\n") { "result.${it.name} = this.${it.name};" }}
return result
""".trimIndent()
).addModifiers(Modifier.PUBLIC).build()

Expand All @@ -518,23 +460,10 @@ abstract class BaseDataTypeGenerator(
.addMethod(buildMethod)

javaType.build().fieldSpecs.map {
val method = MethodSpec.methodBuilder(it.name)
MethodSpec.methodBuilder(it.name)
.addJavadoc(it.javadoc)
.returns(builderClassName)
.addStatement("this.${it.name} = ${it.name}")

val fieldName = it.name
val field = fields.find { it.name.contains(fieldName) }

if (field?.isNullable == true && field.initialValue == null) {
method
.addStatement(
"this.\$N = true",
generateBooleanFieldName(it.name)
)
}

method
.addStatement("return this")
.addParameter(ParameterSpec.builder(it.type, it.name).build())
.addModifiers(Modifier.PUBLIC).build()
Expand Down
Loading

0 comments on commit 08eb4ad

Please sign in to comment.