Skip to content

Commit

Permalink
fix kotlin warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kenny-statsig committed Jul 12, 2023
1 parent c0b4700 commit 69f6476
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
32 changes: 25 additions & 7 deletions src/main/java/com/statsig/androidsdk/DynamicConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ class DynamicConfig(
}
}

private inline fun <reified A, reified B> List<*>.asListOfPairs(): List<Pair<A, B>>? {
return this.mapNotNull { if (it is Pair<*, *>) it.asPairOf() else null }
}

private inline fun <reified A, reified B> Pair<*, *>.asPairOf(): Pair<A, B>? {
if (first !is A || second !is B) return null
return first as A to second as B
}

private inline fun <reified K, reified V> Map<*, *>.asMapOf(default: Map<K, V>? = null): Map<K, V>? {
if (keys.first() !is K || values.first() !is V) return default
return toList().asListOfPairs<K, V>()?.associate { Pair(it.first, it.second) }
}

/**
* Gets a dictionary from the config, falling back to the provided default value
* @param key the index within the DynamicConfig to fetch a dictionary from
Expand All @@ -108,7 +122,7 @@ class DynamicConfig(
*/
fun getDictionary(key: String, default: Map<String, Any>?): Map<String, Any>? {
return when (val value = this.jsonValue[key]) {
is Map<*, *> -> value as Map<String, Any>
is Map<*, *> -> value.asMapOf()
else -> default
}
}
Expand All @@ -120,12 +134,16 @@ class DynamicConfig(
*/
fun getConfig(key: String): DynamicConfig? {
return when (val value = this.jsonValue[key]) {
is Map<*, *> -> DynamicConfig(
key,
value as Map<String, Any>,
this.rule,
this.details,
)
is Map<*, *> ->
when (val valueTyped = value.asMapOf<String, Any>()) {
null -> null
else -> DynamicConfig(
key,
valueTyped,
this.rule,
this.details,
)
}
else -> null
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/statsig/androidsdk/StatsigClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ internal class StatsigClient() {
}
}
}
return stableID!!
return stableID
}

internal fun isInitialized(): Boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/statsig/androidsdk/StatsigNetwork.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal interface StatsigNetwork {

suspend fun apiRetryFailedLogs(api: String, sdkKey: String)

suspend fun addFailedLogRequest(body: String)
suspend fun addFailedLogRequest(requestBody: String)
}

internal fun StatsigNetwork(): StatsigNetwork = StatsigNetworkImpl()
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/statsig/androidsdk/DynamicConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class DynamicConfigTest {
assertEquals(true, emptyConfig.getBoolean("test_config", true))
assertEquals(3.0, emptyConfig.getDouble("test_config", 3.0), 0.0)
val arr = arrayOf("test", "one")
@Suppress("UNCHECKED_CAST")
assertArrayEquals(arr, emptyConfig.getArray("test_config", arr as Array<Any>))
assertEquals("default", emptyConfig.getRuleID())
assertNull(emptyConfig.getConfig("nested"))
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/statsig/androidsdk/LayerConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class LayerConfigTest {
assertEquals(true, emptyConfig.getBoolean("test_config", true))
assertEquals(3.0, emptyConfig.getDouble("test_config", 3.0), 0.0)
val arr = arrayOf("test", "one")
@Suppress("UNCHECKED_CAST")
assertArrayEquals(arr, emptyConfig.getArray("test_config", arr as Array<Any>))
assertEquals("default", emptyConfig.getRuleID())
assertNull(emptyConfig.getConfig("nested"))
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/statsig/androidsdk/TestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class TestUtil {
}
""".trimIndent()
val gson = GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create()
@Suppress("UNCHECKED_CAST")
return gson.fromJson(string, Map::class.java) as Map<String, Any>
}

Expand Down

0 comments on commit 69f6476

Please sign in to comment.