@@ -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
2129data class Book (val pages : List <Page >)
2230
@@ -28,19 +36,27 @@ data class BookDto(val pages: List<String>)
2836```
2937
3038We 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
3341object PageMapper : ObjectMappie<Page, String>() {
3442 override fun map (from : Page ): String = from.text
3543}
3644
3745object 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
4561object BookMapper : ObjectMappie<Book, BookDto>()
4662```
0 commit comments