Skip to content

Commit

Permalink
Merge branch 'main' into renovate/all
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Jan 10, 2025
2 parents d5dd262 + 6d2e218 commit fd4911a
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ internal class ArrowEitherCallAdapter<E, R>(

override fun cancel() = original.cancel()

override fun execute(): Response<Either<E, R>> =
throw UnsupportedOperationException("This adapter does not support sync execution")
override fun execute(): Response<Either<E, R>> = throw UnsupportedOperationException("This adapter does not support sync execution")

override fun request(): Request = original.request()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ internal class ArrowResponseECallAdapter<E, R>(

override fun cancel() = original.cancel()

override fun execute(): Response<ResponseE<E, R>> =
throw UnsupportedOperationException("This adapter does not support sync execution")
override fun execute(): Response<ResponseE<E, R>> = throw UnsupportedOperationException("This adapter does not support sync execution")

override fun request(): Request = original.request()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public class EitherCallAdapterFactory : CallAdapter.Factory() {
}
}

private fun parseTypeName(type: Type) =
type.toString()
.split(".")
.last()
private fun parseTypeName(type: Type) = type.toString()
.split(".")
.last()
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ private class EitherCall<R>(

override fun cancel() = delegate.cancel()

override fun execute(): Response<Either<CallError, R>> =
throw UnsupportedOperationException("This adapter does not support sync execution")
override fun execute(): Response<Either<CallError, R>> = throw UnsupportedOperationException("This adapter does not support sync execution")

override fun request(): Request = delegate.request()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import io.kotest.property.Arb
import io.kotest.property.arbitrary.map
import io.kotest.property.arbitrary.of

public fun Arb.Companion.suspendFunThatThrowsFatalThrowable(): Arb<suspend () -> Either<Any, Any>> =
fatalThrowable().map { suspend { throw it } } as Arb<suspend () -> Either<Any, Any>>
public fun Arb.Companion.suspendFunThatThrowsFatalThrowable(): Arb<suspend () -> Either<Any, Any>> = fatalThrowable().map { suspend { throw it } } as Arb<suspend () -> Either<Any, Any>>

public fun Arb.Companion.fatalThrowable(): Arb<Throwable> =
Arb.of(listOf(ThreadDeath(), StackOverflowError(), OutOfMemoryError(), InterruptedException()))
public fun Arb.Companion.fatalThrowable(): Arb<Throwable> = Arb.of(listOf(ThreadDeath(), StackOverflowError(), OutOfMemoryError(), InterruptedException()))
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class OpticsProcessor(
private val codegen: CodeGenerator,
private val logger: KSPLogger,
private val options: OpticsProcessorOptions,
) :
SymbolProcessor {
) : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
val (resolved, deferred) = resolver
.getSymbolsWithAnnotation("arrow.optics.optics")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ data class OpticsProcessorOptions(
val inlineText: String = if (useInline) "inline" else ""

companion object {
fun from(options: Map<String, String>): OpticsProcessorOptions =
OpticsProcessorOptions(
useInline = options.getOrDefault("inline", "false").toBooleanStrict(),
)
fun from(options: Map<String, String>): OpticsProcessorOptions = OpticsProcessorOptions(
useInline = options.getOrDefault("inline", "false").toBooleanStrict(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.processing.SymbolProcessorProvider

class OpticsProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor =
OpticsProcessor(environment.codeGenerator, environment.logger, OpticsProcessorOptions.from(environment.options))
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = OpticsProcessor(environment.codeGenerator, environment.logger, OpticsProcessorOptions.from(environment.options))
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ data class ADT(val pckg: KSName, val declaration: KSClassDeclaration, val target
else -> "<${typeParameters.joinToString(separator = ",")}>"
}

operator fun Snippet.plus(snippet: Snippet): Snippet =
copy(imports = imports + snippet.imports, content = "$content\n${snippet.content}")
operator fun Snippet.plus(snippet: Snippet): Snippet = copy(imports = imports + snippet.imports, content = "$content\n${snippet.content}")
}

@Suppress("RecursivePropertyAccessor")
Expand Down Expand Up @@ -87,8 +86,7 @@ data class Focus(
}?.map { it.qualifiedString() }.orEmpty()

companion object {
operator fun invoke(fullName: String, paramName: String, subclasses: List<String> = emptyList()): Focus =
Focus(fullName, paramName, null, subclasses = subclasses)
operator fun invoke(fullName: String, paramName: String, subclasses: List<String> = emptyList()): Focus = Focus(fullName, paramName, null, subclasses = subclasses)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,85 +33,81 @@ fun OpticsProcessorOptions.generateIsoDsl(ele: ADT, isoOptic: ValueClassDsl): Sn
)
}

private fun OpticsProcessorOptions.processLensSyntax(ele: ADT, foci: List<Focus>, className: String): String {
return if (ele.typeParameters.isEmpty()) {
foci.joinToString(separator = "\n") { focus ->
"""
private fun OpticsProcessorOptions.processLensSyntax(ele: ADT, foci: List<Focus>, className: String): String = if (ele.typeParameters.isEmpty()) {
foci.joinToString(separator = "\n") { focus ->
"""
|${ele.visibilityModifierName} $inlineText val <__S> $Lens<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Lens<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|${ele.visibilityModifierName} $inlineText val <__S> $Optional<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Optional<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|${ele.visibilityModifierName} $inlineText val <__S> $Traversal<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Traversal<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|
""".trimMargin()
}
} else {
val sourceClassNameWithParams = "${ele.sourceClassName}${ele.angledTypeParameters}"
val joinedTypeParams = ele.typeParameters.joinToString(separator = ",")
foci.joinToString(separator = "\n") { focus ->
"""
""".trimMargin()
}
} else {
val sourceClassNameWithParams = "${ele.sourceClassName}${ele.angledTypeParameters}"
val joinedTypeParams = ele.typeParameters.joinToString(separator = ",")
foci.joinToString(separator = "\n") { focus ->
"""
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Lens<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Lens<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Optional<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Optional<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Traversal<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Traversal<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|
""".trimMargin()
}
""".trimMargin()
}
}

private fun OpticsProcessorOptions.processPrismSyntax(ele: ADT, dsl: SealedClassDsl, className: String): String =
if (ele.typeParameters.isEmpty()) {
dsl.foci.joinToString(separator = "\n\n") { focus ->
"""
private fun OpticsProcessorOptions.processPrismSyntax(ele: ADT, dsl: SealedClassDsl, className: String): String = if (ele.typeParameters.isEmpty()) {
dsl.foci.joinToString(separator = "\n\n") { focus ->
"""
|${ele.visibilityModifierName} $inlineText val <__S> $Optional<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Optional<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|${ele.visibilityModifierName} $inlineText val <__S> $Prism<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Prism<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|${ele.visibilityModifierName} $inlineText val <__S> $Traversal<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Traversal<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|
""".trimMargin()
""".trimMargin()
}
} else {
dsl.foci.joinToString(separator = "\n\n") { focus ->
val sourceClassNameWithParams = focus.refinedType?.qualifiedString() ?: "${ele.sourceClassName}${ele.angledTypeParameters}"
val joinedTypeParams = when {
focus.refinedArguments.isEmpty() -> ""
else -> focus.refinedArguments.joinToString(separator = ",")
}
} else {
dsl.foci.joinToString(separator = "\n\n") { focus ->
val sourceClassNameWithParams = focus.refinedType?.qualifiedString() ?: "${ele.sourceClassName}${ele.angledTypeParameters}"
val joinedTypeParams = when {
focus.refinedArguments.isEmpty() -> ""
else -> focus.refinedArguments.joinToString(separator = ",")
}
"""
"""
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Optional<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Optional<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Prism<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Prism<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Traversal<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Traversal<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|
""".trimMargin()
}
""".trimMargin()
}
}

private fun OpticsProcessorOptions.processIsoSyntax(ele: ADT, dsl: ValueClassDsl, className: String): String =
if (ele.typeParameters.isEmpty()) {
dsl.foci.joinToString(separator = "\n\n") { focus ->
"""
private fun OpticsProcessorOptions.processIsoSyntax(ele: ADT, dsl: ValueClassDsl, className: String): String = if (ele.typeParameters.isEmpty()) {
dsl.foci.joinToString(separator = "\n\n") { focus ->
"""
|${ele.visibilityModifierName} $inlineText val <__S> $Iso<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Iso<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|${ele.visibilityModifierName} $inlineText val <__S> $Lens<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Lens<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|${ele.visibilityModifierName} $inlineText val <__S> $Optional<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Optional<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|${ele.visibilityModifierName} $inlineText val <__S> $Prism<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Prism<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|${ele.visibilityModifierName} $inlineText val <__S> $Traversal<__S, ${ele.sourceClassName}>.${focus.escapedParamName}: $Traversal<__S, ${focus.classNameWithParameters}> $inlineText get() = this + $className.${focus.escapedParamName}
|
""".trimMargin()
""".trimMargin()
}
} else {
dsl.foci.joinToString(separator = "\n\n") { focus ->
val sourceClassNameWithParams = focus.refinedType?.qualifiedString() ?: "${ele.sourceClassName}${ele.angledTypeParameters}"
val joinedTypeParams = when {
focus.refinedArguments.isEmpty() -> ""
else -> focus.refinedArguments.joinToString(separator = ",")
}
} else {
dsl.foci.joinToString(separator = "\n\n") { focus ->
val sourceClassNameWithParams = focus.refinedType?.qualifiedString() ?: "${ele.sourceClassName}${ele.angledTypeParameters}"
val joinedTypeParams = when {
focus.refinedArguments.isEmpty() -> ""
else -> focus.refinedArguments.joinToString(separator = ",")
}
"""
"""
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Iso<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Iso<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Lens<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Lens<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Optional<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Optional<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Prism<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Prism<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|${ele.visibilityModifierName} $inlineText fun <__S,$joinedTypeParams> $Traversal<__S, $sourceClassNameWithParams>.${focus.escapedParamName}(): $Traversal<__S, ${focus.classNameWithParameters}> = this + $className.${focus.escapedParamName}()
|
""".trimMargin()
}
""".trimMargin()
}
}

private fun resolveClassName(ele: ADT): Pair<String, String> = if (hasPackageCollisions(ele)) {
val classNameAlias = ele.sourceClassName.replace(".", "").replace("`", "").sanitize()
Expand All @@ -121,11 +117,10 @@ private fun resolveClassName(ele: ADT): Pair<String, String> = if (hasPackageCol
ele.sourceClassName to ""
}

private fun hasPackageCollisions(ele: ADT): Boolean =
ele.declaration.getDeclaredProperties().let { properties ->
ele.packageName
.split(".")
.any { p ->
properties.any { it.simpleName.asString() == p }
}
}
private fun hasPackageCollisions(ele: ADT): Boolean = ele.declaration.getDeclaredProperties().let { properties ->
ele.packageName
.split(".")
.any { p ->
properties.any { it.simpleName.asString() == p }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package arrow.optics.plugin.internals

import arrow.optics.plugin.OpticsProcessorOptions

internal fun OpticsProcessorOptions.generateIsos(ele: ADT, target: IsoTarget) =
Snippet(`package` = ele.packageName, name = ele.simpleName, content = processElement(ele, target.foci.first()))
internal fun OpticsProcessorOptions.generateIsos(ele: ADT, target: IsoTarget) = Snippet(`package` = ele.packageName, name = ele.simpleName, content = processElement(ele, target.foci.first()))

private fun OpticsProcessorOptions.processElement(adt: ADT, focus: Focus): String {
val sourceClassNameWithParams = "${adt.sourceClassName}${adt.angledTypeParameters}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package arrow.optics.plugin.internals

import arrow.optics.plugin.OpticsProcessorOptions

internal fun OpticsProcessorOptions.generateLenses(ele: ADT, target: LensTarget) =
Snippet(
`package` = ele.packageName,
name = ele.simpleName,
content = processElement(ele, target.foci),
)
internal fun OpticsProcessorOptions.generateLenses(ele: ADT, target: LensTarget) = Snippet(
`package` = ele.packageName,
name = ele.simpleName,
content = processElement(ele, target.foci),
)

private fun OpticsProcessorOptions.processElement(adt: ADT, foci: List<Focus>): String {
val sourceClassNameWithParams = "${adt.sourceClassName}${adt.angledTypeParameters}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@ package arrow.optics.plugin.internals

import arrow.optics.plugin.OpticsProcessorOptions

internal fun OpticsProcessorOptions.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),
)
internal fun OpticsProcessorOptions.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 OpticsProcessorOptions.processElement(ele: ADT, foci: List<Focus>): String {
return foci.joinToString(separator = "\n\n") { focus ->
val sourceClassNameWithParams =
focus.refinedType?.qualifiedString() ?: "${ele.sourceClassName}${ele.angledTypeParameters}"
val angledTypeParameters = when {
focus.refinedArguments.isEmpty() -> ""
else -> focus.refinedArguments.joinToString(prefix = "<", separator = ",", postfix = ">")
}
val firstLine = when {
ele.typeParameters.isEmpty() ->
"${ele.visibilityModifierName} $inlineText val ${ele.sourceClassName}.Companion.${focus.escapedParamName}: $Prism<${ele.sourceClassName}, ${focus.classNameWithParameters}> $inlineText get()"
else ->
"${ele.visibilityModifierName} $inlineText fun $angledTypeParameters ${ele.sourceClassName}.Companion.${focus.escapedParamName}(): $Prism<$sourceClassNameWithParams, ${focus.classNameWithParameters}>"
}
"$firstLine = $Prism.instanceOf()"
private fun OpticsProcessorOptions.processElement(ele: ADT, foci: List<Focus>): String = foci.joinToString(separator = "\n\n") { focus ->
val sourceClassNameWithParams =
focus.refinedType?.qualifiedString() ?: "${ele.sourceClassName}${ele.angledTypeParameters}"
val angledTypeParameters = when {
focus.refinedArguments.isEmpty() -> ""
else -> focus.refinedArguments.joinToString(prefix = "<", separator = ",", postfix = ">")
}
val firstLine = when {
ele.typeParameters.isEmpty() ->
"${ele.visibilityModifierName} $inlineText val ${ele.sourceClassName}.Companion.${focus.escapedParamName}: $Prism<${ele.sourceClassName}, ${focus.classNameWithParameters}> $inlineText get()"
else ->
"${ele.visibilityModifierName} $inlineText fun $angledTypeParameters ${ele.sourceClassName}.Companion.${focus.escapedParamName}(): $Prism<$sourceClassNameWithParams, ${focus.classNameWithParameters}>"
}
"$firstLine = $Prism.instanceOf()"
}
Loading

0 comments on commit fd4911a

Please sign in to comment.