Skip to content

Commit

Permalink
Added typing for automatic integer casting
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier committed Jun 26, 2024
1 parent ad745a3 commit 2dd72cc
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
11 changes: 10 additions & 1 deletion compiler-plugin/src/main/kotlin/tech/mappie/util/Ir.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ internal fun IrClass.allSuperTypes(): List<IrType> =
this.superTypes + this.superTypes.flatMap { it.erasedUpperBound.allSuperTypes() }

fun IrType.isAssignableFrom(other: IrType): Boolean =
isSubtypeOf(other, IrTypeSystemContextImpl(context.irBuiltIns)) && (isNullable() || !other.isNullable())
other.isSubtypeOf(this, IrTypeSystemContextImpl(context.irBuiltIns)) || isIntegerAssignableFrom(other)

fun IrType.isIntegerAssignableFrom(other: IrType): Boolean =
when (other) {
context.irBuiltIns.byteType -> this in listOf(context.irBuiltIns.byteType)
context.irBuiltIns.shortType -> this in listOf(context.irBuiltIns.byteType, context.irBuiltIns.shortType)
context.irBuiltIns.intType -> this in listOf(context.irBuiltIns.byteType, context.irBuiltIns.shortType, context.irBuiltIns.intType)
context.irBuiltIns.longType -> this in listOf(context.irBuiltIns.byteType, context.irBuiltIns.shortType, context.irBuiltIns.intType, context.irBuiltIns.longType)
else -> false
}

fun getterName(name: Name) =
getterName(name.asString())
Expand Down
2 changes: 0 additions & 2 deletions mappie-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.jetbrains.dokka.gradle.DokkaTask

plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.dokka)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package testing

import tech.mappie.api.Mappie

object IdentityMapper : Mappie<Int, Int>() {
object IntIdentityMapper : Mappie<Int, Int>() {
override fun map(from: Int): Int = mapping()
}
9 changes: 9 additions & 0 deletions testing/src/main/kotlin/testing/IntToLongMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testing

import tech.mappie.api.ObjectMappie

data class LongInput(val value: Long)

data class IntOutput(val value: Int)

object IntToLongMapper : ObjectMappie<LongInput, IntOutput>()
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ object NullableListFromListMapper : ObjectMappie<SourceList, TargetList>() {

object StringIdentityMapper : ObjectMappie<String, String>() {
override fun map(from: String): String = from
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package testing
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class IdentityMapperTest {
class IntIdentityMapperTest {

@Test
fun `map Int to Int via IdentityMapper`() {
assertEquals(1, IdentityMapper.map(1))
assertEquals(1, IntIdentityMapper.map(1))
}
}
15 changes: 15 additions & 0 deletions testing/src/test/kotlin/testing/IntToLongMapperTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testing

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class IntToLongMapperTest {

@Test
fun `map Int to Long via IntToLongMapper`() {
assertEquals(
IntOutput(1),
IntToLongMapper.map(LongInput(1)),
)
}
}

0 comments on commit 2dd72cc

Please sign in to comment.