Skip to content

Commit

Permalink
Add support for forcing property type for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
DeKaN committed Oct 25, 2023
1 parent 756b0b1 commit 7007742
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
android {
defaultConfig {
minSdk = 21
compileSdk = 33
compileSdk = 34
targetSdk = 34
applicationId = "com.example.piano_analytics_android"
versionCode = 1
Expand Down
6 changes: 2 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ moshiIR = "0.22.1"

# AndroidX libraries
compatLibrary = "1.6.1"
annotationsLibrary = "1.6.0"
lifecycle = "2.6.2"
materialLibrary = "1.9.0"
materialLibrary = "1.10.0"

# Third party Libraries
googleAdsId = "18.0.1"
Expand All @@ -27,7 +26,7 @@ viewBindingProperty = "1.5.9"
junit = "4.13.2"
androidxTestCore = "1.5.0"
mockitoKotlin = "2.2.0"
mockitoCore = "5.5.0"
mockitoCore = "5.6.0"

[plugins]
android-library = { id = "com.android.library", version.ref = "android" }
Expand All @@ -40,7 +39,6 @@ moshiIR = { id = "dev.zacsweers.moshix", version.ref = "moshiIR"}
versionUpdater = { id = "com.github.ben-manes.versions", version.ref = "versionUpdater" }

[libraries]
annotations = { module = "androidx.annotation:annotation", version.ref = "annotationsLibrary" }
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "compatLibrary" }
lifecycleProcess = { module = "androidx.lifecycle:lifecycle-process", version.ref = "lifecycle"}
material = { module = "com.google.android.material:material", version.ref = "materialLibrary" }
Expand Down
2 changes: 1 addition & 1 deletion piano-analytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ version = VERSION_NAME
android {
defaultConfig {
minSdk = 21
compileSdk = 33
compileSdk = 34
buildConfigField("String", "SDK_VERSION", """"${project.version}"""")

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ internal class EventPropertiesJsonAdapter(
is Double,
is Boolean,
is Array<*>,
-> Property(PropertyName(entry.key), entry.value)
-> {
val delimiterIndex = entry.key.lastIndexOf(DELIMITER)
val key = entry.key.substring(delimiterIndex + 1)
val type = if (delimiterIndex != -1) {
val prefix = entry.key.substring(0, delimiterIndex)
Property.Type.values().firstOrNull { it.prefix == prefix }
} else {
null
}
Property(PropertyName(key), entry.value, type)
}

else -> null
}
Expand All @@ -29,6 +39,15 @@ internal class EventPropertiesJsonAdapter(

override fun toJson(writer: JsonWriter, value: Set<Property>?) {
requireNotNull(value)
mapAdapter.toJson(writer, value.associate { it.name.key.lowercase() to it.value })
mapAdapter.toJson(
writer,
value.associate {
it.forceType?.prefix?.plus(DELIMITER).orEmpty() + it.name.key.lowercase() to it.value
}
)
}

companion object {
private const val DELIMITER = ":"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.util.Date
class Property {
val name: PropertyName
val value: Any
val forceType: Type?

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -17,10 +18,11 @@ class Property {

override fun hashCode(): Int = name.key.lowercase().hashCode()

internal constructor(name: PropertyName, value: Any) {
internal constructor(name: PropertyName, value: Any, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

/**
Expand All @@ -29,10 +31,11 @@ class Property {
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: String) {
constructor(name: PropertyName, value: String, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

/**
Expand All @@ -41,10 +44,11 @@ class Property {
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: Int) {
constructor(name: PropertyName, value: Int, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

/**
Expand All @@ -53,10 +57,11 @@ class Property {
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: Long) {
constructor(name: PropertyName, value: Long, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

/**
Expand All @@ -65,10 +70,11 @@ class Property {
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: Double) {
constructor(name: PropertyName, value: Double, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

/**
Expand All @@ -77,18 +83,19 @@ class Property {
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: Date) : this(name, value.time / 1000)
constructor(name: PropertyName, value: Date) : this(name, value.time / 1000, Type.DATE)

/**
* Creates a new property
*
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: Boolean) {
constructor(name: PropertyName, value: Boolean, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

/**
Expand All @@ -97,10 +104,11 @@ class Property {
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: Array<String>) {
constructor(name: PropertyName, value: Array<String>, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

/**
Expand All @@ -109,10 +117,11 @@ class Property {
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: Array<Int>) {
constructor(name: PropertyName, value: Array<Int>, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

/**
Expand All @@ -121,9 +130,21 @@ class Property {
* @param name property name
* @param value property value
*/
constructor(name: PropertyName, value: Array<Double>) {
constructor(name: PropertyName, value: Array<Double>, forceType: Type? = null) {
require(name != PropertyName.ANY_PROPERTY)
this.name = name
this.value = value
this.forceType = forceType
}

enum class Type(val prefix: String) {
STRING("s"),
INTEGER("n"),
FLOAT("f"),
DATE("d"),
BOOLEAN("b"),
STRING_ARRAY("a:s"),
INTEGER_ARRAY("a:n"),
FLOAT_ARRAY("a:f"),
}
}

0 comments on commit 7007742

Please sign in to comment.