From 57b57d763a1482df1956ef3bc5aa862da03e0dee Mon Sep 17 00:00:00 2001 From: Stefan Koppier Date: Mon, 5 Aug 2024 10:39:15 +0200 Subject: [PATCH] Fixed nested mappers bug (#78) --- .../tech/mappie/resolving/MappingResolver.kt | 10 ++-- ...apperClassCanContainAllDeclarationsTest.kt | 1 - .../MapperClassInsideOtherDeclarationTest.kt | 50 +++++++++++++++++++ website/src/changelog.md | 1 + 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperClassInsideOtherDeclarationTest.kt diff --git a/compiler-plugin/src/main/kotlin/tech/mappie/resolving/MappingResolver.kt b/compiler-plugin/src/main/kotlin/tech/mappie/resolving/MappingResolver.kt index 70d1cab0..7902597c 100644 --- a/compiler-plugin/src/main/kotlin/tech/mappie/resolving/MappingResolver.kt +++ b/compiler-plugin/src/main/kotlin/tech/mappie/resolving/MappingResolver.kt @@ -50,13 +50,17 @@ class MappingResolver : BaseVisitor>, MappieDefini } override fun visitClass(declaration: IrClass, data: MappieDefinitions): Map> { + val nested = declaration.declarations + .filterIsInstance().map { it.accept(data) } + .fold(mapOf(), Map>::plus) + return if (declaration.accept(ShouldTransformCollector(declaration.fileEntry), Unit)) { declaration.declarations - .filter { it is IrClass || it is IrFunction } + .filterIsInstance() .map { it.accept(data) } - .fold(mapOf(), Map>::plus) + .fold(nested, Map>::plus) } else { - emptyMap() + nested } } diff --git a/compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperClassCanContainAllDeclarationsTest.kt b/compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperClassCanContainAllDeclarationsTest.kt index 4c999cf3..917a4e51 100644 --- a/compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperClassCanContainAllDeclarationsTest.kt +++ b/compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperClassCanContainAllDeclarationsTest.kt @@ -67,5 +67,4 @@ class MapperClassCanContainAllDeclarationsTest { .isEqualTo(Output("test")) } } - } \ No newline at end of file diff --git a/compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperClassInsideOtherDeclarationTest.kt b/compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperClassInsideOtherDeclarationTest.kt new file mode 100644 index 00000000..9060399a --- /dev/null +++ b/compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperClassInsideOtherDeclarationTest.kt @@ -0,0 +1,50 @@ +package tech.mappie.testing + +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.KotlinCompilation +import tech.mappie.testing.compilation.KotlinCompilation.ExitCode +import tech.mappie.testing.compilation.SourceFile.Companion.kotlin +import java.io.File + +class MapperClassInsideOtherDeclarationTest { + + data class Input(val text: String) + data class Output(val text: String) + + @TempDir + lateinit var directory: File + + @Test + fun `mapper can be declared nested`() { + KotlinCompilation(directory).apply { + sources = buildList { + add( + kotlin("Test.kt", + """ + import tech.mappie.api.ObjectMappie + import tech.mappie.testing.MapperClassInsideOtherDeclarationTest.* + + class Thing { + class Mapper : ObjectMappie() + } + """ + ) + ) + } + }.compile { + assertThat(exitCode).isEqualTo(ExitCode.OK) + assertThat(messages).isEmpty() + + val mapper = classLoader + .loadObjectMappieClass("Thing\$Mapper") + .constructors + .first() + .call() + + assertThat(mapper.map(Input("test"))) + .isEqualTo(Output("test")) + } + } +} \ No newline at end of file diff --git a/website/src/changelog.md b/website/src/changelog.md index e3cad00e..8b5aea46 100644 --- a/website/src/changelog.md +++ b/website/src/changelog.md @@ -6,6 +6,7 @@ changelog: title: "v0.7.0" items: - "Added more built-in mappers." + - "Fixed a bug where mappers nested inside classes which are not a mapper are not generated." - date: "2024-08-31" title: "v0.6.0" items: