Skip to content

Commit

Permalink
Updated the TraitWithIdentity in-line with the Trait class and added …
Browse files Browse the repository at this point in the history
…unit tests
  • Loading branch information
gazreese committed Feb 15, 2024
1 parent 166e6ce commit 63a6b75
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
35 changes: 28 additions & 7 deletions FlagsmithClient/src/main/java/com/flagsmith/entities/Trait.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
),
Expand Down

0 comments on commit 63a6b75

Please sign in to comment.