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

Codec modifications #556

Open
eugkhp opened this issue Nov 6, 2023 · 0 comments
Open

Codec modifications #556

eugkhp opened this issue Nov 6, 2023 · 0 comments

Comments

@eugkhp
Copy link

eugkhp commented Nov 6, 2023

Hey!

It it possible to modify/extend the codec in some way? I would love for it to work the similar way to circe, but I can't find the related method

I want to build a codec for my type based on another codec, but it only allows to transform a codec.

Here is the problem I'm trying to solve

  final case class Event[A: Codec](
      eventId: EventId,
      eventTimestamp: Instant,
      data: A,
    )

  object Event {
    implicit def codec[A: Codec]: Codec[Event[A]] = ??? 
  }   

And I want the Codec[Event[A]] to just add two fields(eventId, eventTimestamp) to the existing codec.

The behavior is really similar to circe's deepMerge. Is it possbile to do with vulcan (avro)?

  def deepMerge(that: Json): Json =
    (asObject, that.asObject) match {
      case (Some(lhs), Some(rhs)) =>
        fromJsonObject(
          lhs.toIterable.foldLeft(rhs) {
            case (acc, (key, value)) =>
              rhs(key).fold(acc.add(key, value)) { r =>
                acc.add(key, value.deepMerge(r))
              }
          }
        )
      case _ => that
    }
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