Skip to content

Commit

Permalink
Code cleanup in DataClass now that we have ClassName passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
baconz committed Aug 29, 2023
1 parent aaa31d4 commit 573937c
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,21 @@ fun TypeSpec.Builder.addGeneratedMethods(generateMethods: List<GeneratedMethod>,

if (generateMethods.contains(TO_STRING)) {
if (propertySpecs.isNotEmpty()) {
withToStringImplementation()
withToStringImplementation(className)
}
}

if (generateMethods.contains(COPY)) {
if (propertySpecs.isNotEmpty()) {
withCopyImplementation()
withCopyImplementation(className)
}
}
}

internal fun TypeSpec.Builder.withCopyImplementation(): TypeSpec.Builder {
internal fun TypeSpec.Builder.withCopyImplementation(className: ClassName): TypeSpec.Builder {
// Note that we need to build the type to get its name because kotlin poet keeps the name internal to the builder
val type = build()
val constructorParamNames = type.primaryConstructor?.parameters?.map { it.name }?.toSet() ?: return this
val typeName = type.name ?: return this
val className = ClassName("", typeName)
val constructorProperties = propertySpecs
.excludeInternalProperties()
.filter { constructorParamNames.contains(it.name) }
Expand Down Expand Up @@ -139,8 +137,8 @@ internal fun TypeSpec.Builder.withEqualsImplementation(className: ClassName): Ty
.addStatement("this.%L == other.%L", property.name, property.name).build()
}

fun methodCode(typeName: String?): CodeBlock {
if (typeName == null) {
fun methodCode(): CodeBlock {
if (propertySpecs.isEmpty()) {
return CodeBlock.builder()
.addStatement("return·other != null·&&·other::class·==·this::class")
.build()
Expand Down Expand Up @@ -170,7 +168,7 @@ internal fun TypeSpec.Builder.withEqualsImplementation(className: ClassName): Ty
.addModifiers(KModifier.OVERRIDE)
.addParameter("other", KotlinSymbols.Any.copy(nullable = true))
.returns(KotlinSymbols.Boolean)
.addCode(methodCode(build().name))
.addCode(methodCode())
.build())
}

Expand Down Expand Up @@ -221,11 +219,9 @@ internal fun TypeSpec.Builder.withHashCodeImplementation(): TypeSpec.Builder = a
)
}

internal fun TypeSpec.Builder.withToStringImplementation(): TypeSpec.Builder {
// Note that we need to build the type to get its name because kotlin poet keeps the name internal to the builder
val name = build().name ?: return this
internal fun TypeSpec.Builder.withToStringImplementation(className: ClassName): TypeSpec.Builder {
fun printPropertiesTemplate() =
"$name(" + propertySpecs
"${className.simpleName}(" + propertySpecs
.excludeInternalProperties()
.joinToString(",") { "${it.name}=\$${it.name}" } + ")"

Expand Down

0 comments on commit 573937c

Please sign in to comment.