-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Module kotlinx-uuid-datetime | ||
|
||
Provides support for UUIDv7 using kotlinx-datetime. | ||
|
||
```kotlin | ||
dependencies { | ||
implementation("app.softwork:kotlinx-uuid-datetime:LATEST") | ||
} | ||
``` | ||
|
||
When declaring a table having UUID as Primary Key: | ||
|
||
```kotlin | ||
// SQL DSL | ||
object MyTable : KotlinxUUIDTable() { | ||
// there is "id" property with the kotlin-uud type | ||
} | ||
|
||
// DAO API | ||
class MyTableEntity(id: EntityID<UUID>) : KotlinxUUIDEntity(id) { | ||
companion object : KotlinxUUIDEntityClass<MyTableEntity>(MyTable) | ||
|
||
} | ||
``` | ||
|
||
To declare a regular column, use `kotlinxUUID` function: | ||
|
||
```kotlin | ||
object MyTable : Table() { | ||
val something = kotlinxUUID("SOME_COLUMN") | ||
} | ||
``` | ||
|
||
Unfortunately, there is a function called `uuid` in the base class, inside Exposed, this is why we can't | ||
overwrite/override it, so it may lead to confusion. The function `uuid` only works with `java.util.UUID`: | ||
|
||
```kotlin | ||
object MyTable : Table() { | ||
val column1 = kotlinxUUID("C1") // kotlinx.uuid.UUID | ||
val column2 = uuid("C2") // java.util.UUID | ||
} | ||
``` |
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,36 @@ | ||
/* | ||
* Copyright 2020-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
* Copyright 2021 hfhbd and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
plugins { | ||
id("kotlinJvm") | ||
id("publish") | ||
id("dokkaLicensee") | ||
id("kover") | ||
} | ||
|
||
kotlin.jvmToolchain(11) | ||
|
||
dependencies { | ||
api(projects.kotlinxUuidCore) | ||
api(libs.datetime) | ||
|
||
testImplementation(kotlin("test-junit")) | ||
testRuntimeOnly(libs.slf4j) | ||
} | ||
|
||
licensee { | ||
allow("MIT") | ||
} | ||
|
||
publishing { | ||
publications.register<MavenPublication>("maven") { | ||
from(components["java"]) | ||
} | ||
} | ||
|
||
java { | ||
withJavadocJar() | ||
withSourcesJar() | ||
} |
20 changes: 20 additions & 0 deletions
20
kotlinx-uuid-datetime/src/main/kotlin/kotlinx/uuid/datetime/Dsl.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,20 @@ | ||
package kotlinx.uuid.datetime | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import kotlinx.uuid.SecureRandom | ||
import kotlinx.uuid.UUID | ||
import kotlinx.uuid.UUIDExperimentalAPI | ||
import kotlinx.uuid.UUIDv7 | ||
import kotlinx.uuid.unixTimeStamp | ||
import kotlin.random.Random | ||
|
||
@UUIDExperimentalAPI | ||
public fun UUIDv7(random: Random = SecureRandom): UUID = | ||
UUIDv7(timeStamp = Clock.System.now().toEpochMilliseconds(), random = random) | ||
|
||
/** | ||
* The UUIDv7 48 bit big-endian unsigned number of Unix epoch timestamp in milliseconds | ||
*/ | ||
@UUIDExperimentalAPI | ||
public val UUID.instant: Instant get() = Instant.fromEpochMilliseconds(unixTimeStamp) |
27 changes: 27 additions & 0 deletions
27
kotlinx-uuid-datetime/src/test/kotlin/kotlinx/uuid/datetime/InstantTest.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,27 @@ | ||
package kotlinx.uuid.datetime | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import kotlinx.uuid.UUIDExperimentalAPI | ||
import kotlinx.uuid.UUIDv7 | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
@OptIn(UUIDExperimentalAPI::class) | ||
class InstantTest { | ||
|
||
@Test | ||
fun testConversionInstant() { | ||
val timestamp = Clock.System.now() | ||
val expected = timestamp.clampToMillisecondPrecision() | ||
|
||
val uuid = UUIDv7(timeStamp = timestamp.toEpochMilliseconds()) | ||
val result = uuid.instant | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
private fun Instant.clampToMillisecondPrecision(): Instant { | ||
return Instant.fromEpochMilliseconds(this.toEpochMilliseconds()) | ||
} | ||
} |
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