Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier committed Sep 21, 2024
1 parent 391c727 commit db935d3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import tech.mappie.generation.classes.ObjectMappieCodeGenerator
import tech.mappie.generation.enums.EnumMappieCodeGenerator
import tech.mappie.resolving.MappingResolver
import tech.mappie.resolving.ResolverContext
import tech.mappie.resolving.classes.sources.GeneratedViaMapperTransformation
import tech.mappie.resolving.classes.sources.ImplicitPropertyMappingSource
import tech.mappie.util.isMappieMapFunction
import tech.mappie.util.mappieType

Expand All @@ -18,8 +16,8 @@ class MappieCodeGenerator(private val context: CodeGenerationContext) : IrElemen
// TODO: Use MappingSelector instead?
val context = if (context.model is ClassMappieCodeGenerationModel) {
val models = context.model.mappings.values
.filter { source -> source is ImplicitPropertyMappingSource && source.transformation is GeneratedViaMapperTransformation }
.map { source -> (source as ImplicitPropertyMappingSource).transformation as GeneratedViaMapperTransformation }
.filter { source -> source.hasGeneratedTransformationMapping() }
.map { source -> source.selectGeneratedTransformationMapping() }
.distinctBy { it.source.type to it.target.type }
.map { transformation ->
MappingResolver.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.ir.expressions.IrPropertyReference
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.name.Name
import tech.mappie.MappieIrRegistrar.Companion.context
import tech.mappie.exceptions.MappiePanicException
import tech.mappie.resolving.MappieDefinition
import tech.mappie.resolving.classes.targets.ClassMappingTarget
import tech.mappie.util.isList
Expand All @@ -17,6 +18,17 @@ import tech.mappie.util.mappieType

sealed interface ClassMappingSource {
val type: IrType

fun hasGeneratedTransformationMapping() =
(this is ImplicitPropertyMappingSource && transformation is GeneratedViaMapperTransformation) ||
(this is ExplicitPropertyMappingSource && transformation is GeneratedViaMapperTransformation)

fun selectGeneratedTransformationMapping() =
when (this) {
is ExplicitPropertyMappingSource -> transformation as GeneratedViaMapperTransformation
is ImplicitPropertyMappingSource -> transformation as GeneratedViaMapperTransformation
else -> throw MappiePanicException("source $this should not occur in selectGeneratedTransformationMapping.")
}
}

sealed interface ImplicitClassMappingSource : ClassMappingSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,10 @@ class MapperGenerationRequestProblems(
val mappings = mapping.mappings
.filter { (_, sources) -> sources.size == 1 }
.mapValues { (_, sources) -> sources.single() }
.filter { (_, source) -> hasGeneratedTransformationMapping(source) }
.map { (target, source) -> target to selectGeneratedTransformationMapping(source) }
.filter { (_, source) -> source.hasGeneratedTransformationMapping() }
.map { (target, source) -> target to source.selectGeneratedTransformationMapping() }

return MapperGenerationRequestProblems(context, mappings)
}

private fun hasGeneratedTransformationMapping(source: ClassMappingSource) =
(source is ImplicitPropertyMappingSource && source.transformation is GeneratedViaMapperTransformation) ||
(source is ExplicitPropertyMappingSource && source.transformation is GeneratedViaMapperTransformation)

private fun selectGeneratedTransformationMapping(source: ClassMappingSource) =
when (source) {
is ExplicitPropertyMappingSource -> source.transformation as GeneratedViaMapperTransformation
is ImplicitPropertyMappingSource -> source.transformation as GeneratedViaMapperTransformation
else -> throw MappiePanicException("source $source should not occur in selectGeneratedTransformationMapping.")
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ class GeneratedClassListObjectTest {
assertThat(messages).isEmpty()


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

assertThat(mapper.map(Input(InnerInput(listOf(InnerInnerInput("first"), InnerInnerInput("second"))))))
.isEqualTo(Output(InnerOutput(listOf(InnerInnerOutput("first"), InnerInnerOutput("second")))))
}
}

@Test
fun `map data classes with nested list mapping should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.objects.GeneratedClassListObjectTest.*
class Mapper : ObjectMappie<Input, Output>() {
override fun map(from: Input) = mapping {
to::a fromProperty from::a
}
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()


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

0 comments on commit db935d3

Please sign in to comment.