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

Kotlin 2.1.0 migration #136

Merged
merged 6 commits into from
Nov 29, 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ build/

#Kotlin
.kotlin
kotlin-js-store

### IntelliJ IDEA ###
.idea
Expand Down
10 changes: 1 addition & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.jreleaser.model.Signing
import java.io.ByteArrayOutputStream

plugins {
alias(libs.plugins.kotlin.jvm)
Expand All @@ -19,20 +18,13 @@ dependencies {
jacocoAggregation(project(":compiler-plugin"))
}

val branch = ByteArrayOutputStream()

exec {
commandLine("git", "rev-parse", "--abbrev-ref", "HEAD")
standardOutput = branch
}

sonar {
properties {
property("sonar.projectKey", "Mr-Mappie_mappie")
property("sonar.organization", "mappie")
property("sonar.host.url", "https://sonarcloud.io")
property("sonar.qualitygate.wait", "true")
property("sonar.branch.name", branch.toString(Charsets.UTF_8))
property("sonar.branch.name", "main")
property("sonar.coverage.jacoco.xmlReportPaths", layout.buildDirectory
.file("reports/jacoco/testCodeCoverageReport/testCodeCoverageReport.xml")
.get().asFile.absolutePath
Expand Down
3 changes: 3 additions & 0 deletions compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ publishing {

tasks.withType<Test> {
useJUnitPlatform()

finalizedBy(tasks.jacocoTestReport)

maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
}

tasks.withType<KotlinCompile>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package tech.mappie
import tech.mappie.MappieCommandLineProcessor.Companion.ARGUMENT_STRICTNESS_ENUMS
import tech.mappie.MappieCommandLineProcessor.Companion.ARGUMENT_STRICTNESS_VISIBILITY
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import tech.mappie.MappieCommandLineProcessor.Companion.ARGUMENT_USE_DEFAULT_ARGUMENTS
import tech.mappie.MappieCommandLineProcessor.Companion.ARGUMENT_WARNINGS_AS_ERRORS
Expand All @@ -20,7 +20,7 @@ class MappieCompilerPluginRegistrar : CompilerPluginRegistrar() {

override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {

val messageCollector = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
val messageCollector = configuration.get(CommonConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
IrGenerationExtension.registerExtension(
MappieIrRegistrar(
messageCollector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ClassMappieCodeGenerationModelFactory(private val request: ClassMappingReq
private fun select(target: ClassMappingTarget, sources: List<ClassMappingSource>): ClassMappingSource? =
if (sources.isEmpty()) {
null
} else {
} else {
sources.singleOrNull() ?: when (target) {
is FunctionCallTarget -> sources.first()
is SetterTarget -> sources.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private class TargetNameCollector(private val context: ResolverContext) : BaseVi
return when (expression.symbol.owner.name) {
IDENTIFIER_TO -> {
val value = expression.valueArguments.first()!!
return if (value.isConstantLike && value is IrConst<*>) {
return if (value.isConstantLike && value is IrConst) {
Name.identifier(value.value as String)
} else {
val problem = Problem.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ sealed interface PropertyMappingTransformation {
val type: IrType
}

@ConsistentCopyVisibility
data class PropertyMappingTransformTranformation private constructor(
val function: IrExpression,
override val type: IrType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tech.mappie.util

fun <T> Collection<List<T>>.filterSingle(): List<T> =
filter { it.size == 1 }.map { it.single() }
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package tech.mappie.util

fun <K, V> List<Map<K, V>>.merge(): Map<K, V> = fold(emptyMap(), Map<K, V>::plus)

fun <K, V> Sequence<Map<K, V>>.merge(): Map<K, V> = fold(emptyMap(), Map<K, V>::plus)
fun <K, V> Sequence<Map<K, V>>.merge(): Map<K, V> = fold(emptyMap(), Map<K, V>::plus)

fun <K, V> Map<K, List<V>>.filterSingle(): Map<K, V> =
filter { it.value.size == 1 }.mapValues { (_, v) -> v.single() }
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tech.mappie.resolving.classes.sources.ValueMappingSource
import tech.mappie.util.CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR
import tech.mappie.util.location
import org.jetbrains.kotlin.name.Name
import tech.mappie.util.filterSingle
import tech.mappie.validation.Problem
import tech.mappie.validation.ValidationContext

Expand Down Expand Up @@ -36,8 +37,7 @@ class CompileTimeReceiverDslProblems private constructor(
companion object {
fun of(context: ValidationContext, mapping: ClassMappingRequest): CompileTimeReceiverDslProblems {
val mappings = mapping.mappings.values
.filter { it.size == 1 }
.map { it.single() }
.filterSingle()
.filterIsInstance<ValueMappingSource>()
.map { it.expression }
.filterIsInstance<IrCall>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tech.mappie.resolving.ClassMappingRequest
import tech.mappie.resolving.MappingResolver
import tech.mappie.resolving.ResolverContext
import tech.mappie.resolving.classes.sources.*
import tech.mappie.util.filterSingle
import tech.mappie.util.location
import tech.mappie.util.mappieType
import tech.mappie.validation.MappingValidation
Expand Down Expand Up @@ -55,10 +56,8 @@ class MapperGenerationRequestProblems(

companion object {
fun of(context: ValidationContext, mapping: ClassMappingRequest): MapperGenerationRequestProblems {
val mappings = mapping.mappings
.map { it.value }
.filter { it.size == 1 }
.map { it.single() }
val mappings = mapping.mappings.values
.filterSingle()
.filter { it.hasGeneratedTransformationMapping() }
.map { it.selectGeneratedTransformationMapping() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import tech.mappie.validation.Problem

class MultipleSourcesProblems(
private val targetType: IrType,
private val mappings: List<Pair<ClassMappingTarget, List<ClassMappingSource>>>
private val mappings: Map<ClassMappingTarget, List<ClassMappingSource>>
) {

fun all(): List<Problem> = mappings.map { (target, sources) ->
Expand All @@ -22,7 +22,7 @@ class MultipleSourcesProblems(
mapping.target,
mapping.mappings
.filter { (target, _) -> target.required }
.filter { (_, sources) -> sources.size != 1 }.map { it.toPair() }
.filter { (_, sources) -> sources.size != 1 }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.kotlin.ir.util.dumpKotlinLike
import org.jetbrains.kotlin.ir.util.fileEntry
import tech.mappie.resolving.ClassMappingRequest
import tech.mappie.resolving.classes.sources.ExplicitPropertyMappingSource
import tech.mappie.util.filterSingle
import tech.mappie.util.location
import tech.mappie.validation.Problem
import tech.mappie.validation.ValidationContext
Expand All @@ -26,8 +27,7 @@ class UnnecessaryFromPropertyNotNullProblems(
UnnecessaryFromPropertyNotNullProblems(
context,
mapping.mappings.values
.filter { it.size == 1 }
.map { it.single() }
.filterSingle()
.filterIsInstance<ExplicitPropertyMappingSource>()
.filter { it.forceNonNull && !it.getterType.isNullable() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.ir.util.fileEntry
import tech.mappie.resolving.ClassMappingRequest
import tech.mappie.resolving.classes.sources.*
import tech.mappie.resolving.classes.targets.ClassMappingTarget
import tech.mappie.util.filterSingle
import tech.mappie.util.hasFlexibleNullabilityAnnotation
import tech.mappie.util.location
import tech.mappie.validation.Problem
Expand All @@ -15,10 +16,10 @@ import tech.mappie.validation.ValidationContext
class UnsafePlatformTypeAssignmentProblems(
private val context: ValidationContext,
private val mapping: ClassMappingRequest,
private val mappings: List<Pair<ClassMappingTarget, ClassMappingSource>>,
private val mappings: Map<ClassMappingTarget, ClassMappingSource>,
) {

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

private fun validate(target: ClassMappingTarget, source: ClassMappingSource): Problem? {
val sourceTypeString = source.type.removeAnnotations().dumpKotlinLike()
Expand Down Expand Up @@ -57,12 +58,10 @@ class UnsafePlatformTypeAssignmentProblems(
companion object {
fun of(context: ValidationContext, mapping: ClassMappingRequest): UnsafePlatformTypeAssignmentProblems {
val mappings = mapping.mappings
.filter { (_, sources) -> sources.size == 1 }
.filter { (target, sources) ->
val source = sources.single()
.filterSingle()
.filter { (target, source) ->
source.type.hasFlexibleNullabilityAnnotation() && !target.type.isNullable()
}
.map { (target, sources) -> target to sources.single() }

return UnsafePlatformTypeAssignmentProblems(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.jetbrains.kotlin.ir.util.fileEntry
import tech.mappie.resolving.ClassMappingRequest
import tech.mappie.resolving.classes.sources.*
import tech.mappie.resolving.classes.targets.ClassMappingTarget
import tech.mappie.util.filterSingle
import tech.mappie.util.hasFlexibleNullabilityAnnotation
import tech.mappie.util.location
import tech.mappie.validation.Problem
Expand All @@ -14,10 +15,10 @@ import tech.mappie.validation.ValidationContext
class UnsafeTypeAssignmentProblems(
private val context: ValidationContext,
private val mapping: ClassMappingRequest,
private val mappings: List<Pair<ClassMappingTarget, ClassMappingSource>>,
private val mappings: Map<ClassMappingTarget, ClassMappingSource>,
) {

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

private fun validate(target: ClassMappingTarget, source: ClassMappingSource): Problem? {
val targetTypeString = target.type.dumpKotlinLike()
Expand Down Expand Up @@ -58,13 +59,11 @@ class UnsafeTypeAssignmentProblems(
companion object {
fun of(context: ValidationContext, mapping: ClassMappingRequest): UnsafeTypeAssignmentProblems {
val mappings = mapping.mappings
.filter { (_, sources) -> sources.size == 1 }
.filter { (target, sources) ->
val source = sources.single()
.filterSingle()
.filter { (target, source) ->
!source.type.isSubtypeOf(target.type, IrTypeSystemContextImpl(context.pluginContext.irBuiltIns)) ||
((source.type.isNullable() && !source.type.hasFlexibleNullabilityAnnotation()) && !target.type.isNullable())
}
.map { (target, sources) -> target to sources.single() }

return UnsafeTypeAssignmentProblems(
context,
Expand Down
2 changes: 2 additions & 0 deletions gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ tasks.test {
dependsOn(project(":compiler-plugin").tasks.publishToMavenLocal)
dependsOn(project(":mappie-api").tasks.named("publishKotlinMultiplatformPublicationToMavenLocal"))
dependsOn(project(":mappie-api").tasks.named("publishJvmPublicationToMavenLocal"))

maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
}
8 changes: 6 additions & 2 deletions gradle-plugin/src/test/kotlin/tech/mappie/testing/TestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ abstract class TestBase {

protected lateinit var runner: GradleRunner

protected open val kotlinVersion = "2.0.21"
protected open val gradleVersion: String? = null

protected open val kotlinVersion = "2.1.0"

@BeforeEach
fun setup() {
runner = GradleRunner.create().apply {
forwardOutput()
withProjectDir(directory)
gradleVersion?.let { withGradleVersion(gradleVersion) }
}

directory.resolve("src/main/kotlin").mkdirs()
directory.resolve("src/main/java").mkdirs()
directory.resolve("src/test/kotlin").mkdirs()

println("Using kotlin version $kotlinVersion")
gradleVersion?.let { println("Using Gradle version $it") }
println("Using Kotlin version $kotlinVersion")

kotlin("settings.gradle.kts",
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.mappie.testing.compatibility.gradle

import org.junit.jupiter.api.Test

class Gradle810CompatibilityTest : GradleCompatibilityTestBase() {

override val gradleVersion = "8.10.2"

@Test
fun `test compatibility with gradle 8_10_2`() {
runner.withArguments("build").build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.mappie.testing.compatibility.gradle

import org.junit.jupiter.api.Test

class Gradle811CompatibilityTest : GradleCompatibilityTestBase() {

override val gradleVersion = "8.11.1"

@Test
fun `test compatibility with gradle 8_11_1`() {
runner.withArguments("build").build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.mappie.testing.compatibility.gradle

import org.junit.jupiter.api.Test

class Gradle88CompatibilityTest : GradleCompatibilityTestBase() {

override val gradleVersion = "8.8"

@Test
fun `test compatibility with gradle 8_8`() {
runner.withArguments("build").build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.mappie.testing.compatibility.gradle

import org.junit.jupiter.api.Test

class Gradle89CompatibilityTest : GradleCompatibilityTestBase() {

override val gradleVersion = "8.9"

@Test
fun `test compatibility with gradle 8_9`() {
runner.withArguments("build").build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tech.mappie.testing.compatibility.gradle

import org.junit.jupiter.api.BeforeEach
import tech.mappie.testing.TestBase

abstract class GradleCompatibilityTestBase : TestBase() {

@BeforeEach
fun files() {
kotlin(
"src/main/kotlin/Mapper.kt",
"""
import tech.mappie.api.ObjectMappie

data class Input(val value: String)
data class Output(val value: String)

object Mapper : ObjectMappie<Input, Output>()
""".trimIndent()
)

kotlin(
"src/test/kotlin/MapperTest.kt",
"""
import kotlin.test.*

class MapperTest {

@Test
fun map() {
assertEquals(Output("test"), Mapper.map(Input("test")))
}
}
""".trimIndent()
)
}
}
Loading