From 8a1451a74338319f8c8e2d21f4913feccf3cc5fc Mon Sep 17 00:00:00 2001 From: Weihao Ding <158090588+weihao-statsig@users.noreply.github.com> Date: Wed, 11 Sep 2024 09:36:03 -0700 Subject: [PATCH] chore: expose user obj constructor with both userid and customID (#360) create a user object with a userid and customids More: https://statsigconnect.slack.com/archives/C07ETLMDKC4/p1725876639552919 --- .../kotlin/com/statsig/sdk/StatsigUser.kt | 113 +++++++----------- .../java/com/statsig/sdk/StatsigUserTest.kt | 9 ++ .../com/statsig/sdk/StatsigUserTestJava.java | 23 ++++ 3 files changed, 74 insertions(+), 71 deletions(-) create mode 100644 src/test/java/com/statsig/sdk/StatsigUserTestJava.java diff --git a/src/main/kotlin/com/statsig/sdk/StatsigUser.kt b/src/main/kotlin/com/statsig/sdk/StatsigUser.kt index d7d7df2..de8d0b9 100644 --- a/src/main/kotlin/com/statsig/sdk/StatsigUser.kt +++ b/src/main/kotlin/com/statsig/sdk/StatsigUser.kt @@ -11,10 +11,10 @@ import java.util.TreeMap * * userID or at least a customID is expected: learn more https://docs.statsig.com/messages/serverRequiredUserID * - * @property userID a unique identifier for the user. - * @property customIDs a map of key-value pairs representing the ID type and value for the user + * @property userID a unique identifier for the user. + * @property customIDs a map of key-value pairs representing the ID type and value for the user * @property email an email associated with the current user - * @property ip the ip address of the requests for the user + * @property ip the IP address of the requests for the user * @property userAgent the user agent of the requests for this user * @property country the country location of the user * @property locale the locale for the user @@ -28,51 +28,54 @@ data class StatsigUser private constructor( @SerializedName("customIDs") var customIDs: Map?, -) { - constructor(userID: String) : this(userID, null) - constructor(customIDs: Map) : this(null, customIDs) @SerializedName("email") - var email: String? = null + var email: String? = null, @SerializedName("ip") - var ip: String? = null + var ip: String? = null, @SerializedName("userAgent") - var userAgent: String? = null + var userAgent: String? = null, @SerializedName("country") - var country: String? = null + var country: String? = null, @SerializedName("locale") - var locale: String? = null + var locale: String? = null, @SerializedName("appVersion") - var appVersion: String? = null + var appVersion: String? = null, @SerializedName("custom") - var custom: Map? = null + var custom: Map? = null, @SerializedName("privateAttributes") - var privateAttributes: Map? = null + var privateAttributes: Map? = null, @SerializedName("statsigEnvironment") internal var statsigEnvironment: Map? = null +) { + constructor(userID: String) : this(userID = userID, null) + + constructor(customIDs: Map) : this(null, customIDs = customIDs) + + constructor(userID: String, customIDs: Map) : this(userID = userID, customIDs = customIDs, null) internal fun getCopyForLogging(): StatsigUser { - val userCopy = StatsigUser(userID, customIDs) - userCopy.email = email - userCopy.ip = ip - userCopy.userAgent = userAgent - userCopy.country = country - userCopy.locale = locale - userCopy.appVersion = appVersion - userCopy.custom = custom - userCopy.statsigEnvironment = statsigEnvironment - // DO NOT copy privateAttributes to the logging copy! - userCopy.privateAttributes = null - - return userCopy + return StatsigUser( + userID = this.userID, + customIDs = this.customIDs, + email = this.email, + ip = this.ip, + userAgent = this.userAgent, + country = this.country, + locale = this.locale, + appVersion = this.appVersion, + custom = this.custom, + statsigEnvironment = this.statsigEnvironment + // DO NOT copy privateAttributes to the logging copy! + ) } fun getHashWithoutStableID(): String { @@ -105,50 +108,18 @@ data class StatsigUser private constructor( override fun toString(): String { val sb = StringBuilder() sb.append("StatsigUser {\n") - sb.append("\tuserID: ") - sb.append(userID) - sb.append("\n") - - sb.append("\temail: ") - sb.append(email) - sb.append("\n") - - sb.append("\tip: ") - sb.append(ip) - sb.append("\n") - - sb.append("\tuserAgent: ") - sb.append(userAgent) - sb.append("\n") - - sb.append("\tcountry: ") - sb.append(country) - sb.append("\n") - - sb.append("\tlocale: ") - sb.append(locale) - sb.append("\n") - - sb.append("\tappVersion: ") - sb.append(appVersion) - sb.append("\n") - - sb.append("\tcustom: ") - sb.append(custom) - sb.append("\n") - - sb.append("\tstatsigEnvironment: ") - sb.append(statsigEnvironment) - sb.append("\n}") - - sb.append("\tprivateAttributes: ") - sb.append(privateAttributes) - sb.append("\n") - - sb.append("\tcustomIDs: ") - sb.append(customIDs) - sb.append("\n") - + sb.append("\tuserID: ").append(userID).append("\n") + sb.append("\temail: ").append(email).append("\n") + sb.append("\tip: ").append(ip).append("\n") + sb.append("\tuserAgent: ").append(userAgent).append("\n") + sb.append("\tcountry: ").append(country).append("\n") + sb.append("\tlocale: ").append(locale).append("\n") + sb.append("\tappVersion: ").append(appVersion).append("\n") + sb.append("\tcustom: ").append(custom).append("\n") + sb.append("\tstatsigEnvironment: ").append(statsigEnvironment).append("\n") + sb.append("\tprivateAttributes: ").append(privateAttributes).append("\n") + sb.append("\tcustomIDs: ").append(customIDs).append("\n") + sb.append("}") return sb.toString() } diff --git a/src/test/java/com/statsig/sdk/StatsigUserTest.kt b/src/test/java/com/statsig/sdk/StatsigUserTest.kt index a9a285d..25b0845 100644 --- a/src/test/java/com/statsig/sdk/StatsigUserTest.kt +++ b/src/test/java/com/statsig/sdk/StatsigUserTest.kt @@ -14,6 +14,15 @@ class StatsigUserTest { assertNull(user.customIDs) } + @Test + fun testInitializeWithUserIDAndCustomID() { + val userID = "abc123" + val customIDs = mapOf("customID" to "abc123") + val user = StatsigUser(userID, customIDs) + assertEquals(user.userID, "abc123") + assertEquals(user.customIDs, customIDs) + } + @Test fun testInitializeCustomID() { val customIDs = mapOf("customID" to "abc123") diff --git a/src/test/java/com/statsig/sdk/StatsigUserTestJava.java b/src/test/java/com/statsig/sdk/StatsigUserTestJava.java new file mode 100644 index 0000000..9c3c5f6 --- /dev/null +++ b/src/test/java/com/statsig/sdk/StatsigUserTestJava.java @@ -0,0 +1,23 @@ +package com.statsig.sdk; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class StatsigUserTestJava { + @Test + public void testInitializeUserIDAndCustomID() { + String userId = "test123"; + Map customIds = new HashMap<>(); + customIds.put("k1", "v1"); + customIds.put("k2", "v2"); + + StatsigUser user = new StatsigUser(userId, customIds); + + assertEquals(userId, user.getUserID()); + assertEquals(customIds, user.getCustomIDs()); + } +}