Skip to content

Commit

Permalink
Add decoder for Event (closes snowplow#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldemirenes committed Jul 1, 2019
1 parent b9208ed commit 2b67557
Show file tree
Hide file tree
Showing 2 changed files with 750 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ package com.snowplowanalytics.snowplow.analytics.scalasdk

// circe
import io.circe.syntax._
import io.circe.{Encoder, Json, JsonObject}
import io.circe.generic.semiauto.deriveDecoder
import io.circe.{Encoder, Json, JsonObject, Decoder, DecodingFailure}
import io.circe.java8._
import io.circe.CursorOp.DownField
//cats
import cats.implicits._

// iglu
import com.snowplowanalytics.iglu.core.circe.CirceIgluCodecs._
Expand Down Expand Up @@ -61,6 +66,10 @@ object SnowplowEvent {
).asJson
}

implicit val contextsDecoder = deriveDecoder[Contexts].recover {
case DecodingFailure(_, List(DownField("data"), DownField(_))) => Contexts(List())
}

implicit final val unstructCirceEncoder: Encoder[UnstructEvent] =
Encoder.instance { unstructEvent: UnstructEvent =>
if (unstructEvent.data.isEmpty) Json.Null
Expand All @@ -70,6 +79,12 @@ object SnowplowEvent {
).asJson
}

implicit val unstructEventDecoder = deriveDecoder[UnstructEvent].recover {
case DecodingFailure(_, List(DownField("data"), DownField(_))) => UnstructEvent(None)
}

implicit val eventDecoder: Decoder[Event] = deriveDecoder[Event]

/**
* @param shredProperty Type of self-describing entity
* @param vendor Iglu schema vendor
Expand Down
Loading

0 comments on commit 2b67557

Please sign in to comment.