Skip to content

Commit 3dec195

Browse files
Fix issue 281 (#303)
* Fix issue 281 * Updated documentation
1 parent 16b00fd commit 3dec195

File tree

4 files changed

+103
-12
lines changed

4 files changed

+103
-12
lines changed

compiler-plugin/src/main/kotlin/tech/mappie/ir/generation/IrMappieGeneratedClass.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ class IrMappieGeneratedClass(override var name: Name) : IrClass() {
109109

110110
companion object {
111111
fun named(source: IrType, target: IrType): IrClass {
112-
val source = source.makeNotNull().dumpKotlinLike()
113-
val target = target.makeNotNull().dumpKotlinLike()
112+
val source = source.identifier()
113+
val target = target.identifier()
114114
return IrMappieGeneratedClass(identifier(source + "To" + target + "Mapper"))
115115
}
116+
117+
private fun IrType.identifier() =
118+
makeNotNull()
119+
.dumpKotlinLike()
120+
.replace("<", "")
121+
.replace(">", "")
116122
}
117123
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package tech.mappie.testing.sets
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
import tech.mappie.testing.MappieTestCase
6+
7+
class MapLinkedHashSetTest : MappieTestCase() {
8+
9+
data class Input(val value: LinkedHashSet<InnerInput>)
10+
data class InnerInput(val value: String)
11+
12+
data class Output(val value: LinkedHashSet<InnerOutput>)
13+
data class InnerOutput(val value: String)
14+
15+
@Test
16+
fun `map LinkedHashSet implicit succeed`() {
17+
compile {
18+
file("Test.kt",
19+
"""
20+
import tech.mappie.api.ObjectMappie
21+
import tech.mappie.testing.sets.MapLinkedHashSetTest.*
22+
23+
class Mapper : ObjectMappie<Input, Output>()
24+
"""
25+
)
26+
} satisfies {
27+
isOk()
28+
hasNoWarningsOrErrors()
29+
30+
val mapper = objectMappie<Input, Output>()
31+
32+
assertThat(mapper.map(Input(LinkedHashSet(listOf(InnerInput("element"))))))
33+
.isEqualTo(Output(LinkedHashSet(emptyList())))
34+
}
35+
}
36+
37+
@Test
38+
fun `map LinkedHashSet explicit succeed`() {
39+
compile {
40+
file("Test.kt",
41+
"""
42+
import tech.mappie.api.ObjectMappie
43+
import tech.mappie.api.Mappie1
44+
import tech.mappie.testing.sets.MapLinkedHashSetTest.*
45+
46+
class Mapper : ObjectMappie<Input, Output>()
47+
48+
class LinkedHashSetMapper<T, R>(private val inner: Mappie1<T, R>) : ObjectMappie<LinkedHashSet<T>, LinkedHashSet<R>>() {
49+
override fun map(from: LinkedHashSet<T>) = LinkedHashSet<R>(from.map(inner::map))
50+
}
51+
"""
52+
)
53+
} satisfies {
54+
isOk()
55+
hasNoWarningsOrErrors()
56+
57+
val mapper = objectMappie<Input, Output>()
58+
59+
assertThat(mapper.map(Input(LinkedHashSet(listOf(InnerInput("element"))))))
60+
.isEqualTo(Output(LinkedHashSet(listOf(InnerOutput("element")))))
61+
}
62+
}
63+
}

website/src/posts/object-mapping/posts/built-in.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ can be applied explicitly, or implicitly.
1313
The types below that are in *cursive* are not available on all platforms. For example, `BigInteger` is only available
1414
on the JVM platform.
1515

16+
## Collection Mappers
17+
There is support for mapping any `Iterable` of type `T` to any of `List`, `Set`, `MutableList`, and `MutableSet` of type `R`.
18+
1619
## Numeric Mappers
1720
The following integer mappers are built-in
1821

@@ -55,6 +58,9 @@ The following UUID mappers are built-in
5558
|--------|--------|--------|
5659
| *UUID* | - | X |
5760

61+
## The identity Mapper
62+
The identity mapper named `IdentityMapper` maps any `T` to any `T`.
63+
5864
## kotlinx-datetime Mappers
5965
The following kotlinx-datetime mappers can be included via the dependency `tech.mappie.api:module-kotlinx-datetime`.
6066

website/src/posts/object-mapping/posts/collections.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@ eleventyNavigation:
77
order: 10
88
---
99

10-
All mappers of Mappie define mapper variants for collections. Specifically, for `List` and `Set`. When we want to map a
11-
property of such a type, we do not want to copy the collection itself, but the elements contained in such a type.
10+
Mappie has extensive support for collection types.
1211

13-
Mappie automatically detects if the property is a (nullable) collection, and will automatically select the correct mapping
14-
function to be used.
12+
An `ObjectMappie` defines the functions `mapList`, `mapSet`,
13+
`mapArray` (JVM only), and nullable variants `mapNullableList`, `mapNullableSet` to map collections directly.
1514

16-
We can also explicitly reference a mapper using the getters `forList` and `forSet` defined
17-
in each mapper.
15+
For example, we can use `mapList` to automatically map a `List`
16+
```kotlin
17+
object PersonMapper : ObjectMappie<Person, PersonDto>()
18+
19+
val persons: List<Person> = listOf(Person("Sjon"), Person("Piet"))
20+
val personDtos: List<PersonDto> = PersonMapper.mapList(persons)
21+
```
1822

19-
For example, suppose we have the data class `Book` containing a list of `Page`
23+
Mappie also defines several built-in mappers to map collections, most notable for `List` and `Set`. These built-in
24+
mappers are used to automatically map collection types. When defining a mapping manually, these mappers can be
25+
referenced explicitly; they are defined in the package `mappie.api.builtin.collections`.
26+
27+
For example, suppose we have the data class `Book` containing a list of `Page`:
2028
```kotlin
2129
data class Book(val pages: List<Page>)
2230

@@ -28,19 +36,27 @@ data class BookDto(val pages: List<String>)
2836
```
2937

3038
We can define a mapping between `Book` and `BookDto` by defining two mappers: a mapper for `Page` to `String`, which simply
31-
gets the `text` property, and mapper between `Book` and `BookDto` using the inner `forList` mapper of the `PageMapper`
39+
gets the `text` property, and a mapper between `Book` and `BookDto` which uses the built-in mapper `IterableToListMapper`:
3240
```kotlin
3341
object PageMapper : ObjectMappie<Page, String>() {
3442
override fun map(from: Page): String = from.text
3543
}
3644

3745
object BookMapper : ObjectMappie<Book, BookDto>() {
3846
override fun map(from: Book): BookDto = mapping {
39-
BookDto::pages fromProperty Book::pages via PageMapper.forList
47+
BookDto::pages fromProperty Book::pages via IterableToListMapper(PageMapper)
48+
}
49+
}
50+
```
51+
Note that in this case `BookMapper` is superfluous and is equivalent to
52+
```kotlin
53+
object BookMapper : ObjectMappie<Book, BookDto>() {
54+
override fun map(from: Book): BookDto = mapping {
55+
BookDto::pages fromProperty Book::pages
4056
}
4157
}
4258
```
43-
Note that `BookMapper` is superfluous and is equivalent to
59+
or even
4460
```kotlin
4561
object BookMapper : ObjectMappie<Book, BookDto>()
4662
```

0 commit comments

Comments
 (0)