Skip to content

Codec modifications #556

Open
Open
@eugkhp

Description

@eugkhp

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
    }

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