Skip to content

Commit 153266a

Browse files
Copilotcameracker
andauthored
Add IsZero method to UUID type for MongoDB omitzero support (#226)
* Initial plan * Add IsZero method to UUID type for MongoDB omitzero support Co-authored-by: cameracker <[email protected]> * Expand IsZero documentation to reference bsoncodec.Zeroer interface and GitHub issue Co-authored-by: cameracker <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: cameracker <[email protected]> Co-authored-by: Cameron Ackerman <[email protected]>
1 parent 90a218b commit 153266a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

uuid.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ func (u UUID) IsNil() bool {
192192
return u == Nil
193193
}
194194

195+
// IsZero returns if the UUID is equal to the zero UUID (same as IsNil).
196+
// This method is provided to satisfy the bsoncodec.Zeroer interface for MongoDB
197+
// omitzero tag support. See: https://github.com/gofrs/uuid/issues/224
198+
func (u UUID) IsZero() bool {
199+
return u == Nil
200+
}
201+
195202
// Version returns the algorithm version used to generate the UUID.
196203
func (u UUID) Version() byte {
197204
return u[6] >> 4

uuid_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
func TestUUID(t *testing.T) {
3333
t.Run("IsNil", testUUIDIsNil)
34+
t.Run("IsZero", testUUIDIsZero)
3435
t.Run("Bytes", testUUIDBytes)
3536
t.Run("String", testUUIDString)
3637
t.Run("Version", testUUIDVersion)
@@ -49,6 +50,24 @@ func testUUIDIsNil(t *testing.T) {
4950
}
5051
}
5152

53+
func testUUIDIsZero(t *testing.T) {
54+
// Test zero UUID
55+
u := UUID{}
56+
got := u.IsZero()
57+
want := true
58+
if got != want {
59+
t.Errorf("%v.IsZero() = %t, want %t", u, got, want)
60+
}
61+
62+
// Test non-zero UUID
63+
nonZeroUUID := NamespaceDNS // Use a known non-zero UUID
64+
got = nonZeroUUID.IsZero()
65+
want = false
66+
if got != want {
67+
t.Errorf("%v.IsZero() = %t, want %t", nonZeroUUID, got, want)
68+
}
69+
}
70+
5271
func testUUIDBytes(t *testing.T) {
5372
got := codecTestUUID.Bytes()
5473
want := codecTestData

0 commit comments

Comments
 (0)