aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-03-14 01:42:44 +0700
committerShulhan <ms@kilabit.info>2026-03-26 02:10:56 +0700
commit03d4328eb4db5e80fe8ad1df14c3f73bc33c9190 (patch)
tree87c3efda2377855ee45e740144f46ac7a6dd907d
parent364af4dae98ec9a34a44a8468bc2da2d3ce3d988 (diff)
downloadpakakeh.go-03d4328eb4db5e80fe8ad1df14c3f73bc33c9190.tar.xz
lib/uuidv7: implement Equal method for [lib/reflect.Equaler]
The Equal method returns nil if both receiver and parameter has the same value. This method implements [lib/reflect.Equaler] interface.
-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 {