aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/uuidv7/uuidv7.go21
-rw-r--r--lib/uuidv7/uuidv7_example_test.go19
2 files changed, 40 insertions, 0 deletions
diff --git a/lib/uuidv7/uuidv7.go b/lib/uuidv7/uuidv7.go
index 5a6b6e03..6870b562 100644
--- a/lib/uuidv7/uuidv7.go
+++ b/lib/uuidv7/uuidv7.go
@@ -77,6 +77,27 @@ func (id UUIDv7) Bytes() (data []byte) {
return data
}
+// Equal returns nil if id and v are equals.
+// This method implements [git.sr.ht/~shulhan/pakakeh.go/lib/reflect.Equaler]
+// interface.
+func (id *UUIDv7) Equal(v any) (err error) {
+ ptr, ok := v.(*UUIDv7)
+ if !ok {
+ other, ok := v.(UUIDv7)
+ if !ok {
+ return fmt.Errorf(`uuidv7: Equal: want type %T, got %T`, &id, v)
+ }
+ ptr = &other
+ }
+ if id.high != ptr.high {
+ return fmt.Errorf(`uuidv7: not equal, want %s, got %s`, id.String(), ptr.String())
+ }
+ if id.low != ptr.low {
+ return fmt.Errorf(`uuidv7: not equal, want %s, got %s`, id.String(), ptr.String())
+ }
+ return nil
+}
+
// IsZero returns true if all bits is zero.
func (id UUIDv7) IsZero() bool {
return id.high == 0 && id.low == 0
diff --git a/lib/uuidv7/uuidv7_example_test.go b/lib/uuidv7/uuidv7_example_test.go
index c0472dd5..df007698 100644
--- a/lib/uuidv7/uuidv7_example_test.go
+++ b/lib/uuidv7/uuidv7_example_test.go
@@ -46,6 +46,25 @@ func ExampleParse() {
// false 019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF 2026-03-09 14:20:01.123 +0000 UTC
}
+func ExampleUUIDv7_Equal() {
+ id := uuidv7.Parse(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF`)
+ id2 := uuidv7.Parse(`019CD2F8-2AE3-774E-BFFF-FFFFFFFFFFFF`)
+
+ err := id.Equal(id)
+ fmt.Printf("%v\n", err)
+
+ err = id.Equal(&id)
+ fmt.Printf("%v\n", err)
+
+ err = id.Equal(&id2)
+ fmt.Printf("%v\n", err)
+
+ // Output:
+ // <nil>
+ // <nil>
+ // uuidv7: not equal, want 019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF, got 019CD2F8-2AE3-774E-BFFF-FFFFFFFFFFFF
+}
+
func ExampleUUIDv7_MarshalBinary() {
now := time.Date(2026, 3, 9, 14, 20, 0, 123456700, time.UTC)
uuidv7.Now = func() time.Time {