diff --git a/FlagsmithClient/src/main/java/com/flagsmith/entities/Trait.kt b/FlagsmithClient/src/main/java/com/flagsmith/entities/Trait.kt index 2e79dbe..f03c2c8 100644 --- a/FlagsmithClient/src/main/java/com/flagsmith/entities/Trait.kt +++ b/FlagsmithClient/src/main/java/com/flagsmith/entities/Trait.kt @@ -45,21 +45,42 @@ data class Trait ( } -data class TraitWithIdentity( +data class TraitWithIdentity ( @SerializedName(value = "trait_key") val key: String, - @SerializedName(value = "trait_value") val value: Any, + @SerializedName(value = "trait_value") val traitValue: Any, val identity: Identity, ) { + constructor(key: String, value: String, identity: Identity) + : this(key = key, traitValue = value, identity = identity) + + constructor(key: String, value: Int, identity: Identity) + : this(key = key, traitValue = value, identity = identity) + + constructor(key: String, value: Double, identity: Identity) + : this(key = key, traitValue = value, identity = identity) + + constructor(key: String, value: Boolean, identity: Identity) + : this(key = key, traitValue = value, identity = identity) + + @Deprecated("Use traitValue instead or one of the type-safe getters", ReplaceWith("traitValue")) + val value: String + get() { return traitValue as? String ?: traitValue.toString() } + val stringValue: String? - get() = value as? String + get() = traitValue as? String val intValue: Int? - get() = (value as? Double)?.toInt() + get() { + return when (traitValue) { + is Int -> traitValue + is Double -> traitValue.toInt() + else -> null + } + } val doubleValue: Double? - get() = value as? Double + get() = traitValue as? Double val booleanValue: Boolean? - get() = value as? Boolean - + get() = traitValue as? Boolean } diff --git a/FlagsmithClient/src/test/java/com/flagsmith/entities/TraitEntityTests.kt b/FlagsmithClient/src/test/java/com/flagsmith/entities/TraitEntityTests.kt index d8af99e..742b17a 100644 --- a/FlagsmithClient/src/test/java/com/flagsmith/entities/TraitEntityTests.kt +++ b/FlagsmithClient/src/test/java/com/flagsmith/entities/TraitEntityTests.kt @@ -81,6 +81,11 @@ class TraitEntityTests { Assert.assertEquals("string-value", trait.traitValue) Assert.assertEquals("string-value", trait.stringValue) Assert.assertNull(trait.intValue) + + val traitWithIdentity = TraitWithIdentity("string-key", "string-value", Identity("person")) + Assert.assertEquals("string-value", traitWithIdentity.traitValue) + Assert.assertEquals("string-value", traitWithIdentity.stringValue) + Assert.assertNull(traitWithIdentity.intValue) } @Test @@ -92,6 +97,14 @@ class TraitEntityTests { Assert.assertNull(trait.stringValue) Assert.assertEquals("We should maintain the original functionality for the String .value", "1", trait.value) + + val traitWithIdentity = TraitWithIdentity("string-key", 1, Identity("person")) + Assert.assertEquals(1, traitWithIdentity.traitValue) + Assert.assertEquals(1, traitWithIdentity.intValue) + Assert.assertNull("Can't convert an int to a double", traitWithIdentity.doubleValue) + Assert.assertNull(traitWithIdentity.stringValue) + Assert.assertEquals("We should maintain the original functionality for the String .value", + "1", traitWithIdentity.value) } @Test @@ -104,6 +117,15 @@ class TraitEntityTests { Assert.assertNull(trait.stringValue) Assert.assertEquals("We should maintain the original functionality for the String .value", "1.0", trait.value) + + val traitWithIdentity = TraitWithIdentity("string-key", 1.0, Identity("person")) + Assert.assertEquals(1.0, traitWithIdentity.traitValue) + Assert.assertEquals(1.0, traitWithIdentity.doubleValue) + Assert.assertEquals("JS ints are actually doubles so we should handle this", + 1, traitWithIdentity.intValue) + Assert.assertNull(traitWithIdentity.stringValue) + Assert.assertEquals("We should maintain the original functionality for the String .value", + "1.0", traitWithIdentity.value) } @Test diff --git a/FlagsmithClient/src/test/java/com/flagsmith/mockResponses/endpoints/TraitsEndpoint.kt b/FlagsmithClient/src/test/java/com/flagsmith/mockResponses/endpoints/TraitsEndpoint.kt index c92e40d..366d833 100644 --- a/FlagsmithClient/src/test/java/com/flagsmith/mockResponses/endpoints/TraitsEndpoint.kt +++ b/FlagsmithClient/src/test/java/com/flagsmith/mockResponses/endpoints/TraitsEndpoint.kt @@ -11,7 +11,7 @@ data class TraitsEndpoint(private val trait: Trait, private val identity: String body = Gson().toJson( TraitWithIdentity( key = trait.key, - value = trait.traitValue, + traitValue = trait.traitValue, identity = Identity(identity) ) ),