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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

EmmanuelBerkowicz
Copy link

@EmmanuelBerkowicz EmmanuelBerkowicz commented Jun 29, 2025

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/../parse.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)

## Summary
Implements UUID parsing support for DataColumn.parse() as per open issue Kotlin#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)
@Jolanrensen Jolanrensen self-requested a review June 30, 2025 09:42
@EmmanuelBerkowicz
Copy link
Author

Hey @Jolanrensen,
I'm still pretty new to open source, is there an issue?

Copy link
Collaborator

@Jolanrensen Jolanrensen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your addition! :)

Aside from my comment, I'd also reccomend running the linter task "ktlintFormat" across the project. This will fix some newlines and indentations, so they're consistent with the rest of the project.
Our CI runs "build" with all tests and "ktlintCheck", which now fails.

stringParser<UUID> {str ->
try{
UUID.fromString(str)
} catch(e: IllegalArgumentException){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this approach works, the parse() function can make this piece of code be called very often (if you try to parse a column with 100 Strings, it gets called 100 times). Exceptions are meant for exceptional cases, not for default flows, as they are quite heavy to throw (a stacktrace needs to be built every time).
For this reason, we tried to avoid throwing exceptions for the other types we can parse, like dates etc. I think for UUIDs we can do the same thing :)

We could namely check the string by a Regex first, return null when the regex doesn't match, and call UUID.fromString() only when the regex does match. You can keep the try {} catch() around it for safety of course. This will save a lot of stack traces being created :)

I found this 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}" in several places online; I think it's safe to use it, but if you're unsure, please add some more tests.

@Jolanrensen
Copy link
Collaborator

Jolanrensen commented Jun 30, 2025

Hey @Jolanrensen, I'm still pretty new to open source, is there an issue?

No issue :) I self-requested a review so I can comment on some particular parts of the PR. If you correct it and our test suite and the team is okay with it, we can merge it to the master

@Jolanrensen
Copy link
Collaborator

Jolanrensen commented Jun 30, 2025

Actually, as my collegue just pointed out to me, the idea was to use Kotlin's new experimental builtin Uuid class https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.uuid/ instead of the Java variant.

It works very similarly but is more Kotlinlike :). Feel free to OptIn to @ExperimentalUuidApi, I'll add a ParserOption for our users to let them decide whether they want to use the experimental UUID parser or not after you finish this part.

You can use the same Regex approach I mentioned, but as it's Kotlin itself, I'll also ask whether they can create a parseOrNull() function without exceptions :)

@EmmanuelBerkowicz
Copy link
Author

Thanks for the feedback @Jolanrensen !

I'll work on this when I get home and update!

Emmanuel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants