Skip to content

Commit a9d7e91

Browse files
Parse String to UUID #1006
Use kotlin.uuid.Uuid instead of java.util.UUID change implemented in both test and main parse.kt
1 parent 8f194d5 commit a9d7e91

File tree

2 files changed

+20
-16
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api

2 files changed

+20
-16
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/parse.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ import java.time.format.DateTimeFormatterBuilder
4949
import java.time.temporal.Temporal
5050
import java.time.temporal.TemporalQuery
5151
import java.util.Locale
52-
import java.util.UUID
5352
import kotlin.properties.Delegates
5453
import kotlin.reflect.KClass
5554
import kotlin.reflect.KType
5655
import kotlin.reflect.full.withNullability
5756
import kotlin.reflect.jvm.jvmErasure
5857
import kotlin.reflect.typeOf
5958
import kotlin.time.Duration
59+
import kotlin.uuid.ExperimentalUuidApi
60+
import kotlin.uuid.Uuid
6061
import java.time.Duration as JavaDuration
6162
import java.time.Instant as JavaInstant
6263
import java.time.LocalDate as JavaLocalDate
@@ -427,6 +428,7 @@ internal object Parsers : GlobalParserOptions {
427428
}
428429
}
429430

431+
@OptIn(ExperimentalUuidApi::class)
430432
internal val parsersOrder = listOf(
431433
// Int
432434
stringParser<Int> { it.toIntOrNull() },
@@ -493,13 +495,13 @@ internal object Parsers : GlobalParserOptions {
493495
// Boolean
494496
stringParser<Boolean> { it.toBooleanOrNull() },
495497
// UUID
496-
stringParser<UUID> { str ->
498+
stringParser<Uuid> { str ->
497499

498500
val uuidRegex = Regex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
499501

500502
if (uuidRegex.matches(str)) {
501503
try {
502-
UUID.fromString(str)
504+
Uuid.parse(str)
503505
} catch (e: IllegalArgumentException) {
504506
null
505507
}

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/parse.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import org.jetbrains.kotlinx.dataframe.impl.catchSilent
1919
import org.jetbrains.kotlinx.dataframe.type
2020
import org.junit.Test
2121
import java.util.Locale
22-
import java.util.UUID
2322
import kotlin.random.Random
2423
import kotlin.reflect.typeOf
2524
import kotlin.time.Duration
@@ -30,6 +29,8 @@ import kotlin.time.Duration.Companion.milliseconds
3029
import kotlin.time.Duration.Companion.minutes
3130
import kotlin.time.Duration.Companion.nanoseconds
3231
import kotlin.time.Duration.Companion.seconds
32+
import kotlin.uuid.ExperimentalUuidApi
33+
import kotlin.uuid.Uuid
3334
import java.time.Duration as JavaDuration
3435
import java.time.Instant as JavaInstant
3536

@@ -483,27 +484,28 @@ class ParseTests {
483484
df.parse()
484485
}
485486

487+
@OptIn(ExperimentalUuidApi::class)
486488
@Test
487-
fun `parse valid UUID`() {
488-
val uuidString = "550e8400-e29b-41d4-a716-446655440000"
489-
val column by columnOf(uuidString)
489+
fun `parse valid Uuid`() {
490+
val validUUID = "550e8400-e29b-41d4-a716-446655440000"
491+
val column by columnOf(validUUID)
490492
val parsed = column.parse()
491493

492-
parsed.type() shouldBe typeOf<UUID>()
493-
(parsed[0] as UUID).toString() shouldBe uuidString
494+
parsed.type() shouldBe typeOf<Uuid>()
495+
(parsed[0] as Uuid).toString() shouldBe validUUID // Change UUID to Uuid
494496
}
495497

498+
@OptIn(ExperimentalUuidApi::class)
496499
@Test
497-
fun `parse invalid UUID`(){
498-
val invalidUUID = "this is not a UUID"
499-
val column = columnOf(invalidUUID)
500-
val parsed = column.tryParse() // tryParse as string is not formatted.
500+
fun `parse invalid Uuid`() {
501+
val invalidUUID = "this is not a UUID"
502+
val column = columnOf(invalidUUID)
503+
val parsed = column.tryParse() // tryParse as string is not formatted.
501504

502-
parsed.type() shouldNotBe typeOf<UUID>()
503-
parsed.type() shouldBe typeOf<String>()
505+
parsed.type() shouldNotBe typeOf<Uuid>()
506+
parsed.type() shouldBe typeOf<String>()
504507
}
505508

506-
507509
/**
508510
* Asserts that all elements of the iterable are equal to each other
509511
*/

0 commit comments

Comments
 (0)