Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from spotify/format-dates-with-millis
Browse files Browse the repository at this point in the history
Format ISO8601 dates with milliseconds.
  • Loading branch information
nicklasl authored Jul 6, 2023
2 parents 220534d + 5a8181c commit 708732c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
22 changes: 15 additions & 7 deletions OpenFeature/src/main/java/dev/openfeature/sdk/Value.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ sealed interface Value {
override fun equals(other: Any?): kotlin.Boolean {
return other is Null
}

override fun hashCode(): Int {
return javaClass.hashCode()
}
Expand All @@ -73,14 +74,21 @@ object ValueSerializer : JsonContentPolymorphicSerializer<Value>(Value::class) {
}
}

@SuppressLint("SimpleDateFormat")
object DateSerializer : KSerializer<Date> {
@SuppressLint("SimpleDateFormat")
private val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply { timeZone = TimeZone.getTimeZone("UTC") }
private val dateFormatter =
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").apply { timeZone = TimeZone.getTimeZone("UTC") }
private val fallbackDateFormatter =
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply { timeZone = TimeZone.getTimeZone("UTC") }
override val descriptor = PrimitiveSerialDescriptor("Instant", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: Date) = encoder.encodeString(df.format(value))
override fun deserialize(decoder: Decoder): Date = try {
df.parse(decoder.decodeString()) ?: throw IllegalArgumentException("unable to parse ${decoder.decodeString()}")
} catch (e: Exception) {
throw IllegalArgumentException(e)
override fun serialize(encoder: Encoder, value: Date) = encoder.encodeString(dateFormatter.format(value))
override fun deserialize(decoder: Decoder): Date = with(decoder.decodeString()) {
try {
dateFormatter.parse(this)
?: throw IllegalArgumentException("unable to parse $this")
} catch (e: Exception) {
fallbackDateFormatter.parse(this)
?: throw IllegalArgumentException("unable to parse $this")
}
}
}
10 changes: 7 additions & 3 deletions OpenFeature/src/test/java/dev/openfeature/sdk/ValueTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ class ValueTests {

@Test
fun testStructShouldConvertToStruct() {
val value = Value.Structure(mapOf("field1" to Value.Integer(3), "field2" to Value.String("test")))
Assert.assertEquals(value.asStructure(), mapOf("field1" to Value.Integer(3), "field2" to Value.String("test")))
val value =
Value.Structure(mapOf("field1" to Value.Integer(3), "field2" to Value.String("test")))
Assert.assertEquals(
value.asStructure(),
mapOf("field1" to Value.Integer(3), "field2" to Value.String("test"))
)
}

@Test
Expand Down Expand Up @@ -82,7 +86,7 @@ class ValueTests {

@Test
fun testJsonDecode() {
val stringInstant = "2023-03-01T14:01:46Z"
val stringInstant = "2023-03-01T14:01:46.321Z"
val json = "{" +
" \"structure\": {" +
" \"null\": {}," +
Expand Down

0 comments on commit 708732c

Please sign in to comment.