Skip to content

Support optional sealed trait #502

Open
@soujiro32167

Description

@soujiro32167

The following codec results in an error:

import vulcan.Codec
import vulcan.generic.*

sealed trait S
case class A(i: Int) extends S

val c1: Codec[S] = Codec.derive

Codec.option(c1).schema // AvroError(org.apache.avro.AvroRuntimeException: Nested union: ["null",[{"type":"record","name":"A","namespace":"avrounion","fields":[{"name":"i","type":"int"}]}]])

However, what I would like is

implicit val optionalUnion: Codec[Option[S]] = {
    import cats.implicits.catsSyntaxSemigroup
    implicit val ca: Codec[A] = Codec.derive

    Codec.union[Option[S]](alt =>
      alt[None.type] |+| alt[Some[A]]
    )
}

optionalUnion.schema 
/*
[ "null", {
  "type" : "record",
  "name" : "A",
  "namespace" : "avrounion",
  "fields" : [ {
    "name" : "i",
    "type" : "int"
  } ]
} ]
*/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions