Hey everyone! I have a endpoint that returns repli...
# android
d
Hey everyone! I have a endpoint that returns replies of a post, and the structure is something like that:
Copy code
data class PostReply(
  val author: String,
  val body: String,
  val replies: List<PostReply> // can be infinitely deep
)
I’ve created one regular data class, and another with serialized annotations. But I cannot think a good way on how to map
replies
recursively. I tried that but didn’t work:
Copy code
fun PostRepliesResponse.toModel() = PostReplies(
    //...
    replies = replies.map { reply -> reply.toModel() }
)
Can you guys help me?
j
Did it error out or did it just end up with an empty list of replies? Also can you post signature of the
PostReplies
and
PostRepliesResponse
classes?
d
Captura de Tela 2022-11-27 às 18.02.18.png
The classes are equal, but PostRepliesResponse has
@SerializedName("")
on fields
j
What does the
.toModel()
look like on the
PostReply
class?
d
Does not
.toModel()
is a extension to map from
PostRepliesResponse
to
PostReplies
And if I manage this to work, I’m gonna create another like
PostReplies.toResponse()
j
Well the sections of
replies.map { reply -> reply.toModel() }
is trying to call a
toModel
as well, but it's called on a separate type, what is the types of the
replies
list here? It's hard for me to tell without more context, but it seems like we only defined a single
toModel
extension on the top level model but I'm not seeing where we defined a
toModel
extension on the inner reply types
d
Thats the problem I’m trying to solve! The
replies
field is a list of
PostReplies
. Each reply have a replies field of the same time
What I’m trying to do is call
.toModel()
recursively
j
Ah okay, so off the top of my head I'm thinking
Copy code
fun PostReplies.toModel(): List<PostReplies> {
  if (replies.isNotEmpty()) {
    replies.map { reply -> reply.toModel() }
  } else {
    emptyList<PostReplies>()
  }
}
Because we essentially just want it to keep building out that base list recursively and stop at the end if we find no more
The more I look at my solution the more I'm doubting it though 😅
d
I think i got it
I created another extension
Copy code
fun PostRepliesResponse.toModel() = PostReplies(
    id = id,
    ownerUsername = ownerUsername,
    body = body,
    tabcoins = tabcoins,
    repliesAmount = repliesAmount,
    replies = replies.toModel()
)

fun List<PostRepliesResponse>.toModel(): List<PostReplies> {
    return this.map {
        PostReplies(
            id = it.id,
            ownerUsername = it.ownerUsername,
            body = it.body,
            tabcoins = it.tabcoins,
            repliesAmount = it.repliesAmount,
            replies = it.replies.toModel()
        )
    }
}
feels weird, but I think is gonna work 😅
j
ah yes that looks much better, that actually includes the model conversion piece
May need to be careful of any stackoverflows in the future with that recursive model though 😅
d
What do you mean? 😂
@Josh Eldridge | yup! Some how worked 🫠
j
Sweet!