Skip to content

Commit

Permalink
Changed all explicit mapping functions to drop the mapped prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier committed Jun 21, 2024
1 parent 92e60aa commit 8b47618
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ val IDENTIFIER_MAP_LIST = Name.identifier("mapList")

val IDENTIFIER_MAP_SET = Name.identifier("mapSet")

val IDENTIFIER_MAPPED_FROM_ENUM_ENTRY = Name.identifier("mappedFromEnumEntry")
val IDENTIFIER_MAPPED_FROM_ENUM_ENTRY = Name.identifier("fromEnumEntry")

val IDENTIFIER_MAPPED_FROM_PROPERTY = Name.identifier("mappedFromProperty")
val IDENTIFIER_MAPPED_FROM_PROPERTY = Name.identifier("fromProperty")

val IDENTIFIER_MAPPED_FROM_CONSTANT = Name.identifier("mappedFromConstant")
val IDENTIFIER_MAPPED_FROM_CONSTANT = Name.identifier("fromConstant")

val IDENTIFIER_MAPPED_FROM_EXPRESSION = Name.identifier("mappedFromExpression")
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 @@ -24,9 +24,9 @@ public abstract class EnumMappie<FROM: Enum<*>, TO : Enum<*>> : Mappie<FROM, TO>
*
* For example
* ```kotlin
* Colour.UNKNOWN mappedFromEnumEntry Color.ORANGE
* Colour.UNKNOWN fromEnumEntry Color.ORANGE
* ```
* will generate an explicit mapping, mapping `Colour.ORANGE` to `Color.UNKNOWN`.
*/
protected infix fun TO.mappedFromEnumEntry(source: FROM): EnumMappie<FROM, TO> = generated()
protected infix fun TO.fromEnumEntry(source: FROM): EnumMappie<FROM, TO> = generated()
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,44 @@ public abstract class ObjectMappie<FROM, TO> : Mappie<FROM, TO>() {
*
* For example
* ```kotlin
* Person::name mappedFromProperty PersonDto::fullName
* Person::name fromProperty PersonDto::fullName
* ```
* will generate an explicit mapping, setting constructor parameter `Person.name` to `PersonDto.fullName`.
*/
protected infix fun <TO_TYPE, FROM_TYPE> KProperty1<TO, TO_TYPE>.mappedFromProperty(source: KProperty1<FROM, FROM_TYPE>): TransformableValue<FROM_TYPE, TO_TYPE> =
protected infix fun <TO_TYPE, FROM_TYPE> KProperty1<TO, TO_TYPE>.fromProperty(source: KProperty1<FROM, FROM_TYPE>): TransformableValue<FROM_TYPE, TO_TYPE> =
generated()

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

/**
* Explicitly construct a mapping to [TO] from expression source [function].
*
* For example
* ```kotlin
* Person::name mappedFromConstant { personDto -> personDto.fullName + " (full)" }
* Person::name fromConstant { personDto -> personDto.fullName + " (full)" }
* ```
* will generate an explicit mapping, setting constructor parameter `Person.name` to `"John Doe (full)"`,
* assuming `personDto.fullName == "John Doe"`.
*/
protected infix fun <TO_TYPE> KProperty1<TO, TO_TYPE>.mappedFromExpression(function: (FROM) -> TO_TYPE): Unit =
protected infix fun <TO_TYPE> KProperty1<TO, TO_TYPE>.fromExpression(function: (FROM) -> TO_TYPE): Unit =
generated()

/**
* Reference a constructor parameter in lieu of a property reference, if it not exists as a property.
*
* For example
* ```kotlin
* parameter("name") mappedFromProperty PersonDto::fullName
* parameter("name") fromProperty PersonDto::fullName
* ```
* will generate an explicit mapping, setting constructor parameter `name` to `PersonDto.fullName`.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TransformableValue<FROM, TO> {
*
* For example
* ```kotlin
* Person::age mappedFromProperty PersonDto::dateOfBirth transform { it.periodUntil(Clock.todayIn(TimeZone.UTC)) }
* Person::age fromProperty PersonDto::dateOfBirth transform { it.periodUntil(Clock.todayIn(TimeZone.UTC)) }
* ```
* will generate an explicit mapping transforming `PersonDto.dateOfBirth` to the period between it and today.
*
Expand Down
2 changes: 1 addition & 1 deletion testing/src/main/kotlin/testing/ClassMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class ClassDto(

object ClassMapper : ObjectMappie<Class, ClassDto>() {
override fun map(from: Class) = mapping {
ClassDto::argument mappedFromConstant 1
ClassDto::argument fromConstant 1
}
}
4 changes: 2 additions & 2 deletions testing/src/main/kotlin/testing/ColorMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum class Colour {

object ColorMapper : EnumMappie<Color, Colour>() {
override fun map(from: Color): Colour = mapping {
Colour.OTHER mappedFromEnumEntry Color.ORANGE
Colour.OTHER mappedFromEnumEntry Color.PURPLE
Colour.OTHER fromEnumEntry Color.ORANGE
Colour.OTHER fromEnumEntry Color.PURPLE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class ConstructorParameterWhichIsNotAFieldDto(

object ConstructorParameterWhichIsNotAFieldMapper : ObjectMappie<ConstructorParameterWhichIsNotAField, ConstructorParameterWhichIsNotAFieldDto>() {
override fun map(from: ConstructorParameterWhichIsNotAField): ConstructorParameterWhichIsNotAFieldDto = mapping {
parameter("value") mappedFromProperty ConstructorParameterWhichIsNotAField::parameter
parameter("value") fromProperty ConstructorParameterWhichIsNotAField::parameter
}
}
4 changes: 2 additions & 2 deletions testing/src/main/kotlin/testing/ExpressionMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.github.mappie.api.ObjectMappie

object ExpressionMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person): PersonDto = mapping {
PersonDto::age mappedFromConstant 10
PersonDto::description mappedFromExpression { it::class.simpleName!! }
PersonDto::age fromConstant 10
PersonDto::description fromExpression { it::class.simpleName!! }
}
}
2 changes: 1 addition & 1 deletion testing/src/main/kotlin/testing/GameMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ data class GameDto(

object GameMapper : ObjectMappie<Game, GameDto>() {
override fun map(from: Game): GameDto = mapping {
GameDto::description mappedFromProperty Game::description transform { it ?: "default" }
GameDto::description fromProperty Game::description transform { it ?: "default" }
}
}
4 changes: 2 additions & 2 deletions testing/src/main/kotlin/testing/ListMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ data class BookSetDto(val pages: Set<String>)

object BookListMapper : ObjectMappie<BookList, BookListDto>() {
override fun map(from: BookList): BookListDto = mapping {
BookListDto::pages mappedFromProperty BookList::pages via PageMapper.forList
BookListDto::pages fromProperty BookList::pages via PageMapper.forList
}
}

object BookSetMapper : ObjectMappie<BookSet, BookSetDto>() {
override fun map(from: BookSet): BookSetDto = mapping {
BookSetDto::pages mappedFromProperty BookSet::pages via PageMapper.forSet
BookSetDto::pages fromProperty BookSet::pages via PageMapper.forSet
}
}

Expand Down
2 changes: 1 addition & 1 deletion testing/src/main/kotlin/testing/MultipleConstructor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ object MultipleConstructorsWithoutIntMapper : ObjectMappie<MultipleConstructors,

object MultipleConstructorsWitIntMapper : ObjectMappie<MultipleConstructors, MultipleConstructorsDto>() {
override fun map(from: MultipleConstructors): MultipleConstructorsDto = mapping {
MultipleConstructorsDto::int mappedFromConstant 2
MultipleConstructorsDto::int fromConstant 2
}
}
4 changes: 2 additions & 2 deletions testing/src/main/kotlin/testing/NestedMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ data class ThangDto(val description: String)

object ThingMapper : ObjectMappie<Thing, ThingDto>() {
override fun map(from: Thing): ThingDto = mapping {
ThingDto::inner mappedFromProperty Thing::inner via ThangMapper
ThingDto::boolean mappedFromProperty Thing::boolean via BooleanMapper()
ThingDto::inner fromProperty Thing::inner via ThangMapper
ThingDto::boolean fromProperty Thing::boolean via BooleanMapper()
}
}

Expand Down
8 changes: 4 additions & 4 deletions testing/src/main/kotlin/testing/PersonMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ data class PersonDto(val name: String, val description: String, val age: Int)
object PersonMapper : ObjectMappie<Person, PersonDto>() {

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

Expand All @@ -26,7 +26,7 @@ object ConstructorCallPersonMapper : Mappie<Person, PersonDto>() {
object TransformingPersonMapper : ObjectMappie<Person, PersonDto>() {

override fun map(from: Person): PersonDto = mapping {
PersonDto::description mappedFromProperty Person::name transform { "$it Surname" }
PersonDto::age mappedFromConstant 24
PersonDto::description fromProperty Person::name transform { "$it Surname" }
PersonDto::age fromConstant 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 mappedFromConstant 1
PrivateConstructorDto::int fromConstant 1
}
}
4 changes: 2 additions & 2 deletions website/src/posts/enum-mapping/posts/entry-mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ enum class Color { RED, GREEN, BLUE, ORANGE; }
enum class Colour { RED, GREEN, BLUE, OTHER; }
```

We can generate a complete mapper by mapping `Colour.ORANGE` to `Colour.OTHER` via `mappedFromEnumEntry`
We can generate a complete mapper by mapping `Colour.ORANGE` to `Colour.OTHER` via `fromEnumEntry`
```kotlin
object ColorMapper : EnumMappie<Color, Colour>() {
override fun map(from: Color): Colour = mapping {
Colour.OTHER mappedFromEnumEntry Color.ORANGE
Colour.OTHER fromEnumEntry Color.ORANGE
}
}
```
2 changes: 1 addition & 1 deletion website/src/posts/object-mapping/posts/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object PageMapper : ObjectMappie<Page, String>() {

object BookMapper : ObjectMappie<Book, BookDto>() {
override fun map(from: Book): BookDto = mapping {
BookDto::pages mappedFromProperty Book::pages via PageMapper.forList
BookDto::pages fromProperty Book::pages via PageMapper.forList
}
}
```
16 changes: 8 additions & 8 deletions website/src/posts/object-mapping/posts/resolving.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ When a mapping function is defined without any explicit mappings, Mappie will th
3. setting the target from an expression.

## Mapping via a Source Property
Targets can be set via the operator `mappedFromProperty`. This will set the target to the given source property.
Targets can be set via the operator `fromProperty`. This will set the target to the given source property.
For example
```kotlin
object PersonMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person): PersonDto = mapping {
PersonDto::description mappedFromProperty Person::name
PersonDto::description fromProperty Person::name
}
}
```
Expand All @@ -42,28 +42,28 @@ Sometimes, you want to map from a source property, but tweak the value, handle n
some other way. See [Transforming](/object-mapping/transforming/) for some guidelines.

## Mapping via a Constant
Targets can be set via the operator `mappedFromConstant`. This will set the target to the given constant.
Targets can be set via the operator `fromConstant`. This will set the target to the given constant.
For example
```kotlin
object PersonMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person): PersonDto = mapping {
PersonDto::description mappedFromConstant "unknown"
PersonDto::description fromConstant "unknown"
}
}
```
will always set `PersonDto.description` to `"unknown`.

## Mapping via an Expression
Targets can be set via the operator `mappedFromExpression`. This will set the target to the given lambda result.
Targets can be set via the operator `fromExpression`. This will set the target to the given lambda result.

The difference between `mappedFromExpression` and `mappedFromConstant` is that `mappedFromExpression` will take a lambda
The difference between `fromExpression` and `fromConstant` 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
```kotlin
object PersonMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person): PersonDto = mapping {
PersonDto::description mappedFromExpression { from -> "Description: ${from.name}" }
PersonDto::description fromExpression { from -> "Description: ${from.name}" }
}
}
```
Expand All @@ -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") mappedFromConstant "a constant"
parameter("description") fromConstant "a constant"
}
}
```
2 changes: 1 addition & 1 deletion website/src/posts/object-mapping/posts/reusing.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ the operator `via`
```kotlin
object PersonMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Address) = mapping {
PersonDto::address mappedFromProperty PersonDto::address via AddressMapper
PersonDto::address fromProperty PersonDto::address via AddressMapper
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions website/src/posts/object-mapping/posts/transforming.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ we can create a mapper between `Person` and `PersonDto` via
```kotlin
object PersonMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person): PersonDto = mapping {
PersonDto::age mappedFromProperty Person::dateOfBirth transform { dateOfBirth ->
PersonDto::age fromProperty Person::dateOfBirth transform { dateOfBirth ->
Clock.todayIn(TimeZone.currentSystemDefault()).periodUntil(dateOfBirth)
}
}
Expand All @@ -48,7 +48,7 @@ We create a mapping between `Dog` and `DogDto` via
```kotlin
object DogMapper : ObjectMappie<Dog, DogDto>() {
override fun map(from: Dog): DogDto = mapping {
DogDto::name mappedFromProperty DogDto::name transform {
DogDto::name fromProperty DogDto::name transform {
it ?: "unknown"
}
}
Expand Down

0 comments on commit 8b47618

Please sign in to comment.