Skip to content

Commit

Permalink
Implement date field type
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed Sep 24, 2021
1 parent 9df6bb3 commit e6fc2cf
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 11 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ configure<KotlinMultiplatformExtension> {
dependencies {
api(project(":elasticmagic-serde"))
api(project(":elasticmagic-transport"))
implementation(Libs.kotlinCoroutines("core"))
implementation(Libs.kotlinxCoroutines("core"))
api(Libs.kotlinxDatetime())
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions buildSrc/src/main/kotlin/Libs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object Versions {
val kotlin = versionProps["kotlin"]!!.toString()
const val kotlinxSerialization = "1.1.0"
const val kotlinxCoroutines = "1.4.3-native-mt"
const val kotlinxDatetime = "0.2.1"
const val ktor = "1.5.2"

// Other dependencies
Expand All @@ -39,12 +40,20 @@ object Libs {
return "io.kotest:kotest-$flavor:${Versions.kotest}"
}

fun kotlinSerialization(flavor: String): String {
return "org.jetbrains.kotlinx:kotlinx-serialization-$flavor:${Versions.kotlinxSerialization}"
fun kotlinxLib(lib: String, version: String): String {
return "org.jetbrains.kotlinx:kotlinx-$lib:$version"
}

fun kotlinCoroutines(flavor: String): String {
return "org.jetbrains.kotlinx:kotlinx-coroutines-$flavor:${Versions.kotlinxCoroutines}"
fun kotlinxSerialization(flavor: String): String {
return kotlinxLib("serialization-$flavor", Versions.kotlinxSerialization)
}

fun kotlinxCoroutines(flavor: String): String {
return kotlinxLib("coroutines-$flavor", Versions.kotlinxCoroutines)
}

fun kotlinxDatetime(): String {
return kotlinxLib("datetime", Versions.kotlinxDatetime)
}

fun ktorClient(flavor: String): String {
Expand Down
2 changes: 1 addition & 1 deletion integ-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ kotlin {
api(project(":elasticmagic-serde-serialization-json"))
api(project(":elasticmagic-transport-ktor"))

implementation(Libs.kotlinCoroutines("core"))
implementation(Libs.kotlinxCoroutines("core"))
implementation(Libs.ktorClient("core"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion serde-serialization-json/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ kotlin {
val commonMain by getting {
dependencies {
api(project(":elasticmagic-serde"))
api(Libs.kotlinSerialization("json"))
api(Libs.kotlinxSerialization("json"))
}
}
val commonTest by getting {
Expand Down
16 changes: 16 additions & 0 deletions src/commonMain/kotlin/dev/evo/elasticmagic/Document.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.evo.elasticmagic

import kotlinx.datetime.LocalDateTime
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

Expand Down Expand Up @@ -253,6 +254,21 @@ abstract class FieldSet : Named {
params = params,
)
}
fun date(
name: String? = null,
docValues: Boolean? = null,
index: Boolean? = null,
store: Boolean? = null,
params: Params? = null,
): Field<LocalDateTime> {
return field(
name, DateType,
docValues = docValues,
index = index,
store = store,
params = params,
)
}
fun keyword(
name: String? = null,
normalizer: String? = null,
Expand Down
21 changes: 21 additions & 0 deletions src/commonMain/kotlin/dev/evo/elasticmagic/FieldType.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package dev.evo.elasticmagic

import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime

class ValueDeserializationException(value: Any, type: String) :
IllegalArgumentException("Cannot deserialize $value to $type")

Expand Down Expand Up @@ -90,6 +96,21 @@ object TextType : StringType() {
override val name = "text"
}

object DateType : FieldType<LocalDateTime> {
override val name = "date"

override fun serialize(v: LocalDateTime): Any {
return v.toInstant(TimeZone.UTC).toString()
}

override fun deserialize(v: Any, valueFactory: (() -> LocalDateTime)?) = when (v) {
is Int -> Instant.fromEpochMilliseconds(v.toLong()).toLocalDateTime(TimeZone.UTC)
is Long -> Instant.fromEpochMilliseconds(v).toLocalDateTime(TimeZone.UTC)
is String -> Instant.parse(v).toLocalDateTime(TimeZone.UTC)
else -> throw ValueDeserializationException(v, "LocalDateTime")
}
}

data class Join(
val name: String,
val parent: String? = null,
Expand Down
22 changes: 22 additions & 0 deletions src/commonTest/kotlin/dev/evo/elasticmagic/FieldTypeTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.evo.elasticmagic

import io.kotest.matchers.shouldBe
import kotlinx.datetime.LocalDateTime

import kotlin.test.Test

class FieldTypeTests {
@Test
fun dateSerialize() {
DateType.serialize(LocalDateTime(2015, 1, 1, 12, 10, 30)) shouldBe "2015-01-01T12:10:30Z"
DateType.serialize(LocalDateTime(2015, 1, 1, 12, 10, 30, 1_000_000)) shouldBe "2015-01-01T12:10:30.001Z"
}

@Test
fun dateDeserialize() {
DateType.deserialize(0) shouldBe LocalDateTime(1970, 1, 1, 0, 0, 0)
DateType.deserialize(1618249875_000) shouldBe LocalDateTime(2021, 4, 12, 17, 51, 15)
DateType.deserialize("2015-01-01T12:10:30Z") shouldBe LocalDateTime(2015, 1, 1, 12, 10, 30)
DateType.deserialize("2015-01-01T12:10:30.001Z") shouldBe LocalDateTime(2015, 1, 1, 12, 10, 30, 1_000_000)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class MappingCompilerTests {
fun testRuntimeFields() {
val logDoc = object : Document() {
// TODO: Replace with date field
val timestamp by keyword("@timestamp")
val timestamp by date("@timestamp")

override val runtime = object : RuntimeFields() {
val dayOfWeek by runtime(
Expand Down Expand Up @@ -182,7 +182,7 @@ class MappingCompilerTests {
),
"properties" to mapOf(
"@timestamp" to mapOf(
"type" to "keyword",
"type" to "date",
),
)
)
Expand Down
2 changes: 1 addition & 1 deletion transport-ktor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ kotlin {
val commonTest by getting {
dependencies {
implementation(Libs.ktorClient("mock"))
implementation(Libs.kotlinSerialization("json"))
implementation(Libs.kotlinxSerialization("json"))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion transport/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ kotlin {
getByName("commonTest") {
dependencies {
api(project(":elasticmagic-serde-serialization-json"))
api(Libs.kotlinSerialization("json"))
api(Libs.kotlinxSerialization("json"))
}
}
}
Expand Down

0 comments on commit e6fc2cf

Please sign in to comment.