ktoml icon indicating copy to clipboard operation
ktoml copied to clipboard

A good idea is to use annotation processing with reflekt for the configuration

Open orchestr7 opened this issue 4 years ago • 3 comments

https://github.com/JetBrains-Research/reflekt

orchestr7 avatar Nov 06 '21 19:11 orchestr7

I think @SerialInfo from kotlinx.serialization could make it, like this:

@SerialInfo
@Target(AnnotationTarget.PROPERTY)
public annotation class Comment(vararg val texts: String)

// Model class
data class Account(
    val username: String,
    @Comment("Don't tell others!")
    val password: String
)

// In Encoder
fun comment(descriptor: SerialDescriptor, index: Int) {
    descriptor.getElementAnnotations(index)
        .filterIsInstance<Comment>()
        .map {
            if (it.isEmpty())
                emptyArray<String>()
            else
                it[0].texts // As Comment is not @Repeatable, we take the first
        }
        .forEach(writer::emitComment) // functional YES!
}

Peanuuutz avatar Jan 14 '22 13:01 Peanuuutz

Yes, I think this is covered by kotlinx.serialization already. I'm not sure what functionality Reflekt would provide that we don't have access to, but I could be missing something

NightEule5 avatar Jan 15 '22 00:01 NightEule5

Yeah, it looks so. I actually wanted to add some custom annotations for compile time reflection, but yes Kotlinx.serialization already has mostly everything we need. May be we will use reflekt for something else next time :)

orchestr7 avatar Jan 15 '22 16:01 orchestr7