I have a plenty of DTO `data class`es from our bac...
# javascript
p
I have a plenty of DTO `data class`es from our backend team (so Ican't change them). I need to serialize some of them to GraphQL, so I'm writing custom serializers:
Copy code
val MessageTemplateTextDtoCreateInput.gql
    get() = buildString {
        append('{')
        appendKV("body", body)
        appendKV("languageId", languageId)
        appendKV("subject", subject, true)
        append('}')
    }

fun StringBuilder.appendKV(key: CharSequence, value: Any?, last: Boolean = false) {
    append(key)
    append(':')
    if (value is String) {
        append('"')
    }
    append(
        when (value) {
            is Array<*> -> value.joinToString(",", "[", "]") {
                it.asDynamic().gql as? CharSequence ?: "" // !!! PROBLEM HERE !!!
            }
            else -> value
        }
    )
    if (value is String) {
        append('"')
    }
    if (!last) {
        append(',')
    }
}
The problem is, how can I dynamicaly get the
.gql
extension value I created? • I understand, that Kotlin can't know from
Array<*>
that the members has the
.gql
extension. • I know I can't use rflection in JS to look if the extension is there. • Sealed classes/interfaces might be the solutionm, but I can't change the DTOs. •
asDynamic().gql
does not work (this is a bit WTF to me, I thought this might work and at worst throw an exception or something.) So, any idea how to do this?
t
Copy code
it.unsafeCast<MessageTemplateTextDtoCreateInput>().gql
If
MessageTemplateTextDtoCreateInput
is class:
Copy code
(it as MessageTemplateTextDtoCreateInput).gcl
p
The rpoblem is, there wil be many more classes as
MessageTemplateTextDtoCreateInput
. So, the simpliest solution I come up with is simple
when
and
is
checks for each class with
.gql
extension.