Skip to content

Commit c10101a

Browse files
kiviewEugen
authored and
Eugen
committed
Add Kotlin data mapping examples (BAEL-2247) (eugenp#5560)
1 parent bf2da7e commit c10101a

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

core-kotlin/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@
4040
- [Converting Kotlin Data Class from JSON using GSON](https://www.baeldung.com/kotlin-json-convert-data-class)
4141
- [Concatenate Strings in Kotlin](https://www.baeldung.com/kotlin-concatenate-strings)
4242
- [Kotlin return, break, continue Keywords](https://www.baeldung.com/kotlin-return-break-continue)
43+
- [Mapping of Data Objects in Kotlin](https://www.baeldung.com/kotlin-data-objects-mapping)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.datamapping
2+
3+
data class User(
4+
val firstName: String,
5+
val lastName: String,
6+
val street: String,
7+
val houseNumber: String,
8+
val phone: String,
9+
val age: Int,
10+
val password: String)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.datamapping
2+
3+
import kotlin.reflect.full.memberProperties
4+
5+
fun User.toUserView() = UserView(
6+
name = "$firstName $lastName",
7+
address = "$street $houseNumber",
8+
telephone = phone,
9+
age = age
10+
)
11+
12+
fun User.toUserViewReflection() = with(::UserView) {
13+
val propertiesByName = User::class.memberProperties.associateBy { it.name }
14+
callBy(parameters.associate { parameter ->
15+
parameter to when (parameter.name) {
16+
UserView::name.name -> "$firstName $lastName"
17+
UserView::address.name -> "$street $houseNumber"
18+
UserView::telephone.name -> phone
19+
else -> propertiesByName[parameter.name]?.get(this@toUserViewReflection)
20+
}
21+
})
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.datamapping
2+
3+
data class UserView(
4+
val name: String,
5+
val address: String,
6+
val telephone: String,
7+
val age: Int
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.datamapping
2+
3+
import org.junit.jupiter.api.Test
4+
import org.junit.jupiter.api.assertAll
5+
import kotlin.test.assertEquals
6+
7+
class UserTest {
8+
9+
@Test
10+
fun `maps User to UserResponse using extension function`() {
11+
val p = buildUser()
12+
val view = p.toUserView()
13+
assertUserView(view)
14+
}
15+
16+
@Test
17+
fun `maps User to UserResponse using reflection`() {
18+
val p = buildUser()
19+
val view = p.toUserViewReflection()
20+
assertUserView(view)
21+
}
22+
23+
private fun buildUser(): User {
24+
return User(
25+
"Java",
26+
"Duke",
27+
"Javastreet",
28+
"42",
29+
"1234567",
30+
30,
31+
"s3cr37"
32+
)
33+
}
34+
35+
private fun assertUserView(pr: UserView) {
36+
assertAll(
37+
{ assertEquals("Java Duke", pr.name) },
38+
{ assertEquals("Javastreet 42", pr.address) },
39+
{ assertEquals("1234567", pr.telephone) },
40+
{ assertEquals(30, pr.age) }
41+
)
42+
}
43+
}

0 commit comments

Comments
 (0)