From c10101a9acccd2fef6bbe8353aafd7354574cd9c Mon Sep 17 00:00:00 2001 From: Kevin Wittek Date: Sun, 28 Oct 2018 18:22:24 +0100 Subject: [PATCH] Add Kotlin data mapping examples (BAEL-2247) (#5560) --- core-kotlin/README.md | 1 + .../kotlin/com/baeldung/datamapping/User.kt | 10 +++++ .../baeldung/datamapping/UserExtensions.kt | 22 ++++++++++ .../com/baeldung/datamapping/UserView.kt | 8 ++++ .../com/baeldung/datamapping/UserTest.kt | 43 +++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/datamapping/User.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserExtensions.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserView.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/datamapping/UserTest.kt diff --git a/core-kotlin/README.md b/core-kotlin/README.md index 9906b59a93..ed6894c2c1 100644 --- a/core-kotlin/README.md +++ b/core-kotlin/README.md @@ -40,3 +40,4 @@ - [Converting Kotlin Data Class from JSON using GSON](https://www.baeldung.com/kotlin-json-convert-data-class) - [Concatenate Strings in Kotlin](https://www.baeldung.com/kotlin-concatenate-strings) - [Kotlin return, break, continue Keywords](https://www.baeldung.com/kotlin-return-break-continue) +- [Mapping of Data Objects in Kotlin](https://www.baeldung.com/kotlin-data-objects-mapping) diff --git a/core-kotlin/src/main/kotlin/com/baeldung/datamapping/User.kt b/core-kotlin/src/main/kotlin/com/baeldung/datamapping/User.kt new file mode 100644 index 0000000000..38c88b7c37 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/datamapping/User.kt @@ -0,0 +1,10 @@ +package com.baeldung.datamapping + +data class User( + val firstName: String, + val lastName: String, + val street: String, + val houseNumber: String, + val phone: String, + val age: Int, + val password: String) \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserExtensions.kt b/core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserExtensions.kt new file mode 100644 index 0000000000..1f3d7f3b47 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserExtensions.kt @@ -0,0 +1,22 @@ +package com.baeldung.datamapping + +import kotlin.reflect.full.memberProperties + +fun User.toUserView() = UserView( + name = "$firstName $lastName", + address = "$street $houseNumber", + telephone = phone, + age = age +) + +fun User.toUserViewReflection() = with(::UserView) { + val propertiesByName = User::class.memberProperties.associateBy { it.name } + callBy(parameters.associate { parameter -> + parameter to when (parameter.name) { + UserView::name.name -> "$firstName $lastName" + UserView::address.name -> "$street $houseNumber" + UserView::telephone.name -> phone + else -> propertiesByName[parameter.name]?.get(this@toUserViewReflection) + } + }) +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserView.kt b/core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserView.kt new file mode 100644 index 0000000000..ca27b1961c --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserView.kt @@ -0,0 +1,8 @@ +package com.baeldung.datamapping + +data class UserView( + val name: String, + val address: String, + val telephone: String, + val age: Int +) \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/datamapping/UserTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/datamapping/UserTest.kt new file mode 100644 index 0000000000..2d03039a80 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/datamapping/UserTest.kt @@ -0,0 +1,43 @@ +package com.baeldung.datamapping + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll +import kotlin.test.assertEquals + +class UserTest { + + @Test + fun `maps User to UserResponse using extension function`() { + val p = buildUser() + val view = p.toUserView() + assertUserView(view) + } + + @Test + fun `maps User to UserResponse using reflection`() { + val p = buildUser() + val view = p.toUserViewReflection() + assertUserView(view) + } + + private fun buildUser(): User { + return User( + "Java", + "Duke", + "Javastreet", + "42", + "1234567", + 30, + "s3cr37" + ) + } + + private fun assertUserView(pr: UserView) { + assertAll( + { assertEquals("Java Duke", pr.name) }, + { assertEquals("Javastreet 42", pr.address) }, + { assertEquals("1234567", pr.telephone) }, + { assertEquals(30, pr.age) } + ) + } +} \ No newline at end of file