Skip to content

Commit 7fe7b56

Browse files
Fixes for generation of both nullable and non-nullable in the same mapper (#307)
1 parent 3dec195 commit 7fe7b56

File tree

7 files changed

+117
-11
lines changed

7 files changed

+117
-11
lines changed

compiler-plugin/src/main/kotlin/tech/mappie/ir/MappieDefinitions.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ interface MappieDefinition {
8787
val target: IrType
8888

8989
fun referenceMapFunction() = clazz.functions.first { it.isMappieMapFunction() }
90-
fun referenceMapNullableFunction() = clazz.functions.first { it.isMappieMapNullableFunction() }
9190
}
9291

9392
data class InternalMappieDefinition(

compiler-plugin/src/main/kotlin/tech/mappie/ir/generation/TransformationConstructor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ fun IrBuilderWithScope.constructTransformation(
6363
val definition = definitions.first().clazz
6464

6565
if (definition is IrMappieGeneratedClass) {
66-
context.logger.log(internal("failed to reference generated mapper ${definition.name}."))
66+
context.logger.log(internal("failed to reference generated mapper ${definition.name} in ${origin.clazz.name}."))
6767

6868
irCall(referenceFunctionError()).apply {
6969
arguments[0] = IrConstImpl.string(
7070
SYNTHETIC_OFFSET,
7171
SYNTHETIC_OFFSET,
7272
context.pluginContext.irBuiltIns.stringType,
73-
"Failed to reference generated mapper ${definition.name}"
73+
"Failed to reference generated mapper ${definition.name} in ${origin.clazz.name}"
7474
)
7575
}
7676
} else {
@@ -80,7 +80,7 @@ fun IrBuilderWithScope.constructTransformation(
8080
}
8181
}
8282
} else {
83-
context.logger.log(internal("failed to reference generated mapper."))
83+
context.logger.log(internal("failed to reference an unique generated mapper in ${origin.clazz.name}. Possible options: " + definitions.joinToString(prefix = "'", postfix = "'") { it.clazz.name.asString() }))
8484

8585
irCall(referenceFunctionError()).apply {
8686
arguments[0] = IrConstImpl.string(

compiler-plugin/src/main/kotlin/tech/mappie/ir/generation/classes/ClassMappieCodeGenerationModelFactory.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tech.mappie.ir.generation.classes
22

33
import org.jetbrains.kotlin.ir.types.IrType
4+
import org.jetbrains.kotlin.ir.types.makeNullable
45
import org.jetbrains.kotlin.ir.types.typeOrFail
56
import org.jetbrains.kotlin.ir.util.dumpKotlinLike
67
import tech.mappie.ir.MappieContext
@@ -91,7 +92,7 @@ class ClassMappieCodeGenerationModelFactory {
9192
private fun generated(origin: InternalMappieDefinition, mappings: Map<ClassMappingTarget, ClassMappingSource>): Map<GeneratedMappieDefinition, CodeGenerationModel> =
9293
mappings.entries.fold(mutableMapOf<GeneratedMappieDefinition, CodeGenerationModel>()) { acc, (target, source) ->
9394
acc.apply {
94-
fun isUnique(source: IrType, target: IrType) = acc.entries.none { it.key.target == target && it.key.source == source }
95+
fun isUnique(source: IrType, target: IrType) = acc.entries.none { it.key.target.makeNullable() == target.makeNullable() && it.key.source.makeNullable() == source.makeNullable() }
9596

9697
val source = source as? TransformableClassMappingSource
9798
when (val transformation = source?.transformation) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tech.mappie.testing.lists
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
import tech.mappie.testing.MappieTestCase
6+
7+
class MapTwoListsOfDifferentTypeTest : MappieTestCase() {
8+
9+
data class Input(val first: List<FirstInput>, val second: List<SecondInput>)
10+
data class FirstInput(val value: String)
11+
data class SecondInput(val value: Int)
12+
13+
data class Output(val first: List<FirstOutput>, val second: List<SecondOutput>)
14+
data class FirstOutput(val value: String)
15+
data class SecondOutput(val value: Int)
16+
17+
@Test
18+
fun `map two lists of different type should succeed`() {
19+
compile {
20+
file("Test.kt",
21+
"""
22+
import tech.mappie.api.ObjectMappie
23+
import tech.mappie.testing.lists.MapTwoListsOfDifferentTypeTest.*
24+
25+
class Mapper : ObjectMappie<Input, Output>()
26+
"""
27+
)
28+
} satisfies {
29+
isOk()
30+
hasNoWarningsOrErrors()
31+
32+
val mapper = objectMappie<Input, Output>()
33+
34+
assertThat(mapper.map(Input(listOf(FirstInput("a")), listOf(SecondInput(1)))))
35+
.isEqualTo(Output(listOf(FirstOutput("a")), listOf(SecondOutput(1))))
36+
}
37+
}
38+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import org.junit.jupiter.api.Test
55
import tech.mappie.testing.MappieTestCase
66

77
class GeneratedClassNullToNullTest : MappieTestCase() {
8-
data class Input(val a: InnerInput?)
8+
data class Input(val a: InnerInput, val b: InnerInput?)
99
@Suppress("unused") enum class InnerInput { FIRST, SECOND }
10-
data class Output(val a: InnerOutput?)
10+
data class Output(val a: InnerOutput, val b: InnerOutput?)
1111
@Suppress("unused") enum class InnerOutput { FIRST, SECOND }
1212

1313
@Test
@@ -27,11 +27,11 @@ class GeneratedClassNullToNullTest : MappieTestCase() {
2727

2828
val mapper = objectMappie<Input, Output>()
2929

30-
assertThat(mapper.map(Input(InnerInput.FIRST)))
31-
.isEqualTo(Output(InnerOutput.FIRST))
30+
assertThat(mapper.map(Input(InnerInput.SECOND,InnerInput.FIRST)))
31+
.isEqualTo(Output(InnerOutput.SECOND, InnerOutput.FIRST))
3232

33-
assertThat(mapper.map(Input(null)))
34-
.isEqualTo(Output(null))
33+
assertThat(mapper.map(Input(InnerInput.FIRST, null)))
34+
.isEqualTo(Output(InnerOutput.FIRST, null))
3535
}
3636
}
3737
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tech.mappie.testing.objects
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
import tech.mappie.testing.MappieTestCase
6+
7+
class GeneratedDoubleNestedClassTest : MappieTestCase() {
8+
data class Input(val a: InnerInput, val b: InnerInput?)
9+
data class InnerInput(val value: InnerInput?)
10+
data class Output(val a: InnerOutput, val b: InnerOutput?)
11+
data class InnerOutput(val value: InnerOutput?)
12+
13+
@Test
14+
fun `map object with nested class without declaring mapping should succeed`() {
15+
compile {
16+
file("Test.kt",
17+
"""
18+
import tech.mappie.api.ObjectMappie
19+
import tech.mappie.testing.objects.GeneratedDoubleNestedClassTest.*
20+
21+
class Mapper : ObjectMappie<Input, Output>()
22+
"""
23+
)
24+
} satisfies {
25+
isOk()
26+
hasNoWarningsOrErrors()
27+
28+
val mapper = objectMappie<Input, Output>()
29+
30+
assertThat(mapper.map(Input(InnerInput(InnerInput(null)), null)))
31+
.isEqualTo(Output(InnerOutput(InnerOutput(null)), null))
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tech.mappie.testing.objects
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
import tech.mappie.testing.MappieTestCase
6+
7+
class GeneratedDoubleNullableClassTest : MappieTestCase() {
8+
data class Input(val a: InnerInput, val b: InnerInput?)
9+
data class InnerInput(val value: String)
10+
data class Output(val a: InnerOutput, val b: InnerOutput?)
11+
data class InnerOutput(val value: String)
12+
13+
@Test
14+
fun `map object with nested class without declaring mapping should succeed`() {
15+
compile {
16+
file("Test.kt",
17+
"""
18+
import tech.mappie.api.ObjectMappie
19+
import tech.mappie.testing.objects.GeneratedDoubleNullableClassTest.*
20+
21+
class Mapper : ObjectMappie<Input, Output>()
22+
"""
23+
)
24+
} satisfies {
25+
isOk()
26+
hasNoWarningsOrErrors()
27+
28+
val mapper = objectMappie<Input, Output>()
29+
30+
assertThat(mapper.map(Input(InnerInput("a"), InnerInput("b"))))
31+
.isEqualTo(Output(InnerOutput("a"), InnerOutput("b")))
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)