From f10ca5a8108058470ce9d0ab816f3f7c90bc22ad Mon Sep 17 00:00:00 2001 From: Chanh Nguyen Date: Mon, 22 Nov 2021 17:17:27 +0200 Subject: [PATCH] Extend metadata model for published_by (#193) * extend metadata model for published_by Signed-off-by: Chanh Nguyen --- src/main/scala/org/zalando/kanadi/api/Events.scala | 13 ++++++++----- .../org/zalando/kanadi/models/PublishedBy.scala | 13 +++++++++++++ .../scala/org/zalando/kanadi/api/JsonSpec.scala | 8 +++++--- 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 src/main/scala/org/zalando/kanadi/models/PublishedBy.scala diff --git a/src/main/scala/org/zalando/kanadi/api/Events.scala b/src/main/scala/org/zalando/kanadi/api/Events.scala index 1080d18..9cbaa48 100644 --- a/src/main/scala/org/zalando/kanadi/api/Events.scala +++ b/src/main/scala/org/zalando/kanadi/api/Events.scala @@ -151,11 +151,12 @@ final case class Metadata(eid: EventId = EventId.random, flowId: Option[FlowId] = None, partition: Option[Partition] = None, partitionCompactionKey: Option[PartitionCompactionKey] = None, - spanCtx: Option[SpanCtx] = None) + spanCtx: Option[SpanCtx] = None, + publishedBy: Option[PublishedBy] = None) object Metadata { - implicit val metadataEncoder: Encoder[Metadata] = Encoder.forProduct9( + implicit val metadataEncoder: Encoder[Metadata] = Encoder.forProduct10( "eid", "occurred_at", "event_type", @@ -164,10 +165,11 @@ object Metadata { "flow_id", "partition", "partition_compaction_key", - "span_ctx" + "span_ctx", + "published_by" )(x => Metadata.unapply(x).get) - implicit val metadataDecoder: Decoder[Metadata] = Decoder.forProduct9( + implicit val metadataDecoder: Decoder[Metadata] = Decoder.forProduct10( "eid", "occurred_at", "event_type", @@ -176,7 +178,8 @@ object Metadata { "flow_id", "partition", "partition_compaction_key", - "span_ctx" + "span_ctx", + "published_by" )(Metadata.apply) } diff --git a/src/main/scala/org/zalando/kanadi/models/PublishedBy.scala b/src/main/scala/org/zalando/kanadi/models/PublishedBy.scala new file mode 100644 index 0000000..45f8c8e --- /dev/null +++ b/src/main/scala/org/zalando/kanadi/models/PublishedBy.scala @@ -0,0 +1,13 @@ +package org.zalando.kanadi.models + +import io.circe.syntax._ +import io.circe.{Decoder, Encoder} + +final case class PublishedBy(name: String) extends AnyVal + +object PublishedBy { + implicit val publishedByEncoder: Encoder[PublishedBy] = + Encoder.instance[PublishedBy](_.name.asJson) + implicit val publishedByDecoder: Decoder[PublishedBy] = + Decoder[String].map(PublishedBy.apply) +} diff --git a/src/test/scala/org/zalando/kanadi/api/JsonSpec.scala b/src/test/scala/org/zalando/kanadi/api/JsonSpec.scala index 86bda87..ea3e3cb 100644 --- a/src/test/scala/org/zalando/kanadi/api/JsonSpec.scala +++ b/src/test/scala/org/zalando/kanadi/api/JsonSpec.scala @@ -10,7 +10,7 @@ import org.specs2.specification.core.SpecStructure import io.circe._ import io.circe.parser._ import io.circe.syntax._ -import org.zalando.kanadi.models.{EventId, SpanCtx} +import org.zalando.kanadi.models.{EventId, SpanCtx, PublishedBy} import java.time.OffsetDateTime import io.circe.CursorOp.DownField @@ -28,7 +28,9 @@ class JsonSpec extends Specification { val uuid = UUID.randomUUID() val testEvent = SomeEvent("Bart", "Simpson", uuid) val now = OffsetDateTime.now() - val md = Metadata(eid = EventId(UUID.fromString("4ae5011e-eb01-11e5-8b4a-1c6f65464fc6")), occurredAt = now) + val md = Metadata(eid = EventId(UUID.fromString("4ae5011e-eb01-11e5-8b4a-1c6f65464fc6")), + occurredAt = now, + publishedBy = Some(PublishedBy("bart_simpson"))) val coreEventJson = s""" "first_name": "Bart", @@ -37,7 +39,7 @@ class JsonSpec extends Specification { """ val metadata = - s""""eid": "4ae5011e-eb01-11e5-8b4a-1c6f65464fc6", "occurred_at": ${now.asJson}""" + s""""eid": "4ae5011e-eb01-11e5-8b4a-1c6f65464fc6", "occurred_at": ${now.asJson}, "published_by": "bart_simpson"""" val businessEventJson = s"""{ "metadata": {$metadata},