在深度嵌套数据类中调用 getter 的更优雅方法

发布于 2025-01-11 05:55:32 字数 734 浏览 1 评论 0原文

我已经反序列化了一个巨大的 XML 文件,这给我留下了很多(嵌套的)数据类。我正在寻找一种更优雅的方法来调用这些对象中的特定 getter。所以我不想用所有这些点调用来填充我的代码。

我必须设置相当多的字段来返回一个新模型,目前看起来像这样(我为这个问题编写了模型):

        return TestClass(
    appointmentDate = message.data.appointment.appointmentDescription.AppointmentDetails.appointmentDate
    appointmentX = message.data.appointment.appointmentDescription.X.Y.Z
    appointmentY = message.data.appointment.appointmentDescription.AppointmentDetails.X.Y.Z
    appointmentZ = message.data.appointment.Z
    )

我知道我可以执行 val AppointmentDescription = message.data.appointment.appointmentDescription 并从那里可以缩短一点,但我认为一定有一个更短更好的方法。

有谁知道如何以更优雅和干净的方式做到这一点?我对 Kotlin 还很陌生,我很难在互联网上正确查找任何解决方案,因为我找不到任何解决方案,并且可能正在寻找错误的东西。

I have deserialized a huge XML file and that leaves me with a lot of (nested) data classes. I'm looking for a more elegant way to do calls to specific getters within these objects. So I would rather not fill my code with all these dot calls.

I have to set quite some fields to return a new Model, which currently looks something like this (I made up the model for this question):

        return TestClass(
    appointmentDate = message.data.appointment.appointmentDescription.AppointmentDetails.appointmentDate
    appointmentX = message.data.appointment.appointmentDescription.X.Y.Z
    appointmentY = message.data.appointment.appointmentDescription.AppointmentDetails.X.Y.Z
    appointmentZ = message.data.appointment.Z
    )

I know I could do val appointmentDescription = message.data.appointment.appointmentDescription and do my calls from there to shorten it a bit, but there must be a shorter and better way I assume.

Does anyone know a way to do this in a more elegant and clean way? I'm pretty new to Kotlin and it is quite hard for me to properly lookup any solutions on the internet since I can't find any and am probably searching for the wrong things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2025-01-18 05:55:32

尝试使用 with 函数:

data class Message (val data: Data)
data class Data (val appointment: Appointment)
data class Appointment(val appointmentDescription: AppointmentDescription)
data class AppointmentDescription(val x: Int, val appointmentDetails: AppointmentDetails)
data class AppointmentDetails(val x:Int, val y:Int)

fun TestClass(message: Message) {
    with(message.data.appointment.appointmentDescription) {
        val appointmentX = x
        val appointmentY = appointmentDetails.y
    }
}

这应该很有用:https://medium.com/mobile-app-development-publication/mastering-kotlin-standard-functions-run-with-let-also-and-apply-9cd334b0ef84

Try the with function:

data class Message (val data: Data)
data class Data (val appointment: Appointment)
data class Appointment(val appointmentDescription: AppointmentDescription)
data class AppointmentDescription(val x: Int, val appointmentDetails: AppointmentDetails)
data class AppointmentDetails(val x:Int, val y:Int)

fun TestClass(message: Message) {
    with(message.data.appointment.appointmentDescription) {
        val appointmentX = x
        val appointmentY = appointmentDetails.y
    }
}

This should be useful: https://medium.com/mobile-app-development-publication/mastering-kotlin-standard-functions-run-with-let-also-and-apply-9cd334b0ef84

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文