vulcan icon indicating copy to clipboard operation
vulcan copied to clipboard

Codec modifications

Open eugkhp opened this issue 2 years ago • 0 comments

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
    }

eugkhp avatar Nov 06 '23 21:11 eugkhp