Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optics ksp plugin: fixed handling of variance (#3057) #3060

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import arrow.optics.plugin.isSealed
import arrow.optics.plugin.isValue
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.Variance.INVARIANT
import java.util.Locale

internal fun adt(c: KSClassDeclaration, logger: KSPLogger): ADT =
Expand Down Expand Up @@ -153,12 +154,12 @@ internal fun evalAnnotatedIsoElement(
internal fun KSClassDeclaration.getConstructorTypesNames(): List<String> =
primaryConstructor?.parameters?.map { it.type.resolve().qualifiedString() }.orEmpty()

internal fun KSType.qualifiedString(): String = when (declaration) {
internal fun KSType.qualifiedString(prefix: String = ""): String = when (declaration) {
is KSTypeParameter -> {
val n = declaration.simpleName.asSanitizedString()
val n = declaration.simpleName.asSanitizedString(prefix = prefix)
if (isMarkedNullable) "$n?" else n
}
else -> when (val qname = declaration.qualifiedName?.asSanitizedString()) {
else -> when (val qname = declaration.qualifiedName?.asSanitizedString(prefix = prefix)) {
null -> toString()
else -> {
val withArgs = when {
Expand All @@ -172,7 +173,7 @@ internal fun KSType.qualifiedString(): String = when (declaration) {

internal fun KSTypeArgument.qualifiedString(): String = when (val ty = type?.resolve()) {
null -> toString()
else -> ty.qualifiedString()
else -> ty.qualifiedString(prefix = "${variance.label} ".takeIf { variance != INVARIANT }.orEmpty())
}

internal fun KSClassDeclaration.getConstructorParamNames(): List<String> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fun String.plusIfNotBlank(prefix: String = "", postfix: String = "") =
/**
* Sanitizes each delimited section if it matches with Kotlin reserved keywords.
*/
fun KSName.asSanitizedString(delimiter: String = ".") =
asString().splitToSequence(delimiter).joinToString(delimiter) { if (it in kotlinKeywords) "`$it`" else it }
fun KSName.asSanitizedString(delimiter: String = ".", prefix: String = "") =
asString().splitToSequence(delimiter).joinToString(delimiter, prefix) { if (it in kotlinKeywords) "`$it`" else it }


private val kotlinKeywords = setOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,23 @@ class DSLTests {
""".compilationSucceeds()
}

@Test
fun `DSL works with variance, issue #3057`() {
"""
|$`package`
|$imports
|
|sealed interface ITest {
| data class Test1(val test: String) : ITest
|}
|
|interface Extendable<T>
|@optics
|data class TestClass(val details: Extendable<out ITest>) {
| companion object
|}
""".compilationSucceeds()
}

// Db.content.at(At.map(), One).set(db, None)
}