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

Add "AtTime" generators for V1, V6, and V7 #142

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

Conversation

kohenkatz
Copy link
Contributor

This PR implements the request in #84 to be able to create UUIDs at specific known timestamps, in addition to the current time.

Although that request only mentions UUIDv1, this PR also includes the other two time-based UUID versions, namely v6 and v7.

To prevent code duplication, this PR reimplements NewV1, NewV6, and NewV7 to each call their respective NewV<X>AtTime with the provided EpochFunc, which defaults to providing the current time.

This PR will remain a draft until I have time to write some tests...

Copy link

codecov bot commented May 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (22c52c2) to head (6ca3d15).
Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #142   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         5    +1     
  Lines          513       453   -60     
=========================================
- Hits           513       453   -60     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cameracker
Copy link
Collaborator

cameracker commented May 13, 2024

Nice. Looks good so far. I think it was the right call to add some additional functions. As a mega nit, what do you think of the convention NewVNFromTime()?

@cameracker cameracker self-requested a review May 13, 2024 02:26
@kohenkatz
Copy link
Contributor Author

... what do you think of the convention NewVNFromTime()?

Personally, I like At over From for three reasons:

  1. To me, "From" implies some kind of derivation/modification, while "At" implies that the time is used exactly as-is.
  2. I am used to other frameworks that use At as an indicator that something is a timestamp. For example, Laravel's created_at, updated_at, and deleted_at database fields.
  3. It's slightly shorter to type.

All that said, I really don't care if you want to change it.

@@ -360,7 +373,7 @@ func (g *Gen) NewV7() (UUID, error) {
// When useUnixTSMs is false, it uses the Coordinated Universal Time (UTC) as a count of 100-
//
// nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to the Christian calendar).
func (g *Gen) getClockSequence(useUnixTSMs bool) (uint64, uint16, error) {
func (g *Gen) getClockSequence(useUnixTSMs bool, atTime time.Time) (uint64, uint16, error) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: The doc string for this method needs to be updated to reflect the new behavior

Unrelated to this PR, but it seems like the formatting of the comment is off. I don't think there's supposed to be a paragraph break between "100-" and "nanosecond".

@@ -418,8 +431,8 @@ func (g *Gen) getHardwareAddr() ([]byte, error) {

// Returns the difference between UUID epoch (October 15, 1582)
// and current time in 100-nanosecond intervals.
func (g *Gen) getEpoch() uint64 {
return epochStart + uint64(g.epochFunc().UnixNano()/100)
func (g *Gen) getEpoch(atTime time.Time) uint64 {
Copy link
Member

Choose a reason for hiding this comment

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

Same nit here. Please update the doc string to reflect the new behavior.

@cameracker
Copy link
Collaborator

@kohenkatz Dependabot updated the go-setup github action, and I noted we were testing against older versions of go so I took the opportunity to kick them up to latest and latest -1. Might be worth rebasing against master :)

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.

None yet

3 participants