Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support optional sealed trait #502

Open
soujiro32167 opened this issue Dec 30, 2022 · 0 comments
Open

Support optional sealed trait #502

soujiro32167 opened this issue Dec 30, 2022 · 0 comments

Comments

@soujiro32167
Copy link
Contributor

soujiro32167 commented Dec 30, 2022

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"
  } ]
} ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant