Skip to content

Commit c3aafb8

Browse files
authored
Merge pull request #1287 from EmmanuelBerkowicz/master
Parse String to UUID #1006
2 parents eac6a31 + a9d7e91 commit c3aafb8

File tree

2 files changed

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

2 files changed

+43
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ import kotlin.reflect.full.withNullability
5656
import kotlin.reflect.jvm.jvmErasure
5757
import kotlin.reflect.typeOf
5858
import kotlin.time.Duration
59+
import kotlin.uuid.ExperimentalUuidApi
60+
import kotlin.uuid.Uuid
5961
import java.time.Duration as JavaDuration
6062
import java.time.Instant as JavaInstant
6163
import java.time.LocalDate as JavaLocalDate
@@ -426,6 +428,7 @@ internal object Parsers : GlobalParserOptions {
426428
}
427429
}
428430

431+
@OptIn(ExperimentalUuidApi::class)
429432
internal val parsersOrder = listOf(
430433
// Int
431434
stringParser<Int> { it.toIntOrNull() },
@@ -491,6 +494,21 @@ internal object Parsers : GlobalParserOptions {
491494
posixParserToDoubleWithOptions,
492495
// Boolean
493496
stringParser<Boolean> { it.toBooleanOrNull() },
497+
// UUID
498+
stringParser<Uuid> { str ->
499+
500+
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}")
501+
502+
if (uuidRegex.matches(str)) {
503+
try {
504+
Uuid.parse(str)
505+
} catch (e: IllegalArgumentException) {
506+
null
507+
}
508+
} else {
509+
null
510+
}
511+
},
494512
// BigInteger
495513
stringParser<BigInteger> { it.toBigIntegerOrNull() },
496514
// BigDecimal

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.dataframe.api
22

33
import io.kotest.matchers.should
44
import io.kotest.matchers.shouldBe
5+
import io.kotest.matchers.shouldNotBe
56
import kotlinx.datetime.DateTimeUnit
67
import kotlinx.datetime.Instant
78
import kotlinx.datetime.LocalDate
@@ -28,6 +29,8 @@ import kotlin.time.Duration.Companion.milliseconds
2829
import kotlin.time.Duration.Companion.minutes
2930
import kotlin.time.Duration.Companion.nanoseconds
3031
import kotlin.time.Duration.Companion.seconds
32+
import kotlin.uuid.ExperimentalUuidApi
33+
import kotlin.uuid.Uuid
3134
import java.time.Duration as JavaDuration
3235
import java.time.Instant as JavaInstant
3336

@@ -481,6 +484,28 @@ class ParseTests {
481484
df.parse()
482485
}
483486

487+
@OptIn(ExperimentalUuidApi::class)
488+
@Test
489+
fun `parse valid Uuid`() {
490+
val validUUID = "550e8400-e29b-41d4-a716-446655440000"
491+
val column by columnOf(validUUID)
492+
val parsed = column.parse()
493+
494+
parsed.type() shouldBe typeOf<Uuid>()
495+
(parsed[0] as Uuid).toString() shouldBe validUUID // Change UUID to Uuid
496+
}
497+
498+
@OptIn(ExperimentalUuidApi::class)
499+
@Test
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.
504+
505+
parsed.type() shouldNotBe typeOf<Uuid>()
506+
parsed.type() shouldBe typeOf<String>()
507+
}
508+
484509
/**
485510
* Asserts that all elements of the iterable are equal to each other
486511
*/

0 commit comments

Comments
 (0)