https://kotlinlang.org logo
#getting-started
Title
# getting-started
n

Nicolai

03/29/2022, 6:41 AM
Hi all 👋 Hope one of you can help me 🙂. I get a list of 'Action's and the actionType inside to define which type of object it should be. For example a FuxiNotification. They are very similar but FuxiNotification will not have all the same parameters but many of them. Likewise any additonal actionTypes would have some but not all.
Copy code
data class Action(
    val actionGuid: String,
    val actionType: String,
    val allowClose: Boolean,
    val body: String,
    val deepLink: String,
    val deepLinkId: String,
    val deepLinkSubId: String,
    val deepLinkText: String,
    val fuxiActionVersion: Int,
    val imageUrl: String,
    val isAutoClose: Boolean,
    val isDeleteFromCache: Boolean,
    val isHidden: Boolean,
    val isShowClose: Boolean,
    val pageId: String,
    val questions: List<Any>,
    val submitText: String,
    val submitUrl: String,
    val surveyId: String,
    val theme: String,
    val title: String,
    val validFromUtc: String,
    val validToUtc: String
)
and another data class
Copy code
@kotlinx.parcelize.Parcelize
data class FuxiNotification(
    val actionGuid: String,
    val actionType: String,
    val allowClose: Boolean,
    val body: String,
    val deepLink: String,
    val deepLinkId: String,
    val deepLinkSubId: String,
    val deepLinkText: String,
    val fuxiActionVersion: Int,
    val imageUrl: String,
    val isAutoClose: Boolean,
    val isDeleteFromCache: Boolean,
    val isHidden: Boolean,
    val isShowClose: Boolean,
    val pageId: String,
    val theme: String,
    val title: String,
    val validFromUtc: String,
    val validToUtc: String
) : Parcelable
What would be the way to do this in a smart way so I can map Action to FuxiNotification easily? 🙏
☠️ 1
d

Dorian

03/29/2022, 7:38 AM
@kotlinx.parcelize.Parcelize data class FuxiNotification( val actionGuid: String, val actionType: String, val allowClose: Boolean, val body: String, val deepLink: String, val deepLinkId: String, val deepLinkSubId: String, val deepLinkText: String, val fuxiActionVersion: Int, val imageUrl: String, val isAutoClose: Boolean, val isDeleteFromCache: Boolean, val isHidden: Boolean, val isShowClose: Boolean, val pageId: String, val theme: String, val title: String, val validFromUtc: String, val validToUtc: String ) : Parcelable { companion object { fun from(action: Action): FuxiNotification { if (action.deepLinkId.isNullOrEmpty()) throw CustomException() return FuxiNotification.apply { actionGuid = action.actionGuid ... } } } } FuxiNotification.from(action) To protect FuxiNotification domain, I think you need to map one by one. Non-declarative mapping is not that good way.
m

Michael de Kaste

03/29/2022, 8:18 AM
although not advised, like @Dorian mentioned. You can have a delegation by map and pass that around. However, you can't have it be a data class that way. Moreover, you can get exceptions if not handling correctly.
Copy code
class Action(val map: Map<String, Any?>){
    val actionGuid: String by map
    ...
    val validToUtc: String by map
}

class FuxiNotification(val map: Map<String, Any?>){
    val actionGuid: String by map
    ...
    val validToUtc: String by map
}

fun main(){
    val action = Action(mapOf("actionGuid" to "a value"))
    val fuxi = FuxiNotification(action.map)

    assertSame(action.actionGuid, fuxi.actionGuid)
    //exception
    println(action.body)
}
n

Nicolai

03/29/2022, 12:22 PM
Thank you very much 🙏 .
2 Views