Skip to content

Commit f133894

Browse files
Fixed check of runtime usage of of mapping DSL for multiple object mappie (#257)
1 parent d3cd3af commit f133894

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

compiler-plugin/src/main/kotlin/tech/mappie/fir/analysis/CompileTimeDslReceiverChecker.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ import org.jetbrains.kotlin.fir.types.resolvedType
1111
import tech.mappie.fir.analysis.MappieErrors.COMPILE_TIME_EXTENSION_RECEIVER
1212
import tech.mappie.fir.analysis.MappieErrors.COMPILE_TIME_RECEIVER
1313
import tech.mappie.util.ALL_MAPPING_FUNCTIONS
14+
import tech.mappie.util.CLASS_ID_MULTIPLE_OBJECT_MAPPING_CONSTRUCTOR
1415
import tech.mappie.util.CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR
1516

1617
class CompileTimeDslReceiverChecker : FirFunctionCallChecker(MppCheckerKind.Common) {
1718

19+
private val targets = listOf(CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR, CLASS_ID_MULTIPLE_OBJECT_MAPPING_CONSTRUCTOR)
20+
1821
context(context: CheckerContext, reporter: DiagnosticReporter)
1922
override fun check(expression: FirFunctionCall) {
2023
val name = expression.calleeReference.name
2124
if (name in ALL_MAPPING_FUNCTIONS) {
2225
return
2326
}
2427

25-
if (expression.dispatchReceiver?.resolvedType?.classId == CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR) {
28+
if (expression.dispatchReceiver?.resolvedType?.classId in targets) {
2629
reporter.reportOn(expression.source, COMPILE_TIME_RECEIVER, name)
2730
}
2831

29-
if (expression.extensionReceiver?.resolvedType?.classId == CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR) {
32+
if (expression.extensionReceiver?.resolvedType?.classId in targets) {
3033
reporter.reportOn(expression.source, COMPILE_TIME_EXTENSION_RECEIVER, name)
3134
}
3235
}

compiler-plugin/src/main/kotlin/tech/mappie/fir/analysis/ToCallChecker.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import tech.mappie.fir.analysis.MappieErrors.UNKNOWN_NAME_ERROR
2222
import tech.mappie.fir.util.toConstant
2323
import tech.mappie.fir.util.hasCallableId
2424
import tech.mappie.fir.util.isJavaGetter
25+
import tech.mappie.util.CLASS_ID_MULTIPLE_OBJECT_MAPPING_CONSTRUCTOR
2526
import tech.mappie.util.CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR
2627
import tech.mappie.util.IDENTIFIER_TO
2728

@@ -30,7 +31,7 @@ class ToCallChecker : FirFunctionCallChecker(MppCheckerKind.Common) {
3031
@OptIn(SymbolInternals::class)
3132
context(context: CheckerContext, reporter: DiagnosticReporter)
3233
override fun check(expression: FirFunctionCall) {
33-
if (expression.hasCallableId(CallableId(CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR, IDENTIFIER_TO))) {
34+
if (expression.isToCall()) {
3435
val name = expression.arguments.first().toConstant()?.value as? String?
3536

3637
if (name == null) {
@@ -63,6 +64,10 @@ class ToCallChecker : FirFunctionCallChecker(MppCheckerKind.Common) {
6364
}
6465
}
6566

67+
private fun FirFunctionCall.isToCall() =
68+
hasCallableId(CallableId(CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR, IDENTIFIER_TO))
69+
|| hasCallableId(CallableId(CLASS_ID_MULTIPLE_OBJECT_MAPPING_CONSTRUCTOR, IDENTIFIER_TO))
70+
6671
context (context: CheckerContext)
6772
private fun FirFunctionCall.getTargetRegularClassSymbol() =
6873
dispatchReceiver?.resolvedType?.typeArguments?.last()?.type?.toRegularClassSymbol(context.session)

compiler-plugin/src/main/kotlin/tech/mappie/util/Identifiers.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ val PACKAGE_TECH_MAPPIE_API_CONFIG = FqName("tech.mappie.api.config")
5858

5959
val CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR = ClassId(PACKAGE_TECH_MAPPIE_API, Name.identifier("ObjectMappingConstructor"))
6060

61+
val CLASS_ID_MULTIPLE_OBJECT_MAPPING_CONSTRUCTOR = ClassId(PACKAGE_TECH_MAPPIE_API, Name.identifier("MultipleObjectMappingConstructor"))
62+
6163
val CLASS_ID_ENUM_MAPPING_CONSTRUCTOR = ClassId(PACKAGE_TECH_MAPPIE_API, Name.identifier("EnumMappingConstructor"))
6264

6365
val CLASS_ID_MAPPIE = ClassId(PACKAGE_TECH_MAPPIE_API, Name.identifier("Mappie"))

compiler-plugin/src/test/kotlin/tech/mappie/testing/objects/FromValueTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,31 @@ class FromValueTest {
9797
}
9898
}
9999

100+
@Test
101+
fun `map property fromValue using extension receiver on mapping dsl in ObjectMappie2 should fail`() {
102+
compile(directory) {
103+
file("Test.kt",
104+
"""
105+
import tech.mappie.api.ObjectMappie2
106+
import tech.mappie.testing.objects.FromValueTest.*
107+
108+
class Mapper : ObjectMappie2<Unit, Unit, Output>() {
109+
override fun map(first: Unit, second: Unit) = mapping {
110+
to::value fromValue run {
111+
"test"
112+
}
113+
}
114+
}
115+
"""
116+
)
117+
} satisfies {
118+
isCompilationError()
119+
hasErrorMessage(6,
120+
"The function 'run' was called as an extension method on the mapping dsl which does not exist after compilation",
121+
)
122+
}
123+
}
124+
100125
@Test
101126
fun `map property fromValue using dispatch receiver on mapping dsl should fail`() {
102127
compile(directory) {

website/src/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
title: "Changelog"
33
layout: "layouts/changelog.html"
44
changelog:
5+
- date: "tbd"
6+
title: "v2.2.20-1.6.0"
7+
items:
8+
- "Fixed check of runtime usage of of mapping DSL for multiple object mappie."
59
- date: "2025-09-10"
610
title: "v2.2.20-1.5.0"
711
items:

0 commit comments

Comments
 (0)