Skip to content

Commit

Permalink
feat: add plan versionId support (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Mar 31, 2022
1 parent 8c46250 commit ae84619
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
50 changes: 48 additions & 2 deletions core/src/main/java/com/amplitude/core/events/Plan.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
package com.amplitude.core.events

import com.amplitude.common.jvm.ConsoleLogger
import org.json.JSONException
import org.json.JSONObject

data class Plan(
val branch: String? = null,
val source: String? = null,
val version: String? = null
)
val version: String? = null,
val versionId: String? = null,
) {

/**
* Get JSONObject of current tacking plan
* @return JSONObject including plan information
*/
fun toJSONObject(): JSONObject? {
val plan = JSONObject()
try {
if (!branch.isNullOrEmpty()) {
plan.put(AMP_PLAN_BRANCH, branch)
}
if (!source.isNullOrEmpty()) {
plan.put(AMP_PLAN_SOURCE, source)
}
if (!version.isNullOrEmpty()) {
plan.put(AMP_PLAN_VERSION, version)
}
if (!versionId.isNullOrEmpty()) {
plan.put(AMP_PLAN_VERSION_ID, versionId)
}
} catch (e: JSONException) {
ConsoleLogger.logger.error("JSON Serialization of tacking plan object failed")
}
return plan
}

companion object {
const val AMP_PLAN_BRANCH = "branch"
const val AMP_PLAN_SOURCE = "source"
const val AMP_PLAN_VERSION = "version"
const val AMP_PLAN_VERSION_ID = "versionId"

fun fromJSONObject(jsonObject: JSONObject): Plan {
val branch = jsonObject.optString(AMP_PLAN_BRANCH, null)
val source = jsonObject.optString(AMP_PLAN_SOURCE, null)
val version = jsonObject.optString(AMP_PLAN_VERSION, null)
val versionId = jsonObject.optString(AMP_PLAN_VERSION_ID, null)
return Plan(branch, source, version, versionId)
}
}
}
5 changes: 5 additions & 0 deletions core/src/main/java/com/amplitude/core/utilities/JSONUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.amplitude.core.utilities

import com.amplitude.core.Constants
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.events.Plan
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -47,6 +48,9 @@ object JSONUtil {
eventJSON.addValue("insert_id", event.insertId)
eventJSON.addValue("library", event.library)
eventJSON.addValue("partner_id", event.partnerId)
event.plan?. let {
eventJSON.put("plan", it.toJSONObject())
}
return eventJSON
}

Expand Down Expand Up @@ -189,6 +193,7 @@ internal fun JSONObject.toBaseEvent(): BaseEvent {
event.insertId = this.optString("insert_id", null)
event.library = if (this.has("library")) this.getString("library") else null
event.partnerId = this.optString("partner_id", null)
event.plan = if (this.has("plan")) Plan.fromJSONObject(this.getJSONObject("plan")) else null
return event
}

Expand Down

0 comments on commit ae84619

Please sign in to comment.