Skip to content

Commit

Permalink
Don't allow arbitrary modifiers for addEqualsAndHashCodeBasedOnClassA…
Browse files Browse the repository at this point in the history
…ndSingleProperty
  • Loading branch information
lukellmann committed Aug 21, 2023
1 parent fdf16a2 commit 0da4c62
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGen
}
addPlus(parameterName = "flag", parameterType = entityCN)
addPlus(parameterName = "flags", parameterType = collectionCN)
addEqualsAndHashCodeBasedOnClassAndSingleProperty(entityCN, property = "shift", FINAL)
addEqualsAndHashCodeBasedOnClassAndSingleProperty(entityCN, property = "shift", isFinal = true)
addFunction("toString") {
addModifiers(FINAL, OVERRIDE)
returns<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal fun KordEnum.generateFileSpec(originatingFile: KSFile) = fileSpecForGen
addKdoc("The raw $valueName used by Discord.")
initializer(valueName)
}
addEqualsAndHashCodeBasedOnClassAndSingleProperty(entityCN, property = valueName, FINAL)
addEqualsAndHashCodeBasedOnClassAndSingleProperty(entityCN, property = valueName, isFinal = true)
addFunction("toString") {
addModifiers(FINAL, OVERRIDE)
returns<String>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.kord.ksp.generation.shared

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.KModifier.FINAL
import com.squareup.kotlinpoet.KModifier.OVERRIDE
import com.squareup.kotlinpoet.TypeSpec
import dev.kord.ksp.addFunction
Expand All @@ -11,16 +11,17 @@ import dev.kord.ksp.returns
internal fun TypeSpec.Builder.addEqualsAndHashCodeBasedOnClassAndSingleProperty(
className: ClassName,
property: String,
vararg modifiers: KModifier,
isFinal: Boolean = false,
) {
val final = if (isFinal) arrayOf(FINAL) else emptyArray()
addFunction("equals") {
addModifiers(OVERRIDE, *modifiers)
returns<Boolean>()
addModifiers(*final, OVERRIDE)
addParameter<Any?>("other")
returns<Boolean>()
addStatement("return this·===·other || (other·is·%T·&&·this.$property·==·other.$property)", className)
}
addFunction("hashCode") {
addModifiers(OVERRIDE, *modifiers)
addModifiers(*final, OVERRIDE)
returns<Int>()
addStatement("return $property.hashCode()")
}
Expand Down

0 comments on commit 0da4c62

Please sign in to comment.