47degrees / helios

A purely functional JSON library for Kotlin built on Λrrow
https://47degrees.github.io/helios/
Apache License 2.0
169 stars 22 forks source link

Sealed class support #86

Open Octogonapus opened 5 years ago

Octogonapus commented 5 years ago

The annotation processor generates invalid code for sealed classes.

This code:

@json
sealed class LinkType {
    object Rotary : LinkType()
    object Prismatic : LinkType()

    companion object
}

Generates this:

fun LinkType.toJson(): Json = JsObject(mapOf())

fun Json.Companion.toLinkType(value: Json): Either<DecodingError, LinkType> =
  Either.applicative<DecodingError>().map(

,  {  ->
  LinkType()
}).fix()

fun LinkType.Companion.encoder() = object : Encoder<LinkType> {
  override fun LinkType.encode(): Json = this.toJson()
}

fun LinkType.Companion.decoder() = object : Decoder<LinkType> {
  override fun decode(value: Json): Either<DecodingError, LinkType> =
    Json.toLinkType(value)
}

Apart from the issue with the call to map(), it's not possible to construct a LinkType because it is a sealed class.