From 05444c233acb941ace359df4bfdeaa81dec888ec Mon Sep 17 00:00:00 2001 From: Alexandr Jeliuc Date: Mon, 18 Mar 2024 10:42:56 +0200 Subject: [PATCH] Migrated to `kotlinx.datetime` * removed custom datetime serializer * upgraded dependencies * updated models * upgraded README --- README.md | 5 +++- build.gradle.kts | 4 ++- gradle.properties | 1 + src/main/kotlin/model/AuditLog.kt | 5 ++-- src/main/kotlin/model/Organization.kt | 9 +++--- src/main/kotlin/resource/Databases.kt | 2 +- .../serializer/LocalDateTimeSerializer.kt | 29 ------------------- 7 files changed, 15 insertions(+), 40 deletions(-) delete mode 100644 src/main/kotlin/serializer/LocalDateTimeSerializer.kt diff --git a/README.md b/README.md index 0d50b2b..1f50adc 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Example: ``` ktorVersion=2.3.6 +dateTimeVersion=0.6.0-RC.2 logbackVersion=1.5.1 tursoSdkVersion=0.1.11 // currently published version ``` @@ -29,6 +30,7 @@ Add dependencies to `build.gradle.kts` ```Kotlin // define version variables val ktorVersion: String by project +val dateTimeVersion: String by project val logbackVersion: String by project val tursoSdkVersion: String by project @@ -44,6 +46,7 @@ dependencies { implementation("io.ktor:ktor-client-cio:$ktorVersion") implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion") implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") + implementation("org.jetbrains.kotlinx:kotlinx-datetime-jvm:$dateTimeVersion") implementation ("ch.qos.logback:logback-classic:$logbackVersion") implementation("com.jeliuc:turso-sdk-jvm:$tursoSdkVersion") // ... @@ -99,7 +102,7 @@ client.close() ## Future Plans -* [ ] Use `kotlinx.datetime` for date and time handling +* [x] Use `kotlinx.datetime` for date and time handling * [ ] Transform into a multiplatform library (KMP) - and support JVM, JS, and Native * [ ] HRANA3 protocol implementation (WIP) * [ ] libSQL JDBC driver (WIP) diff --git a/build.gradle.kts b/build.gradle.kts index 4483368..d9eed66 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,13 +6,14 @@ import org.jetbrains.dokka.versioning.VersioningConfiguration import org.jetbrains.dokka.versioning.VersioningPlugin val ktorVersion: String by project +val dateTimeVersion: String by project val spaceUsername: String? by project val spacePassword: String? by project -val sdkVersion: String by project val spaceMavenRepositoryUrl: String by project val sonatypeUsername: String by project val sonatypePassword: String by project +val sdkVersion: String by project group = "com.jeliuc" version = System.getenv("SDK_VERSION") ?: sdkVersion @@ -48,6 +49,7 @@ dependencies { implementation("io.ktor:ktor-client-cio:$ktorVersion") implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion") implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") + implementation("org.jetbrains.kotlinx:kotlinx-datetime-jvm:$dateTimeVersion") } tasks.test { diff --git a/gradle.properties b/gradle.properties index b798a05..aff0434 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,6 @@ kotlin.code.style=official ktorVersion=2.3.6 +dateTimeVersion=0.6.0-RC.2 sdkVersion=0.1.11 spaceUsername= spacePassword= diff --git a/src/main/kotlin/model/AuditLog.kt b/src/main/kotlin/model/AuditLog.kt index adb9061..71c1e7f 100644 --- a/src/main/kotlin/model/AuditLog.kt +++ b/src/main/kotlin/model/AuditLog.kt @@ -5,11 +5,10 @@ package com.jeliuc.turso.sdk.model -import com.jeliuc.turso.sdk.serializer.LocalDateTimeSerializer +import kotlinx.datetime.Instant import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonElement -import java.time.LocalDateTime @Serializable data class ListAuditLogsResponse( @@ -21,7 +20,7 @@ data class ListAuditLogsResponse( data class AuditLog( @SerialName("author") val author: String, @SerialName("code") val code: String, - @SerialName("created_at") @Serializable(with = LocalDateTimeSerializer::class) val createdAt: LocalDateTime, + @SerialName("created_at") val createdAt: Instant, @SerialName("data") val data: JsonElement, @SerialName("message") val message: String, @SerialName("origin") val origin: String, diff --git a/src/main/kotlin/model/Organization.kt b/src/main/kotlin/model/Organization.kt index 6dc1aa6..572774e 100644 --- a/src/main/kotlin/model/Organization.kt +++ b/src/main/kotlin/model/Organization.kt @@ -5,10 +5,9 @@ package com.jeliuc.turso.sdk.model -import com.jeliuc.turso.sdk.serializer.LocalDateTimeSerializer +import kotlinx.datetime.Instant import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import java.time.LocalDateTime @Serializable data class Organization( @@ -94,15 +93,15 @@ data class ListInvitesResponse( @Serializable data class Invite( @SerialName("Accepted") val accepted: Boolean, - @SerialName("CreatedAt") @Serializable(with = LocalDateTimeSerializer::class) val createdAt: LocalDateTime, - @SerialName("DeletedAt") @Serializable(with = LocalDateTimeSerializer::class) val deletedAt: LocalDateTime, + @SerialName("CreatedAt") val createdAt: Instant, + @SerialName("DeletedAt") val deletedAt: Instant, @SerialName("Email") val email: String, @SerialName("ID") val id: Int, @SerialName("Organization") val organization: Organization, @SerialName("OrganizationID") val organizationID: Int, @SerialName("Role") val role: MemberRole, @SerialName("Token") val token: String, - @SerialName("UpdatedAt") @Serializable(with = LocalDateTimeSerializer::class) val updatedAt: LocalDateTime, + @SerialName("UpdatedAt") val updatedAt: Instant, ) @Serializable diff --git a/src/main/kotlin/resource/Databases.kt b/src/main/kotlin/resource/Databases.kt index b69c3a9..0dc8d57 100644 --- a/src/main/kotlin/resource/Databases.kt +++ b/src/main/kotlin/resource/Databases.kt @@ -29,8 +29,8 @@ import io.ktor.http.ContentType import io.ktor.http.Headers import io.ktor.http.HttpHeaders import io.ktor.http.contentType +import kotlinx.datetime.LocalDateTime import java.io.File -import java.time.LocalDateTime val TursoClient.databases: Databases get() = Databases(this) diff --git a/src/main/kotlin/serializer/LocalDateTimeSerializer.kt b/src/main/kotlin/serializer/LocalDateTimeSerializer.kt deleted file mode 100644 index a573eaa..0000000 --- a/src/main/kotlin/serializer/LocalDateTimeSerializer.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2024 Jeliuc.com S.R.L. and Turso SDK contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - */ - -package com.jeliuc.turso.sdk.serializer - -import kotlinx.serialization.ExperimentalSerializationApi -import kotlinx.serialization.KSerializer -import kotlinx.serialization.Serializer -import kotlinx.serialization.encoding.Decoder -import kotlinx.serialization.encoding.Encoder -import java.time.LocalDateTime -import java.time.format.DateTimeFormatter - -@OptIn(ExperimentalSerializationApi::class) -@Serializer(forClass = LocalDateTime::class) -class LocalDateTimeSerializer : KSerializer { - override fun serialize( - encoder: Encoder, - value: LocalDateTime, - ) { - encoder.encodeString(value.format(DateTimeFormatter.ISO_DATE_TIME)) - } - - override fun deserialize(decoder: Decoder): LocalDateTime { - return LocalDateTime.parse(decoder.decodeString(), DateTimeFormatter.ISO_DATE_TIME) - } -}