Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More default mappers #77

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,30 @@ class BuiltinMappieDefinitionsCollector {
"CharToStringMapper",
"LongToBigIntegerMapper",
"LongToBigDecimalMapper",
"LongToStringMapper",
"IntToLongMapper",
"IntToBigIntegerMapper",
"IntToBigDecimalMapper",
"IntToStringMapper",
"ShortToIntMapper",
"ShortToLongMapper",
"ShortToBigIntegerMapper",
"ShortToBigDecimalMapper",
"ShortToStringMapper",
"ByteToShortMapper",
"ByteToIntMapper",
"ByteToLongMapper",
"ByteToBigIntegerMapper",
"ByteToBigDecimalMapper",
"ByteToStringMapper",
"FloatToDoubleMapper",
"FloatToBigDecimalMapper",
"FloatToStringMapper",
"DoubleToBigDecimalMapper",
"DoubleToStringMapper",
"BigIntegerToStringMapper",
"BigDecimalToStringMapper",
"UUIDToStringMapper",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ data class PropertySource(
if (transformation.isEmpty()) {
getter.owner.returnType
} else {
val transformation = transformation.first()
when (transformation) {
when (val transformation = transformation.first()) {
is MappieTransformOperator -> (transformation.type as IrSimpleType).arguments[1].typeOrFail
is MappieViaOperator -> if (getter.owner.returnType.isNullable()) transformation.type.makeNullable() else transformation.type
is MappieViaResolved -> if (getter.owner.returnType.isNullable()) transformation.type.makeNullable() else transformation.type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import tech.mappie.validation.Problem
class MultipleTransformationsProblems(private val mappings: Map<MappieTarget, List<ObjectMappingSource>>) {
fun all(): List<Problem> =
mappings.map { (_, sources) ->
val source = sources.single()
when (source) {
when (val source = sources.single()) {
is ResolvedSource -> source.transformation
is PropertySource -> source.transformation
else -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UnsafePlatformTypeAssignmentProblems(
private val mappings: List<Pair<MappieTarget, ObjectMappingSource>>,
) {

fun all(): List<Problem> = mappings.map { validate(it.first, it.second) }.filterNotNull()
fun all(): List<Problem> = mappings.mapNotNull { validate(it.first, it.second) }

private fun validate(target: MappieTarget, source: ObjectMappingSource): Problem? {
val sourceTypeString = source.type.removeAnnotations().dumpKotlinLike()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UnsafeTypeAssignmentProblems(
private val mappings: List<Pair<MappieTarget, ObjectMappingSource>>,
) {

fun all(): List<Problem> = mappings.map { validate(it.first, it.second) }.filterNotNull()
fun all(): List<Problem> = mappings.mapNotNull { validate(it.first, it.second) }

private fun validate(target: MappieTarget, source: ObjectMappingSource): Problem? {
val sourceTypeString = source.type.dumpKotlinLike()
Expand All @@ -43,7 +43,7 @@ class UnsafeTypeAssignmentProblems(
Problem.error(description, null)
}
is ValueSource -> {
val location = source.origin?.let { location(file, it) }
val location = location(file, source.origin)
val description = "Target $targetString of type $targetTypeString cannot be assigned from value of type $sourceTypeString"
Problem.error(description, location)
}
Expand All @@ -53,7 +53,6 @@ class UnsafeTypeAssignmentProblems(
}
}


companion object {
fun of(file: IrFileEntry, mapping: ConstructorCallMapping): UnsafeTypeAssignmentProblems {
val mappings = mapping.mappings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MapperClassCanContainAllDeclarationsTest {
lateinit var directory: File

@Test
fun `"mapper containaining all kind of declarations should succeed`() {
fun `mapper containing all kind of declarations should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package tech.mappie.testing.builtin

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 tech.mappie.testing.loadObjectMappieClass
import java.io.File
import java.math.BigDecimal

class BigDecimalMappersTest {

@TempDir
lateinit var directory: File

data class BigDecimalInput(val value: BigDecimal)

data class StringOutput(val value: String)

@Test
fun `map BigDecimal to String implicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.builtin.BigDecimalMappersTest.*

class Mapper : ObjectMappie<BigDecimalInput, StringOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = BigDecimal.valueOf(10)

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

assertThat(mapper.map(BigDecimalInput(input)))
.isEqualTo(StringOutput(input.toString()))
}
}

@Test
fun `map BigDecimal to String explicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.api.builtin.*
import tech.mappie.testing.builtin.BigDecimalMappersTest.*

class Mapper : ObjectMappie<BigDecimalInput, StringOutput>() {
override fun map(from: BigDecimalInput) = mapping {
to::value fromProperty from::value via BigDecimalToStringMapper()
}
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = BigDecimal.valueOf(100)

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

assertThat(mapper.map(BigDecimalInput(input)))
.isEqualTo(StringOutput(input.toString()))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package tech.mappie.testing.builtin

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 tech.mappie.testing.loadObjectMappieClass
import java.io.File
import java.math.BigInteger

class BigIntegerMappersTest {

@TempDir
lateinit var directory: File

data class BigIntegerInput(val value: BigInteger)

data class StringOutput(val value: String)

@Test
fun `map BigInteger to String implicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.builtin.BigIntegerMappersTest.*

class Mapper : ObjectMappie<BigIntegerInput, StringOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = BigInteger.valueOf(10)

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

assertThat(mapper.map(BigIntegerInput(input)))
.isEqualTo(StringOutput(input.toString()))
}
}

@Test
fun `map BigInteger to String explicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.api.builtin.*
import tech.mappie.testing.builtin.BigIntegerMappersTest.*

class Mapper : ObjectMappie<BigIntegerInput, StringOutput>() {
override fun map(from: BigIntegerInput) = mapping {
to::value fromProperty from::value via BigIntegerToStringMapper()
}
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = BigInteger.valueOf(100)

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

assertThat(mapper.map(BigIntegerInput(input)))
.isEqualTo(StringOutput(input.toString()))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ByteMappersTest {

data class BigDecimalOutput(val value: BigDecimal)

data class StringOutput(val value: String)

@Test
fun `map Byte to Short implicit should succeed`() {
KotlinCompilation(directory).apply {
Expand Down Expand Up @@ -372,4 +374,73 @@ class ByteMappersTest {
.isEqualTo(BigDecimalOutput(BigDecimal.valueOf(input.toLong())))
}
}

@Test
fun `map Byte to String implicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.builtin.ByteMappersTest.*

class Mapper : ObjectMappie<ByteInput, StringOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input: Byte = 2

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

assertThat(mapper.map(ByteInput(input)))
.isEqualTo(StringOutput(input.toString()))
}
}

@Test
fun `map Byte to String explicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.api.builtin.*
import tech.mappie.testing.builtin.ByteMappersTest.*

class Mapper : ObjectMappie<ByteInput, StringOutput>() {
override fun map(from: ByteInput) = mapping {
to::value fromProperty from::value via ByteToStringMapper()
}
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input: Byte = 5

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

assertThat(mapper.map(ByteInput(input)))
.isEqualTo(StringOutput(input.toString()))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CharMappersTest {
}

@Test
fun `map Long to BigInteger explicit should succeed`() {
fun `map Char to String explicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
Expand Down
Loading