Skip to content

Commit

Permalink
Fix empty contexts and unstruct_event decoding (closes #92)
Browse files Browse the repository at this point in the history
  • Loading branch information
dilyand committed Nov 4, 2019
1 parent 5eae873 commit e71129f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ object SnowplowEvent {
}
}

implicit final val unstructCirceEncoder: Encoder[UnstructEvent] =
Encoder.instance { unstructEvent: UnstructEvent =>
if (unstructEvent.data.isEmpty) Json.Null
else JsonObject(
("schema", Common.UnstructEventUri.toSchemaUri.asJson),
("data", unstructEvent.data.asJson)
).asJson
}

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

/**
* A JSON representation of an atomic event's contexts or derived_contexts fields.
*
Expand All @@ -67,20 +80,7 @@ object SnowplowEvent {
}

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

implicit final val unstructCirceEncoder: Encoder[UnstructEvent] =
Encoder.instance { unstructEvent: UnstructEvent =>
if (unstructEvent.data.isEmpty) Json.Null
else JsonObject(
("schema", Common.UnstructEventUri.toSchemaUri.asJson),
("data", unstructEvent.data.asJson)
).asJson
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3047,9 +3047,6 @@ class EventSpec extends Specification {
val tempJsonStr = tempInstance.asJson.noSpaces
val tempJson = parse(tempJsonStr).getOrElse(throw new RuntimeException("Error while converting to json"))
tempJson.as[Temp].map(_.event) must beRight(event)
// val eventJsonStr = event.toJson(false).noSpaces
// val eventJson = parse(eventJsonStr).getOrElse(throw new RuntimeException("Error while converting to json"))
// eventJson.as[Event] must beRight(event)
}
}

Expand Down

0 comments on commit e71129f

Please sign in to comment.