Skip to content

Parse String to UUID #1006 #1287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import kotlin.reflect.full.withNullability
import kotlin.reflect.jvm.jvmErasure
import kotlin.reflect.typeOf
import kotlin.time.Duration
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
import java.time.Duration as JavaDuration
import java.time.Instant as JavaInstant
import java.time.LocalDate as JavaLocalDate
Expand Down Expand Up @@ -426,6 +428,7 @@ internal object Parsers : GlobalParserOptions {
}
}

@OptIn(ExperimentalUuidApi::class)
internal val parsersOrder = listOf(
// Int
stringParser<Int> { it.toIntOrNull() },
Expand Down Expand Up @@ -491,6 +494,21 @@ internal object Parsers : GlobalParserOptions {
posixParserToDoubleWithOptions,
// Boolean
stringParser<Boolean> { it.toBooleanOrNull() },
// UUID
stringParser<Uuid> { str ->

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}")

if (uuidRegex.matches(str)) {
try {
Uuid.parse(str)
} catch (e: IllegalArgumentException) {
null
}
} else {
null
}
},
// BigInteger
stringParser<BigInteger> { it.toBigIntegerOrNull() },
// BigDecimal
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/parse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.dataframe.api

import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
Expand All @@ -28,6 +29,8 @@ import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
import java.time.Duration as JavaDuration
import java.time.Instant as JavaInstant

Expand Down Expand Up @@ -481,6 +484,28 @@ class ParseTests {
df.parse()
}

@OptIn(ExperimentalUuidApi::class)
@Test
fun `parse valid Uuid`() {
val validUUID = "550e8400-e29b-41d4-a716-446655440000"
val column by columnOf(validUUID)
val parsed = column.parse()

parsed.type() shouldBe typeOf<Uuid>()
(parsed[0] as Uuid).toString() shouldBe validUUID // Change UUID to Uuid
}

@OptIn(ExperimentalUuidApi::class)
@Test
fun `parse invalid Uuid`() {
val invalidUUID = "this is not a UUID"
val column = columnOf(invalidUUID)
val parsed = column.tryParse() // tryParse as string is not formatted.

parsed.type() shouldNotBe typeOf<Uuid>()
parsed.type() shouldBe typeOf<String>()
}

/**
* Asserts that all elements of the iterable are equal to each other
*/
Expand Down
Loading