Skip to content

Commit

Permalink
Fix optics generation for object name (#3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Sep 10, 2024
1 parent a021927 commit 137bcdc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.util.Locale

data class ADT(val pckg: KSName, val declaration: KSClassDeclaration, val targets: List<Target>) {
val sourceClassName = declaration.qualifiedNameOrSimpleName
val sourceName = declaration.simpleName.asString().replaceFirstChar { it.lowercase(Locale.getDefault()) }
val sourceName = declaration.simpleName.asString().replaceFirstChar { it.lowercase(Locale.getDefault()) }.sanitize()
val simpleName = declaration.nameWithParentClass
val packageName = pckg.asSanitizedString()
val visibilityModifierName = when (declaration.companionObject?.getVisibility()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ private fun OpticsProcessorOptions.processElement(adt: ADT, foci: List<Focus>):

val setBody = { focus: Focus ->
val setBodyCopy = """${adt.sourceName}.copy(${
focus.paramName.plusIfNotBlank(
prefix = "`",
postfix = "`",
)
focus.escapedParamName
} = value)"""
when {
focus.subclasses.isNotEmpty() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,37 @@ class DSLTests {
|}
""".compilationSucceeds()
}

@Test
fun `Using Object as the name a class, prisms, #3474`() {
"""
|$`package`
|$imports
|
|@optics
|sealed interface Thing {
| data class Object(val value: Int) : Thing
| companion object
|}
|
|val i: Prism<Thing, Thing.Object> = Thing.`object`
|val r = i != null
""".evals("r" to true)
}

@Test
fun `Using Object as the name a class, lenses, #3474`() {
"""
|$`package`
|$imports
|
|@optics
|data class Object(val value: Int) {
| companion object
|}
|
|val i: Lens<Object, Int> = Object.value
|val r = i != null
""".evals("r" to true)
}
}

0 comments on commit 137bcdc

Please sign in to comment.