Skip to content

Commit e41f337

Browse files
Parse String to UUID #1006
## Summary Implements UUID parsing support for DataColumn.parse() as per open issue #1006 ## Changes - Added UUID parser to `parsersOrder` list in `main/../parse.kt` using `java.util.UUID.fromString()` with exception handling - Added test cases in `test/../ParseTests.kt`: - Valid UUID strings are parsed to UUID objects - Invalid UUID strings remain String type ## Testing - Both positive and negative test cases pass - Unable to run full test suite due to Java version compatibility issue with simple-git plugin (unrelated to this change)
1 parent d81af82 commit e41f337

File tree

2 files changed

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

2 files changed

+35
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ 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
5253
import kotlin.properties.Delegates
5354
import kotlin.reflect.KClass
5455
import kotlin.reflect.KType
@@ -62,6 +63,8 @@ import java.time.LocalDate as JavaLocalDate
6263
import java.time.LocalDateTime as JavaLocalDateTime
6364
import java.time.LocalTime as JavaLocalTime
6465

66+
67+
6568
private val logger = KotlinLogging.logger { }
6669

6770
internal interface StringParser<T> {
@@ -491,6 +494,15 @@ internal object Parsers : GlobalParserOptions {
491494
posixParserToDoubleWithOptions,
492495
// Boolean
493496
stringParser<Boolean> { it.toBooleanOrNull() },
497+
//UUID
498+
stringParser<UUID> {str ->
499+
try{
500+
UUID.fromString(str)
501+
} catch(e: IllegalArgumentException){
502+
null
503+
}
504+
},
505+
494506
// BigInteger
495507
stringParser<BigInteger> { it.toBigIntegerOrNull() },
496508
// BigDecimal

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

Lines changed: 23 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
@@ -18,6 +19,7 @@ import org.jetbrains.kotlinx.dataframe.impl.catchSilent
1819
import org.jetbrains.kotlinx.dataframe.type
1920
import org.junit.Test
2021
import java.util.Locale
22+
import java.util.UUID
2123
import kotlin.random.Random
2224
import kotlin.reflect.typeOf
2325
import kotlin.time.Duration
@@ -481,6 +483,27 @@ class ParseTests {
481483
df.parse()
482484
}
483485

486+
@Test
487+
fun `parse valid UUID`() {
488+
val uuidString = "550e8400-e29b-41d4-a716-446655440000"
489+
val column by columnOf(uuidString)
490+
val parsed = column.parse()
491+
492+
parsed.type() shouldBe typeOf<UUID>()
493+
(parsed[0] as UUID).toString() shouldBe uuidString
494+
}
495+
496+
@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.
501+
502+
parsed.type() shouldNotBe typeOf<UUID>()
503+
parsed.type() shouldBe typeOf<String>()
504+
}
505+
506+
484507
/**
485508
* Asserts that all elements of the iterable are equal to each other
486509
*/

0 commit comments

Comments
 (0)