-
Notifications
You must be signed in to change notification settings - Fork 437
/
processor.kt
124 lines (111 loc) · 4.14 KB
/
processor.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package arrow.optics.plugin.internals
import arrow.optics.plugin.isData
import arrow.optics.plugin.isSealed
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeArgument
import java.util.Locale
internal fun adt(c: KSClassDeclaration, logger: KSPLogger): ADT =
ADT(
c.packageName,
c,
c.targets().map { target ->
when (target) {
OpticsTarget.LENS ->
evalAnnotatedDataClass(c, c.qualifiedNameOrSimpleName.lensErrorMessage, logger)
.let(::LensTarget)
OpticsTarget.OPTIONAL ->
evalAnnotatedDataClass(c, c.qualifiedNameOrSimpleName.optionalErrorMessage, logger)
.let(::OptionalTarget)
OpticsTarget.ISO -> evalAnnotatedIsoElement(c, logger).let(::IsoTarget)
OpticsTarget.PRISM -> evalAnnotatedPrismElement(c, logger).let(::PrismTarget)
OpticsTarget.DSL -> evalAnnotatedDslElement(c, logger)
}
}
)
internal fun KSClassDeclaration.targets(): List<OpticsTarget> =
if (isSealed) listOf(OpticsTarget.PRISM, OpticsTarget.DSL)
else listOf(OpticsTarget.ISO, OpticsTarget.LENS, OpticsTarget.OPTIONAL, OpticsTarget.DSL)
internal fun evalAnnotatedPrismElement(
element: KSClassDeclaration,
logger: KSPLogger
): List<Focus> =
when {
element.isSealed ->
element.sealedSubclassFqNameList().map {
Focus(
it,
it.substringAfterLast(".").replaceFirstChar { c -> c.lowercase(Locale.getDefault()) }
)
}
else -> {
logger.error(element.qualifiedNameOrSimpleName.prismErrorMessage, element)
emptyList()
}
}
internal val KSDeclaration.qualifiedNameOrSimpleName: String
get() = (qualifiedName ?: simpleName).asString()
internal fun KSClassDeclaration.sealedSubclassFqNameList(): List<String> =
getSealedSubclasses().mapNotNull { it.qualifiedName?.asString() }.toList()
internal fun evalAnnotatedDataClass(
element: KSClassDeclaration,
errorMessage: String,
logger: KSPLogger
): List<Focus> =
when {
element.isData ->
element
.getConstructorTypesNames()
.zip(element.getConstructorParamNames(), Focus.Companion::invoke)
else -> {
logger.error(errorMessage, element)
emptyList()
}
}
internal fun evalAnnotatedDslElement(element: KSClassDeclaration, logger: KSPLogger): Target =
when {
element.isData ->
DataClassDsl(
element
.getConstructorTypesNames()
.zip(element.getConstructorParamNames(), Focus.Companion::invoke)
)
element.isSealed -> SealedClassDsl(evalAnnotatedPrismElement(element, logger))
else -> throw IllegalStateException("should only be sealed or data by now")
}
internal fun evalAnnotatedIsoElement(element: KSClassDeclaration, logger: KSPLogger): List<Focus> =
when {
element.isData ->
element
.getConstructorTypesNames()
.zip(element.getConstructorParamNames(), Focus.Companion::invoke)
.takeIf { it.size <= 22 }
?: run {
logger.error(element.qualifiedNameOrSimpleName.isoTooBigErrorMessage, element)
emptyList()
}
else -> {
logger.error(element.qualifiedNameOrSimpleName.isoErrorMessage, element)
emptyList()
}
}
internal fun KSClassDeclaration.getConstructorTypesNames(): List<String> =
primaryConstructor?.parameters?.map { it.type.resolve().qualifiedString() }.orEmpty()
internal fun KSType.qualifiedString(): String = when (val qname = declaration.qualifiedName?.asString()) {
null -> toString()
else -> {
val withArgs = when {
arguments.isEmpty() -> qname
else -> "$qname<${arguments.joinToString(separator = ", ") { it.qualifiedString() }}>"
}
if (isMarkedNullable) "$withArgs?" else withArgs
}
}
internal fun KSTypeArgument.qualifiedString(): String = when (val ty = type?.resolve()) {
null -> toString()
else -> ty.qualifiedString()
}
internal fun KSClassDeclaration.getConstructorParamNames(): List<String> =
primaryConstructor?.parameters?.mapNotNull { it.name?.asString() }.orEmpty()