Skip to content

Commit

Permalink
method references supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Abel committed Nov 21, 2024
1 parent 69019f5 commit b3c1489
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private class ClassMappingStatementCollector(private val context: ResolverContex
}
IDENTIFIER_FROM_EXPRESSION -> {
val target = expression.extensionReceiver!!.accept(TargetNameCollector(context), data)
target to ExpressionMappingSource(expression.valueArguments.first() as IrFunctionExpression)
target to ExpressionMappingSource(expression.valueArguments.first() as IrExpression)
}
IDENTIFIER_VIA -> {
expression.dispatchReceiver!!.accept(data).let { (name, source) ->
Expand All @@ -66,7 +66,13 @@ private class ClassMappingStatementCollector(private val context: ResolverContex
IDENTIFIER_TRANSFORM -> {
expression.dispatchReceiver!!.accept(data).let { (name, source) ->
name to (source as ExplicitPropertyMappingSource).copy(
transformation = PropertyMappingTransformTranformation(expression.valueArguments.first()!! as IrFunctionExpression)
transformation = expression.valueArguments.first().let {
when (it) {
is IrFunctionExpression -> PropertyMappingTransformTranformation(it)
is IrFunctionReference -> PropertyMappingTransformTranformation(it)
else -> throw MappiePanicException("Unexpected expression type: ${expression.type}")
}
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionReference
import org.jetbrains.kotlin.ir.expressions.IrPropertyReference
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.name.Name
Expand Down Expand Up @@ -85,10 +86,12 @@ sealed interface PropertyMappingTransformation {
val type: IrType
}

data class PropertyMappingTransformTranformation(
val function: IrFunctionExpression,
data class PropertyMappingTransformTranformation private constructor(
val function: IrExpression,
override val type: IrType,
) : PropertyMappingTransformation {
override val type = function.function.returnType
constructor(functionReference: IrFunctionReference) : this(functionReference, functionReference.symbol.owner.returnType)
constructor(functionExpression: IrFunctionExpression) : this(functionExpression, functionExpression.function.returnType)
}

data class PropertyMappingViaMapperTransformation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package tech.mappie.testing.objects

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import tech.mappie.testing.compilation.compile
import tech.mappie.testing.loadObjectMappieClass
import java.io.File

class MethodReferenceTest {

data class Input(val x: Int)
data class Output(val value: String)

@TempDir
lateinit var directory: File

@Test
fun `map property fromExpression should succeed with method reference`() {
compile(directory) {
file("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.objects.MethodReferenceTest.*
class Mapper : ObjectMappie<Int, Output>() {
override fun map(from: Int) = mapping {
Output::value fromExpression Int::toString
}
}
"""
)
} satisfies {
isOk()
hasNoMessages()

val mapper = classLoader
.loadObjectMappieClass<Int, Output>("Mapper")
.constructors
.first()
.call()

assertThat(mapper.map(101)).isEqualTo(Output("101"))
}
}

@Test
fun `map property fromProperty should succeed with method reference transform`() {
compile(directory) {
file("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.objects.MethodReferenceTest.*
class Mapper : ObjectMappie<Input, Output>() {
override fun map(from: Input) = mapping {
Output::value fromProperty from::x transform Int::toString
}
}
"""
)
} satisfies {
isOk()
hasNoMessages()

val mapper = classLoader
.loadObjectMappieClass<Input, Output>("Mapper")
.constructors
.first()
.call()

assertThat(mapper.map(Input(101))).isEqualTo(Output("101"))
}
}
}
15 changes: 13 additions & 2 deletions gradle-plugin/src/test/kotlin/tech/mappie/testing/TestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ abstract class TestBase {
"""
pluginManagement {
repositories {
maven(url = "https://dso-nexus-registry.hopp.ns.nl:8443/repository/gradle-plugins-group/")
mavenLocal()
gradlePluginPortal()
}
}
}
dependencyResolutionManagement {
repositories {
mavenLocal()
maven("https://dso-nexus-registry.hopp.ns.nl:8443/repository/public/")
maven("https://dso-nexus-registry.hopp.ns.nl:8443/repository/nl.donna.api.spec.snapshots/")
}
}
""".trimIndent()
)

Expand All @@ -52,9 +61,11 @@ abstract class TestBase {
implementation("tech.mappie:mappie-api:$version")
testImplementation(kotlin("test"))
}
repositories {
mavenLocal()
maven("https://dso-nexus-registry.hopp.ns.nl:8443/repository/public/")
maven("https://dso-nexus-registry.hopp.ns.nl:8443/repository/nl.donna.api.spec.snapshots/")
mavenCentral()
}
Expand Down

0 comments on commit b3c1489

Please sign in to comment.