Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
Extend metadata model for published_by (#193)
Browse files Browse the repository at this point in the history
* extend metadata model for published_by

Signed-off-by: Chanh Nguyen <chanh.nguyen@zalando.fi>
  • Loading branch information
ChanhNguyen17 authored Nov 22, 2021
1 parent 2eb7e5d commit f10ca5a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/main/scala/org/zalando/kanadi/api/Events.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -176,7 +178,8 @@ object Metadata {
"flow_id",
"partition",
"partition_compaction_key",
"span_ctx"
"span_ctx",
"published_by"
)(Metadata.apply)
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/scala/org/zalando/kanadi/models/PublishedBy.scala
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 5 additions & 3 deletions src/test/scala/org/zalando/kanadi/api/JsonSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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},
Expand Down

0 comments on commit f10ca5a

Please sign in to comment.