From 99e0c6a227fca0ba89762e0e803c5feb736ba1c6 Mon Sep 17 00:00:00 2001 From: Stefan Koppier Date: Tue, 2 Jul 2024 12:59:12 +0200 Subject: [PATCH] Added configuration option to disable default value argument sources (#43) --- .../tech/mappie/MappieCommandLineProcessor.kt | 9 +++++++++ .../tech/mappie/MappieCompilerPluginRegistrar.kt | 2 ++ .../main/kotlin/tech/mappie/MappieConfiguration.kt | 1 + .../resolving/classes/ObjectMappingsConstructor.kt | 3 ++- .../src/main/kotlin/tech/mappie/MappieExtension.kt | 5 +++++ .../main/kotlin/tech/mappie/MappieGradlePlugin.kt | 3 +++ website/src/changelog.md | 1 + .../posts/getting-started/posts/configuration.md | 14 ++++++++------ 8 files changed, 31 insertions(+), 7 deletions(-) diff --git a/compiler-plugin/src/main/kotlin/tech/mappie/MappieCommandLineProcessor.kt b/compiler-plugin/src/main/kotlin/tech/mappie/MappieCommandLineProcessor.kt index bb2e5e64..838f3996 100644 --- a/compiler-plugin/src/main/kotlin/tech/mappie/MappieCommandLineProcessor.kt +++ b/compiler-plugin/src/main/kotlin/tech/mappie/MappieCommandLineProcessor.kt @@ -13,6 +13,12 @@ class MappieCommandLineProcessor : CommandLineProcessor { override val pluginId = "mappie" override val pluginOptions = listOf( + CliOption( + optionName = OPTION_USE_DEFAULT_ARGUMENTS, + valueDescription = "boolean", + description = "allow automatic resolving via default argument values", + required = false, + ), CliOption( optionName = OPTION_WARNINGS_AS_ERRORS, valueDescription = "boolean", @@ -36,6 +42,7 @@ class MappieCommandLineProcessor : CommandLineProcessor { override fun processOption(option: AbstractCliOption, value: String, configuration: CompilerConfiguration) { return when (option.optionName) { OPTION_WARNINGS_AS_ERRORS -> configuration.put(ARGUMENT_WARNINGS_AS_ERRORS, value.toBooleanStrict()) + OPTION_USE_DEFAULT_ARGUMENTS -> configuration.put(ARGUMENT_USE_DEFAULT_ARGUMENTS, value.toBooleanStrict()) OPTION_STRICTNESS_ENUMS -> configuration.put(ARGUMENT_STRICTNESS_ENUMS, value.toBooleanStrict()) OPTION_STRICTNESS_VISIBILITY -> configuration.put(ARGUMENT_STRICTNESS_VISIBILITY, value.toBooleanStrict()) else -> throw IllegalArgumentException("Unknown option ${option.optionName}") @@ -44,10 +51,12 @@ class MappieCommandLineProcessor : CommandLineProcessor { companion object { const val OPTION_WARNINGS_AS_ERRORS = "warningsAsErrors" + const val OPTION_USE_DEFAULT_ARGUMENTS = "useDefaultArguments" const val OPTION_STRICTNESS_ENUMS = "strictness.enums" const val OPTION_STRICTNESS_VISIBILITY = "strictness.visibility" val ARGUMENT_WARNINGS_AS_ERRORS = CompilerConfigurationKey(OPTION_WARNINGS_AS_ERRORS) + val ARGUMENT_USE_DEFAULT_ARGUMENTS = CompilerConfigurationKey(OPTION_USE_DEFAULT_ARGUMENTS) val ARGUMENT_STRICTNESS_ENUMS = CompilerConfigurationKey(OPTION_STRICTNESS_ENUMS) val ARGUMENT_STRICTNESS_VISIBILITY = CompilerConfigurationKey(OPTION_STRICTNESS_VISIBILITY) } diff --git a/compiler-plugin/src/main/kotlin/tech/mappie/MappieCompilerPluginRegistrar.kt b/compiler-plugin/src/main/kotlin/tech/mappie/MappieCompilerPluginRegistrar.kt index e2a46854..9e3b9ae7 100644 --- a/compiler-plugin/src/main/kotlin/tech/mappie/MappieCompilerPluginRegistrar.kt +++ b/compiler-plugin/src/main/kotlin/tech/mappie/MappieCompilerPluginRegistrar.kt @@ -8,6 +8,7 @@ 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.CompilerConfiguration +import tech.mappie.MappieCommandLineProcessor.Companion.ARGUMENT_USE_DEFAULT_ARGUMENTS import tech.mappie.MappieCommandLineProcessor.Companion.ARGUMENT_WARNINGS_AS_ERRORS @OptIn(ExperimentalCompilerApi::class) @@ -22,6 +23,7 @@ class MappieCompilerPluginRegistrar : CompilerPluginRegistrar() { messageCollector, MappieConfiguration( warningsAsErrors = configuration.get(ARGUMENT_WARNINGS_AS_ERRORS, false), + useDefaultArguments = configuration.get(ARGUMENT_USE_DEFAULT_ARGUMENTS, true), strictness = StrictnessConfiguration( enums = configuration.get(ARGUMENT_STRICTNESS_ENUMS, true), visibility = configuration.get(ARGUMENT_STRICTNESS_VISIBILITY, false) diff --git a/compiler-plugin/src/main/kotlin/tech/mappie/MappieConfiguration.kt b/compiler-plugin/src/main/kotlin/tech/mappie/MappieConfiguration.kt index c59e9fb0..6d58ca4b 100644 --- a/compiler-plugin/src/main/kotlin/tech/mappie/MappieConfiguration.kt +++ b/compiler-plugin/src/main/kotlin/tech/mappie/MappieConfiguration.kt @@ -2,6 +2,7 @@ package tech.mappie data class MappieConfiguration( val warningsAsErrors: Boolean, + val useDefaultArguments: Boolean, val strictness: StrictnessConfiguration, ) diff --git a/compiler-plugin/src/main/kotlin/tech/mappie/resolving/classes/ObjectMappingsConstructor.kt b/compiler-plugin/src/main/kotlin/tech/mappie/resolving/classes/ObjectMappingsConstructor.kt index ef97cad5..b82780a1 100644 --- a/compiler-plugin/src/main/kotlin/tech/mappie/resolving/classes/ObjectMappingsConstructor.kt +++ b/compiler-plugin/src/main/kotlin/tech/mappie/resolving/classes/ObjectMappingsConstructor.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.ir.types.defaultType import org.jetbrains.kotlin.ir.types.isNullable import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.name.Name +import tech.mappie.MappieIrRegistrar.Companion.context import tech.mappie.resolving.* import tech.mappie.util.* @@ -50,7 +51,7 @@ class ObjectMappingsConstructor(val targetType: IrType, val source: IrValueParam else -> clazz.constructors.firstOrNull { it.valueParameters.isEmpty() }?.let { irConstructorCall(it) } } listOf(ResolvedSource(getter.symbol, irGet(source), via, viaDispatchReceiver)) - } else if (target.hasDefaultValue()) { + } else if (target.hasDefaultValue() && context.configuration.useDefaultArguments) { listOf(ValueSource(target.defaultValue!!.expression, null)) } else { emptyList() diff --git a/gradle-plugin/src/main/kotlin/tech/mappie/MappieExtension.kt b/gradle-plugin/src/main/kotlin/tech/mappie/MappieExtension.kt index 733e19c0..bff9c012 100644 --- a/gradle-plugin/src/main/kotlin/tech/mappie/MappieExtension.kt +++ b/gradle-plugin/src/main/kotlin/tech/mappie/MappieExtension.kt @@ -12,6 +12,11 @@ abstract class MappieExtension(private val project: Project) { */ abstract val warningsAsErrors: Property + /** + * Use default arguments if no mapping exists. + */ + abstract val useDefaultArguments: Property + internal val strictness: MappieStrictnessExtension get() = extensions.getOrPut(MappieStrictnessExtension.NAME) { project.objects.newInstance(MappieStrictnessExtension::class.java) diff --git a/gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt b/gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt index 1aac44d2..948ff266 100644 --- a/gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt +++ b/gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt @@ -20,6 +20,9 @@ class MappieGradlePlugin : KotlinCompilerPluginSupportPlugin { extension.warningsAsErrors.orNull?.apply { add(SubpluginOption("warningsAsErrors", this.toString())) } + extension.useDefaultArguments.orNull?.apply { + add(SubpluginOption("useDefaultArguments", this.toString())) + } extension.strictness.enums.orNull?.apply { add(SubpluginOption("strictness.enums", this.toString() )) } diff --git a/website/src/changelog.md b/website/src/changelog.md index 6e707916..ba2451d2 100644 --- a/website/src/changelog.md +++ b/website/src/changelog.md @@ -8,6 +8,7 @@ changelog: - "[#17](https://github.com/Mr-Mappie/mappie/issues/17) mappers can now be declared as inner declarations, instead of requiring them to be top-level." - "[#31](https://github.com/Mr-Mappie/mappie/issues/31) added the explicit mapping method `thrownByEnumEntry` to throw an exception as a result when mapping an enum entry." - "[#28](https://github.com/Mr-Mappie/mappie/issues/28) added implicit mapping inference of mappers with the same name but a different type, but a mapper for those types are defined." + - "[#42](https://github.com/Mr-Mappie/mappie/issues/42) added a configuration option to disable resolving via default arguments." - "Several other bug fixes." - date: "2024-06-27" title: "v0.2.0" diff --git a/website/src/posts/getting-started/posts/configuration.md b/website/src/posts/getting-started/posts/configuration.md index 6e67427f..5eff73a1 100644 --- a/website/src/posts/getting-started/posts/configuration.md +++ b/website/src/posts/getting-started/posts/configuration.md @@ -10,7 +10,8 @@ eleventyNavigation: Mappie can be configured via Gradle. The following global configuration options are available ```kotlin mappie { - warningsAsErrors = true // Enable reporting warnings as errors + warningsAsErrors = true // Enable reporting warnings as errors] + useDefaultArguments = false // Disable using default arguments as sources. strictness { visibility = true // Allow calling constructors not visible from the calling scope enums = true // Do not report an error if not all enum sources are mapped @@ -19,8 +20,9 @@ mappie { ``` with the following default values -| Option | Default Value | -|-------------------------|----------------| -| `warningsAsErrors` | `false` | -| `strictness.visibility` | `false` | -| `strictness.enums` | `false` | +| Option | Default Value | +|--------------------------|---------------| +| `warningsAsErrors` | `false` | +| `useDefaultArguments` | `true` | +| `strictness.visibility` | `false` | +| `strictness.enums` | `false` |