From 04c46db4de8ae58d58e672405e51a223549f8c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Poniedzia=C5=82ek?= Date: Wed, 29 Nov 2023 16:26:46 +0100 Subject: [PATCH] Add `_schema_version` field to each context during `toShreddedJson` transformation (close #132) --- .../SnowplowEvent.scala | 15 +++++++++------ .../EventSpec.scala | 11 +++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/SnowplowEvent.scala b/src/main/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/SnowplowEvent.scala index 84435d5..db2c4e3 100644 --- a/src/main/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/SnowplowEvent.scala +++ b/src/main/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/SnowplowEvent.scala @@ -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 diff --git a/src/test/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/EventSpec.scala b/src/test/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/EventSpec.scala index 1f6674f..c3b24c9 100644 --- a/src/test/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/EventSpec.scala +++ b/src/test/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/EventSpec.scala @@ -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", @@ -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, @@ -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", @@ -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", @@ -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 } ], @@ -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", @@ -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), @@ -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 @@ -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),