Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to map Kotlin data objects to data objects

I want to convert/map some "data" class objects to similar "data" class objects. For example, classes for web form to classes for database records.

data class PersonForm(     val firstName: String,     val lastName: String,     val age: Int,     // maybe many fields exist here like address, card number, etc.     val tel: String ) // maps to ... data class PersonRecord(     val name: String, // "${firstName} ${lastName}"     val age: Int, // copy of age     // maybe many fields exist here like address, card number, etc.     val tel: String // copy of tel ) 

I use ModelMapper for such works in Java, but it can't be used because data classes are final (ModelMapper creates CGLib proxies to read mapping definitions). We can use ModelMapper when we make these classes/fields open, but we must implement features of "data" class manually. (cf. ModelMapper examples: https://github.com/jhalterman/modelmapper/blob/master/examples/src/main/java/org/modelmapper/gettingstarted/GettingStartedExample.java)

How to map such "data" objects in Kotlin?

Update: ModelMapper automatically maps fields that have same name (like tel -> tel) without mapping declarations. I want to do it with data class of Kotlin.

Update: The purpose of each classes depends on what kind of application, but these are probably placed in the different layer of an application.

For example:

  • data from database (Database Entity) to data for HTML form (Model/View Model)
  • REST API result to data for database

These classes are similar, but are not the same.

I want to avoid normal function calls for these reasons:

  • It depends on the order of arguments. A function for a class with many fields that have same type (like String) will be broken easily.
  • Many declarations are nesessary though most mappings can be resolved with naming convention.

Of course, a library that has similar feature is intended, but information of the Kotlin feature is also welcome (like spreading in ECMAScript).

like image 495
sunnyone Avatar asked Aug 29 '16 06:08

sunnyone


People also ask

How do I map one object to another in Kotlin?

We are using the UserView default constructor as the method call receiver by using the Kotlin with() function. Inside the lambda function provided to with(), we use reflection to obtain a Map of member properties (with the member name as the key and the member property as the value) using User::class. memberProperties.

How do I add data to map in Kotlin?

Add and update entries To add multiple entries at a time, use putAll() . Its argument can be a Map or a group of Pair s: Iterable , Sequence , or Array . Both put() and putAll() overwrite the values if the given keys already exist in the map. Thus, you can use them to update values of map entries.


1 Answers

  1. Simplest (best?):

    fun PersonForm.toPersonRecord() = PersonRecord(         name = "$firstName $lastName",         age = age,         tel = tel ) 
  2. Reflection (not great performance):

    fun PersonForm.toPersonRecord() = with(PersonRecord::class.primaryConstructor!!) {     val propertiesByName = PersonForm::class.memberProperties.associateBy { it.name }     callBy(args = parameters.associate { parameter ->         parameter to when (parameter.name) {             "name" -> "$firstName $lastName"             else -> propertiesByName[parameter.name]?.get(this@toPersonRecord)         }     }) } 
  3. Cached reflection (okay performance but not as fast as #1):

    open class Transformer<in T : Any, out R : Any> protected constructor(inClass: KClass<T>, outClass: KClass<R>) {     private val outConstructor = outClass.primaryConstructor!!     private val inPropertiesByName by lazy {         inClass.memberProperties.associateBy { it.name }     }      fun transform(data: T): R = with(outConstructor) {         callBy(parameters.associate { parameter ->             parameter to argFor(parameter, data)         })     }      open fun argFor(parameter: KParameter, data: T): Any? {         return inPropertiesByName[parameter.name]?.get(data)     } }  val personFormToPersonRecordTransformer = object : Transformer<PersonForm, PersonRecord>(PersonForm::class, PersonRecord::class) {     override fun argFor(parameter: KParameter, data: PersonForm): Any? {         return when (parameter.name) {             "name" -> with(data) { "$firstName $lastName" }             else -> super.argFor(parameter, data)         }     } }  fun PersonForm.toPersonRecord() = personFormToPersonRecordTransformer.transform(this) 
  4. Storing Properties in a Map

    data class PersonForm(val map: Map<String, Any?>) {     val firstName: String   by map     val lastName: String    by map     val age: Int            by map     // maybe many fields exist here like address, card number, etc.     val tel: String         by map }  // maps to ... data class PersonRecord(val map: Map<String, Any?>) {     val name: String    by map // "${firstName} ${lastName}"     val age: Int        by map // copy of age     // maybe many fields exist here like address, card number, etc.     val tel: String     by map // copy of tel }  fun PersonForm.toPersonRecord() = PersonRecord(HashMap(map).apply {     this["name"] = "${remove("firstName")} ${remove("lastName")}" }) 
like image 72
mfulton26 Avatar answered Sep 22 '22 09:09

mfulton26