-
Notifications
You must be signed in to change notification settings - Fork 437
/
prism.kt
25 lines (23 loc) · 875 Bytes
/
prism.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
package arrow.optics.plugin.internals
internal fun generatePrisms(ele: ADT, target: PrismTarget) =
Snippet(
`package` = ele.packageName,
name = ele.simpleName,
imports =
setOf("import arrow.core.left", "import arrow.core.right", "import arrow.core.identity"),
content = processElement(ele, target.foci)
)
private fun processElement(ele: ADT, foci: List<Focus>): String =
foci.joinToString(separator = "\n\n") { focus ->
"""
|inline val ${ele.sourceClassName}.Companion.${focus.paramName}: $Prism<${ele.sourceClassName}, ${focus.className}> inline get()= $Prism(
| getOrModify = { ${ele.sourceName}: ${ele.sourceClassName} ->
| when (${ele.sourceName}) {
| is ${focus.className} -> ${ele.sourceName}.right()
| else -> ${ele.sourceName}.left()
| }
| },
| reverseGet = ::identity
|)
|""".trimMargin()
}