-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ingestion_metadata field (#63)
* feat: add ingestion_metadata field * feat: clone object from configure when setting event
- Loading branch information
1 parent
f7b50d1
commit 354ec7b
Showing
14 changed files
with
159 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
core/src/main/java/com/amplitude/core/events/IngestionMetadata.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.amplitude.core.events | ||
|
||
import com.amplitude.common.jvm.ConsoleLogger | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
|
||
open class IngestionMetadata @JvmOverloads constructor( | ||
val sourceName: String? = null, | ||
val sourceVersion: String? = null, | ||
) { | ||
|
||
/** | ||
* Get JSONObject of current ingestion metadata | ||
* @return JSONObject including ingestion metadata information | ||
*/ | ||
internal fun toJSONObject(): JSONObject { | ||
val jsonObject = JSONObject() | ||
try { | ||
if (!sourceName.isNullOrEmpty()) { | ||
jsonObject.put(AMP_INGESTION_METADATA_SOURCE_NAME, sourceName) | ||
} | ||
if (!sourceVersion.isNullOrEmpty()) { | ||
jsonObject.put(AMP_INGESTION_METADATA_SOURCE_VERSION, sourceVersion) | ||
} | ||
} catch (e: JSONException) { | ||
ConsoleLogger.logger.error("JSON Serialization of ingestion metadata object failed") | ||
} | ||
return jsonObject | ||
} | ||
|
||
/** | ||
* Get a cloned IngestionMetadata object, to isolate the potentially value changes | ||
*/ | ||
fun clone(): IngestionMetadata { | ||
return IngestionMetadata(sourceName, sourceVersion) | ||
} | ||
|
||
companion object { | ||
const val AMP_INGESTION_METADATA_SOURCE_NAME = "source_name" | ||
const val AMP_INGESTION_METADATA_SOURCE_VERSION = "source_version" | ||
|
||
internal fun fromJSONObject(jsonObject: JSONObject): IngestionMetadata { | ||
val branch = jsonObject.optString(AMP_INGESTION_METADATA_SOURCE_NAME, null) | ||
val source = jsonObject.optString(AMP_INGESTION_METADATA_SOURCE_VERSION, null) | ||
return IngestionMetadata(branch, source) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
core/src/test/kotlin/com/amplitude/core/events/IngestionMetadataTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.amplitude.core.events | ||
|
||
import org.json.JSONObject | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class IngestionMetadataTest { | ||
|
||
@Test | ||
fun `test ingestionMetadata to json object`() { | ||
val sourceName = "ampli" | ||
val sourceVersion = "2.0.0" | ||
val ingestionMetadata = IngestionMetadata(sourceName, sourceVersion) | ||
val result = ingestionMetadata.toJSONObject() | ||
assertEquals(sourceName, result.getString(IngestionMetadata.AMP_INGESTION_METADATA_SOURCE_NAME)) | ||
assertEquals(sourceVersion, result.getString(IngestionMetadata.AMP_INGESTION_METADATA_SOURCE_VERSION)) | ||
} | ||
|
||
@Test | ||
fun `test ingestionMetadata clone new object`() { | ||
val sourceName = "ampli" | ||
val sourceVersion = "2.0.0" | ||
val ingestionMetadata = IngestionMetadata(sourceName, sourceVersion) | ||
val clone = ingestionMetadata.clone() | ||
assertEquals(sourceName, clone.sourceName) | ||
assertEquals(sourceVersion, clone.sourceVersion) | ||
assertFalse(ingestionMetadata === clone) | ||
} | ||
|
||
@Test | ||
fun `test ingestionMetadata from json object`() { | ||
val jsonObject = JSONObject() | ||
val sourceName = "ampli" | ||
val sourceVersion = "2.0.0" | ||
jsonObject.put(IngestionMetadata.AMP_INGESTION_METADATA_SOURCE_NAME, sourceName) | ||
.put(IngestionMetadata.AMP_INGESTION_METADATA_SOURCE_VERSION, sourceVersion) | ||
val ingestionMetadata = IngestionMetadata.fromJSONObject(jsonObject) | ||
assertEquals(sourceName, ingestionMetadata.sourceName) | ||
assertEquals(sourceVersion, ingestionMetadata.sourceVersion) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters