Skip to content
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

feat: Validate UUID without creating new UUID #141

Merged

Conversation

ljahier
Copy link
Contributor

@ljahier ljahier commented Dec 4, 2023

Overview

This pull request introduces a new function, Validate(s string) error, in response to the request in issue #137. The primary goal is to provide a way to validate UUID strings without the overhead of creating a UUID object and its underlying byte array, as currently happens in the Parse(s string) (UUID, error) function.

Changes Made

  • Implemented Validate(s string) error which checks the format of the UUID string without generating a UUID object.
  • Added unit tests to ensure the correctness of the validation logic, particularly for edge cases and various UUID string formats.

Rationale

This change is particularly useful in scenarios where UUIDs are frequently validated but not used in their byte form. It optimizes performance by eliminating unnecessary memory allocations associated with UUID object creation, especially in high-throughput environments.

Testing

Unit tests have been added to cover various cases:

  • Standard UUID formats (with and without hyphens)
  • UUIDs with the "urn:uuid:" prefix
  • Braced UUID strings
  • Edge cases with invalid UUID strings

The tests ensure that the new validation function behaves as expected across a wide range of valid and invalid inputs.

Impact

This update introduces a non-breaking change, adding new functionality to the library without altering existing behaviors.

Looking forward to your feedback and suggestions on this implementation!

Resolves #137

@ljahier ljahier requested a review from a team as a code owner December 4, 2023 15:32
Copy link

google-cla bot commented Dec 4, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@ljahier ljahier force-pushed the feat_validate_uuid_without_creating_new branch from 0d6d439 to 3394eb7 Compare December 4, 2023 15:42
Copy link
Collaborator

@bormanp bormanp left a comment

Choose a reason for hiding this comment

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

Only the comment needs fixing.

uuid.go Outdated
@@ -186,6 +186,55 @@ func Must(uuid UUID, err error) UUID {
return uuid
}

// Validate checks if the input string is a properly formatted UUID in various recognized formats.
Copy link
Collaborator

Choose a reason for hiding this comment

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

// Validate returns an error if s is not a properly formatted UUID in one of the following formats:
//   xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
//   urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
//   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//   {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ✅

@bormanp bormanp merged commit 9ee7366 into google:master Dec 12, 2023
6 checks passed
hslatman added a commit to smallstep/cli that referenced this pull request Dec 19, 2023
In google/uuid#141 a new `Validate` function
was added to the `uuid` package. This can be used when no UUID
struct is required, so can be used instead of `Parse`.

This takes #1087, but uses
`uuid.Validate` in one location.
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.

Validate UUID without creating UUID (and underlying byte array)
2 participants