File tree 5 files changed +84
-0
lines changed
main/kotlin/com/baeldung/datamapping
test/kotlin/com/baeldung/datamapping
5 files changed +84
-0
lines changed Original file line number Diff line number Diff line change 40
40
- [ Converting Kotlin Data Class from JSON using GSON] ( https://www.baeldung.com/kotlin-json-convert-data-class )
41
41
- [ Concatenate Strings in Kotlin] ( https://www.baeldung.com/kotlin-concatenate-strings )
42
42
- [ 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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments