Skip to content

feat(ulid): add isZero() func for 0 value comparaison - #109

Closed
Chroq wants to merge 2 commits into
oklog:mainfrom
Chroq:feat_add_ulid_nil_for_easier_comparaison
Closed

Chroq wants to merge 2 commits into
oklog:mainfrom
Chroq:feat_add_ulid_nil_for_easier_comparaison

Conversation

@Chroq

@Chroq Chroq commented Dec 8, 2023

Copy link
Copy Markdown

@Chroq
Chroq force-pushed the feat_add_ulid_nil_for_easier_comparaison branch from c3fc077 to 0ca1a54 Compare December 8, 2023 09:36
@Chroq

Chroq commented Dec 8, 2023

Copy link
Copy Markdown
Author

Hello, I'd like to suggest a minor modification that would be rather practical. The aim is to be able to easily distinguish between 0 ULIDs. It's the same principle as for the UUID library (https://github.com/google/uuid).

Comment thread ulid.go Outdated
type ULID [16]byte

var (
Nil ULID // empty ULID, all zeros

@peterbourgon peterbourgon Dec 8, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A ULID that's all zeroes is AFAIK valid, and so isn't really accurately called a "nil" ULID. At a minimum, this should be called Zero ULID. Even then, I'm not sure what purpose this definition would serve. Any application that accepts a ulid.ULID shouldn't in general treat a Zero ULID differently than any other ULID. Do you have some use case that would indicate otherwise?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

+1 for naming, Zero (and maybe a isZero() getter as in time library) will be better. I've several usecases where ULIDs are not already set and I avoid a lot of using pointers in my domain structs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's important to understand that var Zero ulid.ULID — which is ultimately a [16]byte of all-zeroes — is a completely valid ULID value, and cannot be treated as any kind of "nil" or otherwise invalid ULID value. If you want to distinguish invalid vs. valid ULID values, you cannot model them as specific values of the ulid.ULID type.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I understand and agree completely. In fact, I don't have any use cases where a zero-filled ULID is valid (they all start well after timestamp 0). So you recommend using a pointer to be totally sure of initialization ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If your application wants to treat a zero value ULID as invalid, no problem! I'm just not sure about treating this specific ULID value as special in the actual module.

@Chroq Chroq changed the title feat(ulid): add Nil var for full 0 easier comparaison feat(ulid): add Zero var for full 0 easier comparaison Dec 11, 2023
@Chroq Chroq changed the title feat(ulid): add Zero var for full 0 easier comparaison feat(ulid): add isZero() func for 0 value comparaison Dec 11, 2023
Comment thread ulid.go
type ULID [16]byte

var (
Zero ULID // empty ULID, all zeros

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

AFAIK this is a completely valid ULID, so it can't be treated as a distinct/invalid value by consumers — right? Do you disagree?

@peterbourgon

Copy link
Copy Markdown
Member

The UUID RFC defines an all-zero UUID as a special case, which it calls a nil UUID. But ULID doesn't define an all-zero ULID in any special way, to be an e.g. nil ULID, AFAIK.

@Chroq

Chroq commented Dec 13, 2023

Copy link
Copy Markdown
Author

Ok, with your permission, I'll close this PR, it's seems to be out of the scope of this module :)

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