-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Annotation processor for [at]bounded
- Loading branch information
1 parent
b9d9871
commit 3541590
Showing
7 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
arrow-annotations-processor/src/main/java/arrow/optics/BoundLensGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package arrow.optics | ||
|
||
import arrow.common.utils.fullName | ||
import me.eugeniomarletti.kotlin.metadata.escapedClassName | ||
import java.io.File | ||
|
||
class BoundLensGenerator( | ||
private val annotatedList: Collection<AnnotatedOptic>, | ||
private val generatedDir: File | ||
) { | ||
|
||
private val boundLens = "arrow.optics.BoundSetter" | ||
|
||
fun generate() = annotatedList.map(this::processElement) | ||
.map { (element, funs) -> | ||
"${boundAnnotationClass.simpleName}.${element.classData.`package`}.${element.type.simpleName.toString().toLowerCase()}.kt" to | ||
funs.joinToString(prefix = "package ${element.classData.`package`.escapedClassName}\n\n", separator = "\n") | ||
}.forEach { (name, fileString) -> File(generatedDir, name).writeText(fileString) } | ||
|
||
private fun String.toUpperCamelCase(): String = split(" ").joinToString("", transform = String::capitalize) | ||
|
||
private fun processElement(annotatedOptic: AnnotatedOptic): Pair<AnnotatedOptic, List<String>> = | ||
annotatedOptic to annotatedOptic.targets.map { variable -> | ||
val sourceClassName = annotatedOptic.classData.fullName.escapedClassName | ||
val sourceName = annotatedOptic.type.simpleName.toString().decapitalize() | ||
val targetClassName = variable.fullName | ||
val targetName = variable.paramName | ||
|
||
""" | ||
|val <T> $boundLens<T, $sourceClassName>.$targetName: $boundLens<T, $targetClassName> get() = this.compose($sourceName${targetName.toUpperCamelCase()}()) | ||
""".trimMargin() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package arrow.optics | ||
|
||
import arrow.bounded | ||
import arrow.lenses | ||
import io.kotlintest.matchers.shouldBe | ||
import io.kotlintest.specs.StringSpec | ||
|
||
@bounded data class Street(val number: Int, val name: String) | ||
@bounded data class Address(val city: String, val street: Street) | ||
@bounded data class Company(val name: String, val address: Address) | ||
@bounded data class Employee(val name: String, val company: Company?) | ||
|
||
//TODO: Rewrite to annotation processor test when multiple file support is implemented | ||
class BoundedTest: StringSpec() { | ||
|
||
init { | ||
|
||
"@bounded classes are generated properly" { | ||
|
||
val employee = Employee("John Doe", Company("Kategory", Address("Functional city", Street(42, "lambda street")))) | ||
val newEmployee = employee.setter().company.nullable.address.street.name.modify { it.capitalize() } | ||
|
||
val expected = Employee("John Doe", Company("Kategory", Address("Functional city", Street(42, "Lambda street")))) | ||
|
||
newEmployee shouldBe expected | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters