Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -49,6 +49,7 @@ import java.time.format.DateTimeFormatterBuilder
import java.time.temporal.Temporal
import java.time.temporal.TemporalQuery
import java.util.Locale
import java.util.UUID
import kotlin.properties.Delegates
import kotlin.reflect.KClass
import kotlin.reflect.KType
Expand Down Expand Up @@ -491,6 +492,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.fromString(str)
} catch (e: IllegalArgumentException) {
null
}
} else {
null
}
},
// BigInteger
stringParser<BigInteger> { it.toBigIntegerOrNull() },
// BigDecimal
Expand Down
23 changes: 23 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 @@ -18,6 +19,7 @@ import org.jetbrains.kotlinx.dataframe.impl.catchSilent
import org.jetbrains.kotlinx.dataframe.type
import org.junit.Test
import java.util.Locale
import java.util.UUID
import kotlin.random.Random
import kotlin.reflect.typeOf
import kotlin.time.Duration
Expand Down Expand Up @@ -481,6 +483,27 @@ class ParseTests {
df.parse()
}

@Test
fun `parse valid UUID`() {
val uuidString = "550e8400-e29b-41d4-a716-446655440000"
val column by columnOf(uuidString)
val parsed = column.parse()

parsed.type() shouldBe typeOf<UUID>()
(parsed[0] as UUID).toString() shouldBe uuidString
}

@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