ShikaSD / compiler-plugin-talk-example

Example of implementing part of serialization compiler plugin as an example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example of how compiler plugin works by implementating a fraction of kotlinx.serialization plugin.

Given:

@Serializable
class Data(val i: String)

Generates:

@Serializable
class Data(val i: String) {
    companion object {
        fun serializer(): KSerializer<Data> = `$serializer`
    }
       
    private object `$serializer` : KSerializer<Data> {
        private val delegate = StringSerializer()
        override val descriptor = delegate.descriptor
        override fun serialize(encoder: Encoder, data: Data) =
            delegate.serialize(encoder, data.toString())
        override fun deserialize(decoder: Decoder) = 
            TODO()
    }
}

Where to look?

compiler-plugin folder - the plugin itself

integration folder - playground

Making new classes available for user

See SerializationResolveExtension in compiler-plugin folder

Handling errors

See SerializationErrors and SerializationDeclarationChecker in compiler-plugin folder

Generating IR

See SerializationLoweringExtension and SerializationLoweringClassGeneration in compiler-plugin folder

About

Example of implementing part of serialization compiler plugin as an example


Languages

Language:Kotlin 100.0%