Skip to content

Commit

Permalink
Mutable DTO support
Browse files Browse the repository at this point in the history
  • Loading branch information
ermadmi78 committed Sep 11, 2024
1 parent 6e85715 commit 61b9b08
Show file tree
Hide file tree
Showing 120 changed files with 18,348 additions and 171 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryEr
kotlin.code.style=official

group = io.github.ermadmi78
version = 4.0.4-SNAPSHOT
version = 4.1.0-beta.01-SNAPSHOT

# dependencies
kotlinJdkVersion=11
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ data class KotlinLayout(
internal val KobbyNode.dtoClass: ClassName
get() = ClassName(dto.packageName, dtoName)

internal val KobbyNode.dtoLambda: LambdaTypeName
get() = LambdaTypeName.get(dtoClass, emptyList(), UNIT)

internal val KobbyNode.builderName: String
get() = dtoName.decorate(dto.builder.decoration)

Expand Down Expand Up @@ -535,6 +538,8 @@ class KotlinDtoLayout(
val enumDecoration: Decoration,
val inputDecoration: Decoration,
val applyPrimaryKeys: Boolean,
val maxNumberOfFieldsForImmutableDtoClass: Int,
val maxNumberOfFieldsForImmutableInputClass: Int,
val serialization: KotlinDtoSerialization,
val jackson: KotlinDtoJacksonLayout,
val builder: KotlinDtoBuilderLayout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class SchemaValidationTest {
Decoration(null, null),
Decoration(null, null),
true,
245,
245,
KotlinDtoSerialization(
true,
"serialization",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,26 @@ open class KobbyKotlinDtoExtension {
*/
var applyPrimaryKeys: Boolean? = null

/**
* Generate immutable DTO class if number of GraphQL type fields <= property value.
* Otherwise, generate a mutable DTO class.
* Set the property value to 0 to always generate mutable DTO classes.
* [More details](https://github.com/ermadmi78/kobby/issues/43)
*
* Default: 245
*/
var maxNumberOfFieldsForImmutableDtoClass: Int? = null

/**
* Generate immutable Input class if number of GraphQL Input type fields <= property value.
* Otherwise, generate a mutable Input class.
* Set the property value to 0 to always generate mutable Input classes.
* [More details](https://github.com/ermadmi78/kobby/issues/43)
*
* Default: 245
*/
var maxNumberOfFieldsForImmutableInputClass: Int? = null

internal val serializationExtension = lazy { KobbyKotlinDtoSerializationExtension() }
internal val jacksonExtension = lazy { KobbyKotlinDtoJacksonExtension() }
internal val builderExtension = lazy { KobbyKotlinDtoBuilderExtension() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ class KobbyPlugin : Plugin<Project> {
applyPrimaryKeys?.also {
kotlinTask.dtoApplyPrimaryKeys.convention(it)
}
maxNumberOfFieldsForImmutableDtoClass?.also {
kotlinTask.dtoMaxNumberOfFieldsForImmutableDtoClass.convention(it)
}
maxNumberOfFieldsForImmutableInputClass?.also {
kotlinTask.dtoMaxNumberOfFieldsForImmutableInputClass.convention(it)
}
serializationExtension.valueOrNull?.apply {
enabled?.also {
kotlinTask.dtoSerializationEnabled.convention(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ open class KobbyKotlin : DefaultTask() {
)
val dtoApplyPrimaryKeys: Property<Boolean> = project.objects.property(Boolean::class.java)

@Input
@Optional
@Option(
option = "dtoMaxNumberOfFieldsForImmutableDtoClass",
description = "Generate immutable DTO class if number of GraphQL type fields <= property value (default 245)"
)
val dtoMaxNumberOfFieldsForImmutableDtoClass: Property<Int> = project.objects.property(Int::class.java)

@Input
@Optional
@Option(
option = "dtoMaxNumberOfFieldsForImmutableInputClass",
description = "Generate immutable Input class if number of GraphQL Input type fields <= property value (default 245)"
)
val dtoMaxNumberOfFieldsForImmutableInputClass: Property<Int> = project.objects.property(Int::class.java)

@Input
@Optional
@Option(
Expand Down Expand Up @@ -768,6 +784,8 @@ open class KobbyKotlin : DefaultTask() {
dtoPackageName.convention("dto")
dtoPostfix.convention("Dto")
dtoApplyPrimaryKeys.convention(false)
dtoMaxNumberOfFieldsForImmutableDtoClass.convention(245)
dtoMaxNumberOfFieldsForImmutableInputClass.convention(245)

dtoSerializationEnabled.convention(project.provider {
project.hasDependency("org.jetbrains.kotlinx", "kotlinx-serialization-json")
Expand Down Expand Up @@ -916,6 +934,8 @@ open class KobbyKotlin : DefaultTask() {
Decoration(dtoEnumPrefix.orNull, dtoEnumPostfix.orNull),
Decoration(dtoInputPrefix.orNull, dtoInputPostfix.orNull),
dtoApplyPrimaryKeys.get(),
dtoMaxNumberOfFieldsForImmutableDtoClass.get(),
dtoMaxNumberOfFieldsForImmutableInputClass.get(),
KotlinDtoSerialization(
dtoSerializationEnabled.get(),
dtoSerializationClassDiscriminator.get(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io.github.ermadmi78.kobby.kobby

repositories {
mavenLocal()
mavenCentral()
}

buildscript {
repositories {
mavenLocal()
mavenCentral()
}
}

plugins {
kotlin("jvm") version "testKotlinVersion"
id("io.github.ermadmi78.kobby")
}

kobby {
kotlin {
scalars = mapOf(
"Date" to typeOf("java.time", "LocalDate"),
"JSON" to typeMap.parameterize(typeString, typeAny.nullable())
)
dto {
maxNumberOfFieldsForImmutableDtoClass = 0
maxNumberOfFieldsForImmutableInputClass = 0
}
}
}

dependencies {
// Add this dependency to enable Jackson annotation generation in DTO classes by Kobby
compileOnly("com.fasterxml.jackson.core:jackson-annotations:testJacksonVersion")

// Add this dependency to enable default Ktor adapters generation
compileOnly("io.ktor:ktor-client-cio:testKtorVersion")

// Add this dependencies to remove warning "Runtime JAR files in the classpath should have the same version"
compileOnly(kotlin("stdlib"))
compileOnly(kotlin("stdlib-jdk7"))
compileOnly(kotlin("reflect"))
}

tasks {
jar {
enabled = false
}
}
Loading

0 comments on commit 61b9b08

Please sign in to comment.