Skip to content

Commit

Permalink
Added configuration option to disable default value argument sources (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier authored Jul 2, 2024
1 parent cf781b7 commit 99e0c6a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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}")
Expand All @@ -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<Boolean>(OPTION_WARNINGS_AS_ERRORS)
val ARGUMENT_USE_DEFAULT_ARGUMENTS = CompilerConfigurationKey<Boolean>(OPTION_USE_DEFAULT_ARGUMENTS)
val ARGUMENT_STRICTNESS_ENUMS = CompilerConfigurationKey<Boolean>(OPTION_STRICTNESS_ENUMS)
val ARGUMENT_STRICTNESS_VISIBILITY = CompilerConfigurationKey<Boolean>(OPTION_STRICTNESS_VISIBILITY)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tech.mappie

data class MappieConfiguration(
val warningsAsErrors: Boolean,
val useDefaultArguments: Boolean,
val strictness: StrictnessConfiguration,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions gradle-plugin/src/main/kotlin/tech/mappie/MappieExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ abstract class MappieExtension(private val project: Project) {
*/
abstract val warningsAsErrors: Property<Boolean>

/**
* Use default arguments if no mapping exists.
*/
abstract val useDefaultArguments: Property<Boolean>

internal val strictness: MappieStrictnessExtension get() =
extensions.getOrPut(MappieStrictnessExtension.NAME) {
project.objects.newInstance(MappieStrictnessExtension::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() ))
}
Expand Down
1 change: 1 addition & 0 deletions website/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 8 additions & 6 deletions website/src/posts/getting-started/posts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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` |

0 comments on commit 99e0c6a

Please sign in to comment.