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

question, define union of types #82

Open
nicolaemarasoiu opened this issue Aug 6, 2020 · 1 comment
Open

question, define union of types #82

nicolaemarasoiu opened this issue Aug 6, 2020 · 1 comment

Comments

@nicolaemarasoiu
Copy link

nicolaemarasoiu commented Aug 6, 2020

Hi, we have a Kafka topic with values of Avro type union of two Avro types (any message is of one or another type). When we have union types for fields, we know how to work with them in the Avro Hugger context, but when the union is top level, do you know a way to define an ad hoc type that is simply the union of the two explicit types? Thank you

@nicolaemarasoiu
Copy link
Author

nicolaemarasoiu commented Aug 6, 2020

in an attempt to define a Serde for an Either type i realize that Avro spec must mention how to serde unions, do you know more details? thank you
(i would imagine a header which indicates which particular type is instance by instance, so that both Serializer and Deserializer must first serde this type discriminator and then that type which is valid for that instance), so serializer below is not correct for sure even if it compiles)

implicit val consumed: Consumed[String, Either[TariffUpserted, TariffCancelled]] =
      Consumed.`with`(
        keySerde = Serdes.String,
        valueSerde = new Serde[Either[TariffUpserted, TariffCancelled]] {
          override def serializer(): Serializer[Either[TariffUpserted, TariffCancelled]] =
            (topic: String, data: Either[TariffUpserted, TariffCancelled]) => {
              data match {
                case Left(tariffUpserted) =>
                  implicitly[Serde[TariffUpserted]].serializer().serialize(topic, tariffUpserted)
                case Right(contractCancelled) =>
                  implicitly[Serde[TariffCancelled]]
                    .serializer()
                    .serialize(topic, contractCancelled)
              }
            }

          override def deserializer(): Deserializer[Either[TariffUpserted, TariffCancelled]] = {
            def deserializer(): Deserializer[Either[TariffUpserted, TariffCancelled]] =
              new Deserializer[Either[TariffUpserted, TariffCancelled]] {
                override def deserialize(
                    topic: String,
                    data: Array[Byte]
                ): Either[TariffUpserted, TariffCancelled] = {

                }
              }
          }
        }
      )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant