-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for deserialization Duration in HOCON format
- Loading branch information
Alex Mihailov
committed
Oct 24, 2022
1 parent
a7cee0b
commit 430084b
Showing
5 changed files
with
118 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
formats/hocon/src/main/kotlin/kotlinx/serialization/hocon/internal/SuppressAnimalSniffer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kotlinx.serialization.hocon.internal | ||
|
||
/** | ||
* Suppresses Animal Sniffer plugin errors for certain methods. | ||
* Such methods include references to Java 8 methods that are not | ||
* available in Android API, but can be desugared by R8. | ||
*/ | ||
@Retention(AnnotationRetention.BINARY) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | ||
internal annotation class SuppressAnimalSniffer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
formats/hocon/src/test/kotlin/kotlinx/serialization/hocon/HoconDurationDeserializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package kotlinx.serialization.hocon | ||
|
||
import kotlin.time.* | ||
import kotlin.time.Duration.Companion.days | ||
import kotlin.time.Duration.Companion.hours | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlin.time.Duration.Companion.seconds | ||
import kotlinx.serialization.* | ||
import org.junit.Assert.* | ||
import org.junit.Test | ||
|
||
class HoconDurationDeserializerTest { | ||
|
||
@Serializable | ||
data class Simple(val d: Duration) | ||
|
||
@Serializable | ||
data class Nullable(val d: Duration?) | ||
|
||
@Serializable | ||
data class Complex(val i: Int, val s: Simple, val n: Nullable, val l: List<Simple>, val ln: List<Nullable>, val f: Boolean) | ||
|
||
@Test | ||
fun testDeserializeDurationInHoconFormat() { | ||
var obj = deserializeConfig("d = 10s", Simple.serializer()) | ||
assertEquals(10.seconds, obj.d) | ||
obj = deserializeConfig("d = 10 hours", Simple.serializer()) | ||
assertEquals(10.hours, obj.d) | ||
obj = deserializeConfig("d = 5 ms", Simple.serializer()) | ||
assertEquals(5.milliseconds, obj.d) | ||
} | ||
|
||
@Test | ||
fun testDeserializeNullableDurationInHoconFormat() { | ||
var obj = deserializeConfig("d = null", Nullable.serializer()) | ||
assertNull(obj.d) | ||
|
||
obj = deserializeConfig("d = 5 days", Nullable.serializer()) | ||
assertEquals(5.days, obj.d!!) | ||
} | ||
|
||
@Test | ||
fun testDeserializeComplexDurationInHoconFormat() { | ||
val obj = deserializeConfig(""" | ||
i = 6 | ||
s: { d = 5m } | ||
n: { d = null } | ||
l: [ { d = 1m }, { d = 2s } ] | ||
ln: [ { d = null }, { d = 6h } ] | ||
f = true | ||
""".trimIndent(), Complex.serializer()) | ||
assertEquals(5.minutes, obj.s.d) | ||
assertNull(obj.n.d) | ||
assertEquals(2, obj.l.size) | ||
assertEquals(1.minutes, obj.l[0].d) | ||
assertEquals(2.seconds, obj.l[1].d) | ||
assertEquals(2, obj.ln.size) | ||
assertNull(obj.ln[0].d) | ||
assertEquals(6.hours, obj.ln[1].d!!) | ||
assertEquals(6, obj.i) | ||
assertTrue(obj.f) | ||
} | ||
|
||
@Test | ||
fun testThrowsWhenNotTimeUnitHocon() { | ||
assertThrows(SerializationException::class.java) { | ||
deserializeConfig("d = 10 unknown", Simple.serializer()) | ||
} | ||
} | ||
} |