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

Added fromValue and deprecated fromConstant #25

Merged
merged 1 commit into from
Jun 25, 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 @@ -93,9 +93,8 @@ class IrTransformer : IrElementTransformerVoidWithContext() {
fun ObjectMappingSource.toIr(builder: IrBuilderWithScope): IrExpression =
when (this) {
is PropertySource -> toIr(builder)
is DefaultParameterValueSource -> value
is ExpressionSource -> toIr(builder)
is ConstantSource<*> -> value
is ValueSource -> value
}

fun ExpressionSource.toIr(builder: IrBuilderWithScope): IrExpression {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ val IDENTIFIER_MAPPED_FROM_PROPERTY = Name.identifier("fromProperty")

val IDENTIFIER_MAPPED_FROM_CONSTANT = Name.identifier("fromConstant")

val IDENTIFIER_MAPPED_FROM_VALUE = Name.identifier("fromValue")

val IDENTIFIER_MAPPED_FROM_EXPRESSION = Name.identifier("fromExpression")

val IDENTIFIER_PARAMETER = Name.identifier("parameter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ private class ObjectBodyStatementCollector(

target to source
}
IDENTIFIER_MAPPED_FROM_VALUE -> {
val target = expression.extensionReceiver!!.accept(TargetValueCollector(file!!), data)
val source = expression.valueArguments.first()!!

target to ValueSource(source)
}
IDENTIFIER_MAPPED_FROM_EXPRESSION -> {
val target = expression.extensionReceiver!!.accept(TargetValueCollector(file!!), data)
val source = expression.valueArguments.first() as IrFunctionExpression
Expand Down Expand Up @@ -207,7 +213,7 @@ private class SourceValueCollector(
}

override fun visitConst(expression: IrConst<*>, data: Unit): ObjectMappingSource {
return ConstantSource(expression.type, expression)
return ValueSource(expression)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tech.mappie.resolving.classes

import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionExpression
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
Expand Down Expand Up @@ -41,15 +40,8 @@ data class ExpressionSource(
override fun resolveType() = type
}

data class DefaultParameterValueSource(
data class ValueSource(
val value: IrExpression,
) : ObjectMappingSource {
override fun resolveType() = value.type
}

data class ConstantSource<T>(
val type: IrType,
val value: IrConst<T>,
) : ObjectMappingSource {
override fun resolveType() = type
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ObjectMappingsConstructor(val targetType: IrType, val source: IrValueParam
if (getter != null) {
listOf(PropertySource(getter.symbol, getter.returnType, source.symbol, true))
} else if (target.hasDefaultValue()) {
listOf(DefaultParameterValueSource(target.defaultValue!!.expression))
listOf(ValueSource(target.defaultValue!!.expression))
} else {
emptyList()
}
Expand Down
15 changes: 14 additions & 1 deletion mappie-api/src/commonMain/kotlin/tech/mappie/api/ObjectMappie.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,28 @@ public abstract class ObjectMappie<FROM, TO> : Mappie<FROM, TO>() {
* ```
* will generate an explicit mapping, setting constructor parameter `Person.name` to `"John Doe"`.
*/
@Deprecated("This function is unnecessarily limiting.", replaceWith = ReplaceWith("this fromValue value"))
protected infix fun <TO_TYPE> KProperty1<TO, TO_TYPE>.fromConstant(value: TO_TYPE): Unit =
generated()

/**
* Explicitly construct a mapping to [TO] from a value source [value].
*
* For example
* ```kotlin
* Person::name fromValue "John Doe"
* ```
* will generate an explicit mapping, setting constructor parameter `Person.name` to `"John Doe"`.
*/
protected infix fun <TO_TYPE> KProperty1<TO, TO_TYPE>.fromValue(value: TO_TYPE): Unit =
generated()

/**
* Explicitly construct a mapping to [TO] from expression source [function].
*
* For example
* ```kotlin
* Person::name fromConstant { personDto -> personDto.fullName + " (full)" }
* Person::name fromExpression { personDto -> personDto.fullName + " (full)" }
* ```
* will generate an explicit mapping, setting constructor parameter `Person.name` to `"John Doe (full)"`,
* assuming `personDto.fullName == "John Doe"`.
Expand Down
2 changes: 1 addition & 1 deletion testing/src/main/kotlin/testing/ExpressionMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tech.mappie.api.ObjectMappie

object ExpressionMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person): PersonDto = mapping {
PersonDto::age fromConstant 10
PersonDto::age fromValue 10
PersonDto::description fromExpression { it::class.simpleName!! }
}
}
4 changes: 2 additions & 2 deletions testing/src/main/kotlin/testing/PersonMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object PersonMapper : ObjectMappie<Person, PersonDto>() {

override fun map(from: Person): PersonDto = mapping {
PersonDto::description fromProperty Person::name
PersonDto::age fromConstant 26
PersonDto::age fromValue 26
}
}

Expand All @@ -27,6 +27,6 @@ object TransformingPersonMapper : ObjectMappie<Person, PersonDto>() {

override fun map(from: Person): PersonDto = mapping {
PersonDto::description fromProperty Person::name transform { "$it Surname" }
PersonDto::age fromConstant 24
PersonDto::age fromValue 24
}
}
2 changes: 1 addition & 1 deletion testing/src/main/kotlin/testing/PrivateConstructor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ data class PrivateConstructorDto constructor(val string: String, val int: Int) {

object PrivateConstructorMapper : ObjectMappie<PrivateConstructor, PrivateConstructorDto>() {
override fun map(from: PrivateConstructor): PrivateConstructorDto = mapping {
PrivateConstructorDto::int fromConstant 1
PrivateConstructorDto::int fromValue 1
}
}
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:
- "[#13](https://github.com/Mr-Mappie/mappie/issues/13) added support for declaring a mapper without an implementation of map."
- "[#16](https://github.com/Mr-Mappie/mappie/issues/16) improved resolution of explicit parameter names."
- "[#21](https://github.com/Mr-Mappie/mappie/issues/21) added global configuration option to report all warnings as errors."
- "[#24](https://github.com/Mr-Mappie/mappie/issues/24) added explicit mapping fromValue as a replacement of the much more restricting fromConstant."
- date: "2024-06-22"
title: "v0.1.0"
items:
Expand Down
10 changes: 5 additions & 5 deletions website/src/posts/object-mapping/posts/resolving.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ will set `PersonDto.description` to `Person.name`.
Sometimes, you want to map from a source property, but tweak the value, handle nullability, or transform the source in
some other way. See [Transforming](/object-mapping/transforming/) for some guidelines.

## Mapping via a Constant
Targets can be set via the operator `fromConstant`. This will set the target to the given constant.
## Mapping via a Value
Targets can be set via the operator `fromValue`. This will set the target to the given value.
For example
```kotlin
object PersonMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person): PersonDto = mapping {
PersonDto::description fromConstant "unknown"
PersonDto::description fromValue "unknown"
}
}
```
Expand All @@ -56,7 +56,7 @@ will always set `PersonDto.description` to `"unknown`.
## Mapping via an Expression
Targets can be set via the operator `fromExpression`. This will set the target to the given lambda result.

The difference between `fromExpression` and `fromConstant` is that `fromExpression` will take a lambda
The difference between `fromExpression` and `fromValue` is that `fromExpression` will take a lambda
function as a parameter, which takes the original `source` as a parameter. Allowing for more flexibility.

For example
Expand Down Expand Up @@ -86,7 +86,7 @@ we can use `parameter("description")` to reference the constructor parameter
```kotlin
object PersonMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person): PersonDto = mapping {
parameter("description") fromConstant "a constant"
parameter("description") fromValue "a constant"
}
}
```