Skip to content

Commit

Permalink
Add _schema_version field to each context during toShreddedJson t…
Browse files Browse the repository at this point in the history
…ransformation (close #132)
  • Loading branch information
pondzix committed Nov 29, 2023
1 parent b464b4e commit 04c46db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ object SnowplowEvent {
case class Contexts(data: List[SelfDescribingData[Json]]) extends AnyVal {
def toShreddedJson: Map[String, Json] =
data.groupBy(x => (x.schema.vendor, x.schema.name, x.schema.format, x.schema.version.model)).map {
case ((vendor, name, _, model), d) =>
(transformSchema(Data.Contexts(Data.CustomContexts), vendor, name, model),
d.map { selfdesc =>
selfdesc.data
}.asJson
)
case ((vendor, name, _, model), contextsSdd) =>
val transformedName = transformSchema(Data.Contexts(Data.CustomContexts), vendor, name, model)
val transformedData = contextsSdd.map(addSchemaVersionToData).asJson
(transformedName, transformedData)
}
}

private def addSchemaVersionToData(contextSdd: SelfDescribingData[Json]): Json = {
val version = Json.obj("_schema_version" -> contextSdd.schema.toSchemaUri.asJson)
contextSdd.data.deepMerge(version)
}

implicit final val contextsCirceEncoder: Encoder[Contexts] =
Encoder.instance { contexts =>
if (contexts.data.isEmpty) JsonObject.empty.asJson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class EventSpec extends Specification with ScalaCheck {
"mkt_content" : null,
"mkt_campaign" : null,
"contexts_org_schema_web_page_1" : [ {
"_schema_version" : "iglu:org.schema/WebPage/jsonschema/1-0-0",
"genre" : "blog",
"inLanguage" : "en-US",
"datePublished" : "2014-11-06T00:00:00Z",
Expand All @@ -232,6 +233,7 @@ class EventSpec extends Specification with ScalaCheck {
"keywords" : [ "snowplow", "javascript", "tracker", "event" ]
} ],
"contexts_org_w3_performance_timing_1" : [ {
"_schema_version" : "iglu:org.w3/PerformanceTiming/jsonschema/1-0-0",
"navigationStart" : 1415358089861,
"unloadEventStart" : 1415358090270,
"unloadEventEnd" : 1415358090287,
Expand Down Expand Up @@ -327,6 +329,7 @@ class EventSpec extends Specification with ScalaCheck {
"refr_domain_userid" : null,
"refr_dvce_tstamp" : null,
"contexts_com_snowplowanalytics_snowplow_ua_parser_context_1": [{
"_schema_version" : "iglu:com.snowplowanalytics.snowplow/ua_parser_context/jsonschema/1-0-0",
"useragentFamily": "IE",
"useragentMajor": "7",
"useragentMinor": "0",
Expand Down Expand Up @@ -663,6 +666,7 @@ class EventSpec extends Specification with ScalaCheck {
"mkt_content" : null,
"mkt_campaign" : null,
"contexts_org_schema_web_page_1" : [ {
"_schema_version" : "iglu:org.schema/WebPage/jsonschema/1-0-0",
"genre" : "blog",
"inLanguage" : "en-US",
"datePublished" : "2014-11-06T00:00:00Z",
Expand All @@ -672,9 +676,11 @@ class EventSpec extends Specification with ScalaCheck {
} ],
"contexts_org_acme_context_one_1" : [
{
"_schema_version" : "iglu:org.acme/context_one/jsonschema/1-0-0",
"item" : 1
},
{
"_schema_version" : "iglu:org.acme/context_one/jsonschema/1-0-1",
"item" : 2
}
],
Expand Down Expand Up @@ -752,6 +758,7 @@ class EventSpec extends Specification with ScalaCheck {
"refr_domain_userid" : null,
"refr_dvce_tstamp" : null,
"contexts_com_snowplowanalytics_snowplow_ua_parser_context_1": [{
"_schema_version" : "iglu:com.snowplowanalytics.snowplow/ua_parser_context/jsonschema/1-0-0",
"useragentFamily": "IE",
"useragentMajor": "7",
"useragentMinor": "0",
Expand Down Expand Up @@ -794,6 +801,7 @@ class EventSpec extends Specification with ScalaCheck {
"contexts_org_schema_web_page_1" ->
List(
JsonObject(
("_schema_version", "iglu:org.schema/WebPage/jsonschema/1-0-0".asJson),
("genre", "blog".asJson),
("inLanguage", "en-US".asJson),
("datePublished", "2014-11-06T00:00:00Z".asJson),
Expand All @@ -805,9 +813,11 @@ class EventSpec extends Specification with ScalaCheck {
"contexts_org_acme_context_one_1" ->
List(
JsonObject(
("_schema_version", "iglu:org.acme/context_one/jsonschema/1-0-0".asJson),
("item", 1.asJson)
).asJson,
JsonObject(
("_schema_version", "iglu:org.acme/context_one/jsonschema/1-0-1".asJson),
("item", 2.asJson)
).asJson
).asJson
Expand All @@ -816,6 +826,7 @@ class EventSpec extends Specification with ScalaCheck {
"contexts_com_snowplowanalytics_snowplow_ua_parser_context_1" ->
List(
JsonObject(
("_schema_version", "iglu:com.snowplowanalytics.snowplow/ua_parser_context/jsonschema/1-0-0".asJson),
("useragentFamily", "IE".asJson),
("useragentMajor", "7".asJson),
("useragentMinor", "0".asJson),
Expand Down

0 comments on commit 04c46db

Please sign in to comment.