Skip to content

Commit

Permalink
Fix ttl (#1351)
Browse files Browse the repository at this point in the history
* Fix ttl parsing

* Fix ttl parsing

* spotless!
  • Loading branch information
rlepinski authored Nov 9, 2023
1 parent 3da3af3 commit 11321e4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import com.urbanairship.json.optionalField

internal data class EvaluationOptions(
val disallowStaleValue: Boolean?,
val disallowStaleContact: Boolean?,
val ttl: Int?
) {
companion object {
private const val KEY_DISALLOW_STALE_VALUE = "disallow_stale_value"
private const val KEY_DISALLOW_STALE_CONTACT = "disallow_stale_contact"
private const val KEY_TTL = "ttl"

/**
Expand All @@ -28,7 +26,6 @@ internal data class EvaluationOptions(
try {
return EvaluationOptions(
disallowStaleValue = json.optionalField(KEY_DISALLOW_STALE_VALUE),
disallowStaleContact = json.optionalField(KEY_DISALLOW_STALE_CONTACT),
ttl = json.optionalField(KEY_TTL)
)
} catch (ex: JsonException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public inline fun <reified T> JsonMap.optionalField(key: String): T? {
String::class -> field.optString() as T
Boolean::class -> field.getBoolean(false) as T
Long::class -> field.getLong(0) as T
ULong::class -> field.getLong(0).toULong() as T
Double::class -> field.getDouble(0.0) as T
Integer::class -> field.getInt(0) as T
JsonList::class -> field.optList() as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,10 @@ internal class EvaluationOptions(
private const val KEY_TTL = "ttl"

fun fromJson(json: JsonMap): EvaluationOptions? {
val converted: (JsonValue) -> ULong? = {
if (it.isLong) { it.getLong(0).toULong() } else { null }
}

try {
return EvaluationOptions(
return EvaluationOptions(
disallowStaleValues = json.optionalField(KEY_STALE_DATA_FLAG),
ttl = converted(json.opt(KEY_TTL))
)
} catch (ex: JsonException) {
UALog.e(ex) { "failed to parse Evaluation options from $json" }
return null
}
ttl = json.optionalField(KEY_TTL)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.urbanairship.featureflag

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.urbanairship.json.jsonMapOf
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class EvaluationOptionsTest {

@Test
fun testParse() {
val json = jsonMapOf(
"disallow_stale_value" to true,
"ttl" to 1800000
)

val fromJson = EvaluationOptions.fromJson(json)
assert(fromJson?.disallowStaleValues == true)
assert(fromJson?.ttl == 1800000.toULong())
}
}

0 comments on commit 11321e4

Please sign in to comment.