Last active
December 26, 2020 14:35
-
-
Save KaustubhPatange/089792e18c19783247bb5b554e4ccaee to your computer and use it in GitHub Desktop.
Mapping a data class to another data class typically found in the apps which follows clean architecture (domain, etc.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* If there is two same classes like below one maybe domain & other may be entity | |
* Following method might help to map such instance to other classes. | |
* | |
* This is done completely through reflection by matching parameters name. | |
* | |
* [convertType] This can be used to transform intermediate values if their types are different in other. | |
*/ | |
inline fun <reified F : Any, reified T : Any> mapToClass(from: F, convertType: (String, Any?) -> Any? = { _,v -> v }): T { | |
val args = HashMap<KParameter, Any?>() | |
val params: List<KParameter> = T::class.constructors.first().parameters | |
F::class.memberProperties.forEach { prop: KProperty1<out F, Any?> -> | |
if (prop.visibility == KVisibility.PUBLIC) { | |
val kParam: KParameter = params.first { it.name == prop.name } | |
val value: Any? = prop.getter.call(from) | |
args[kParam] = convertType.invoke(kParam.name!!, value) | |
} | |
} | |
return T::class.constructors.first().callBy(args) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage