Skip to content

Commit 25c572a

Browse files
Automated commit of generated code
1 parent b7629f6 commit 25c572a

File tree

3 files changed

+118
-12
lines changed
  • core/generated-sources/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api

3 files changed

+118
-12
lines changed

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/parse.kt

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import java.time.format.DateTimeFormatter
2020
import java.util.Locale
2121
import kotlin.reflect.KProperty
2222
import kotlin.reflect.KType
23+
import kotlin.uuid.ExperimentalUuidApi
24+
import kotlin.uuid.Uuid
2325

2426
/**
2527
* ### Global Parser Options
@@ -73,6 +75,16 @@ public interface GlobalParserOptions {
7375
public val nulls: Set<String>
7476

7577
public val skipTypes: Set<KType>
78+
79+
/**
80+
* Whether to allow parsing UUIDs to the experimental [kotlin.uuid.Uuid] type.
81+
* By default, this is false and UUIDs are not recognized.
82+
*
83+
* NOTE: Interacting with a [Uuid][Uuid] in your code might require
84+
* `@`[OptIn][OptIn]`(`[ExperimentalUuidApi][ExperimentalUuidApi]`::class)`.
85+
* In notebooks, add `-opt-in=kotlin.uuid.ExperimentalUuidApi` to the compiler arguments.
86+
*/
87+
public var parseExperimentalUuid: Boolean
7688
}
7789

7890
/**
@@ -101,6 +113,11 @@ public interface GlobalParserOptions {
101113
* @param skipTypes a set of types that should be skipped during parsing. Parsing will be attempted for all other types.
102114
* By default, it's an empty set. To skip all types except a specified one, use [convertTo] instead.
103115
* @param useFastDoubleParser whether to use [FastDoubleParser], defaults to `true`. Please report any issues you encounter.
116+
* @param parseExperimentalUuid whether to allow parsing UUIDs to the experimental [kotlin.uuid.Uuid] type.
117+
* By default, this is false and UUIDs are not recognized.
118+
* NOTE: Interacting with a [Uuid][Uuid] in your code might require
119+
* `@`[OptIn][OptIn]`(`[ExperimentalUuidApi][ExperimentalUuidApi]`::class)`.
120+
* In notebooks, add `-opt-in=kotlin.uuid.ExperimentalUuidApi` to the compiler arguments.
104121
*/
105122
public class ParserOptions(
106123
public val locale: Locale? = null,
@@ -110,8 +127,31 @@ public class ParserOptions(
110127
public val nullStrings: Set<String>? = null,
111128
public val skipTypes: Set<KType>? = null,
112129
public val useFastDoubleParser: Boolean? = null,
130+
public val parseExperimentalUuid: Boolean? = null,
113131
) {
114132

133+
/** For binary compatibility. */
134+
@Deprecated(
135+
message = PARSER_OPTIONS,
136+
level = DeprecationLevel.HIDDEN,
137+
)
138+
public constructor(
139+
locale: Locale? = null,
140+
dateTimeFormatter: DateTimeFormatter? = null,
141+
dateTimePattern: String? = null,
142+
nullStrings: Set<String>? = null,
143+
skipTypes: Set<KType>? = null,
144+
useFastDoubleParser: Boolean? = null,
145+
) : this(
146+
locale = locale,
147+
dateTimeFormatter = dateTimeFormatter,
148+
dateTimePattern = dateTimePattern,
149+
nullStrings = nullStrings,
150+
skipTypes = skipTypes,
151+
useFastDoubleParser = useFastDoubleParser,
152+
parseExperimentalUuid = null,
153+
)
154+
115155
/** For binary compatibility. */
116156
@Deprecated(
117157
message = PARSER_OPTIONS,
@@ -129,7 +169,31 @@ public class ParserOptions(
129169
nullStrings = nullStrings,
130170
skipTypes = null,
131171
useFastDoubleParser = null,
172+
parseExperimentalUuid = null,
173+
)
174+
175+
/** For binary compatibility. */
176+
@Deprecated(
177+
message = PARSER_OPTIONS_COPY,
178+
level = DeprecationLevel.HIDDEN,
132179
)
180+
public fun copy(
181+
locale: Locale? = this.locale,
182+
dateTimeFormatter: DateTimeFormatter? = this.dateTimeFormatter,
183+
dateTimePattern: String? = this.dateTimePattern,
184+
nullStrings: Set<String>? = this.nullStrings,
185+
skipTypes: Set<KType>? = this.skipTypes,
186+
useFastDoubleParser: Boolean? = this.useFastDoubleParser,
187+
): ParserOptions =
188+
ParserOptions(
189+
locale = locale,
190+
dateTimeFormatter = dateTimeFormatter,
191+
dateTimePattern = dateTimePattern,
192+
nullStrings = nullStrings,
193+
skipTypes = skipTypes,
194+
useFastDoubleParser = useFastDoubleParser,
195+
parseExperimentalUuid = null,
196+
)
133197

134198
/** For binary compatibility. */
135199
@Deprecated(
@@ -149,6 +213,7 @@ public class ParserOptions(
149213
nullStrings = nullStrings,
150214
skipTypes = skipTypes,
151215
useFastDoubleParser = useFastDoubleParser,
216+
parseExperimentalUuid = null,
152217
)
153218

154219
internal fun getDateTimeFormatter(): DateTimeFormatter? =
@@ -166,6 +231,7 @@ public class ParserOptions(
166231
nullStrings: Set<String>? = this.nullStrings,
167232
skipTypes: Set<KType>? = this.skipTypes,
168233
useFastDoubleParser: Boolean? = this.useFastDoubleParser,
234+
parseExperimentalUuid: Boolean? = this.parseExperimentalUuid,
169235
): ParserOptions =
170236
ParserOptions(
171237
locale = locale,
@@ -174,6 +240,7 @@ public class ParserOptions(
174240
nullStrings = nullStrings,
175241
skipTypes = skipTypes,
176242
useFastDoubleParser = useFastDoubleParser,
243+
parseExperimentalUuid = parseExperimentalUuid,
177244
)
178245

179246
override fun equals(other: Any?): Boolean {
@@ -188,6 +255,7 @@ public class ParserOptions(
188255
if (dateTimePattern != other.dateTimePattern) return false
189256
if (nullStrings != other.nullStrings) return false
190257
if (skipTypes != other.skipTypes) return false
258+
if (parseExperimentalUuid != other.parseExperimentalUuid) return false
191259

192260
return true
193261
}
@@ -199,11 +267,12 @@ public class ParserOptions(
199267
result = 31 * result + (dateTimePattern?.hashCode() ?: 0)
200268
result = 31 * result + (nullStrings?.hashCode() ?: 0)
201269
result = 31 * result + (skipTypes?.hashCode() ?: 0)
270+
result = 31 * result + (parseExperimentalUuid?.hashCode() ?: 0)
202271
return result
203272
}
204273

205274
override fun toString(): String =
206-
"ParserOptions(locale=$locale, dateTimeFormatter=$dateTimeFormatter, dateTimePattern=$dateTimePattern, nullStrings=$nullStrings, skipTypes=$skipTypes, useFastDoubleParser=$useFastDoubleParser)"
275+
"ParserOptions(locale=$locale, dateTimeFormatter=$dateTimeFormatter, dateTimePattern=$dateTimePattern, nullStrings=$nullStrings, skipTypes=$skipTypes, useFastDoubleParser=$useFastDoubleParser, parseExperimentalUuid=$parseExperimentalUuid)"
207276
}
208277

209278
/** Tries to parse a column of strings into a column of a different type.

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ internal object Parsers : GlobalParserOptions {
142142
override val skipTypes: Set<KType>
143143
get() = skipTypesSet
144144

145+
override var parseExperimentalUuid: Boolean = false
146+
145147
override fun addDateTimePattern(pattern: String) {
146148
formatters.add(DateTimeFormatter.ofPattern(pattern))
147149
}
@@ -180,6 +182,7 @@ internal object Parsers : GlobalParserOptions {
180182
.let { formatters.add(it) }
181183

182184
useFastDoubleParser = true
185+
parseExperimentalUuid = false
183186
_locale = null
184187
nullStrings.addAll(listOf("null", "NULL", "NA", "N/A"))
185188
}
@@ -428,6 +431,8 @@ internal object Parsers : GlobalParserOptions {
428431
}
429432
}
430433

434+
private 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}")
435+
431436
@OptIn(ExperimentalUuidApi::class)
432437
internal val parsersOrder = listOf(
433438
// Int
@@ -494,20 +499,25 @@ internal object Parsers : GlobalParserOptions {
494499
posixParserToDoubleWithOptions,
495500
// Boolean
496501
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}")
502+
// Uuid
503+
stringParserWithOptions<Uuid> { options ->
504+
val parser = { str: String ->
505+
val parseExperimentalUuid = options?.parseExperimentalUuid ?: this.parseExperimentalUuid
506+
when {
507+
!parseExperimentalUuid -> null
508+
509+
uuidRegex.matches(str) -> {
510+
try {
511+
Uuid.parse(str)
512+
} catch (_: IllegalArgumentException) {
513+
null
514+
}
515+
}
501516

502-
if (uuidRegex.matches(str)) {
503-
try {
504-
Uuid.parse(str)
505-
} catch (e: IllegalArgumentException) {
506-
null
517+
else -> null
507518
}
508-
} else {
509-
null
510519
}
520+
parser
511521
},
512522
// BigInteger
513523
stringParser<BigInteger> { it.toBigIntegerOrNull() },

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,20 @@ class ParseTests {
489489
fun `parse valid Uuid`() {
490490
val validUUID = "550e8400-e29b-41d4-a716-446655440000"
491491
val column by columnOf(validUUID)
492+
val parsed = column.parse(ParserOptions(parseExperimentalUuid = true))
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 valid Uuid with GlobalParserOptions`() {
501+
val validUUID = "550e8400-e29b-41d4-a716-446655440000"
502+
val column by columnOf(validUUID)
503+
DataFrame.parser.parseExperimentalUuid = true
492504
val parsed = column.parse()
505+
DataFrame.parser.resetToDefault()
493506

494507
parsed.type() shouldBe typeOf<Uuid>()
495508
(parsed[0] as Uuid).toString() shouldBe validUUID // Change UUID to Uuid
@@ -500,6 +513,20 @@ class ParseTests {
500513
fun `parse invalid Uuid`() {
501514
val invalidUUID = "this is not a UUID"
502515
val column = columnOf(invalidUUID)
516+
// tryParse as string is not formatted.
517+
val parsed = column.tryParse(
518+
ParserOptions(parseExperimentalUuid = true),
519+
)
520+
521+
parsed.type() shouldNotBe typeOf<Uuid>()
522+
parsed.type() shouldBe typeOf<String>()
523+
}
524+
525+
@OptIn(ExperimentalUuidApi::class)
526+
@Test
527+
fun `do not parse Uuid by default`() {
528+
val validUUID = "550e8400-e29b-41d4-a716-446655440000"
529+
val column = columnOf(validUUID)
503530
val parsed = column.tryParse() // tryParse as string is not formatted.
504531

505532
parsed.type() shouldNotBe typeOf<Uuid>()

0 commit comments

Comments
 (0)