diff options
| -rw-r--r-- | lib/uuidv7/example_test.go (renamed from lib/uuidv7/uuidv7_example_test.go) | 16 | ||||
| -rw-r--r-- | lib/uuidv7/uuid.go (renamed from lib/uuidv7/uuidv7.go) | 46 | ||||
| -rw-r--r-- | lib/uuidv7/uuid_test.go (renamed from lib/uuidv7/uuidv7_test.go) | 6 |
3 files changed, 34 insertions, 34 deletions
diff --git a/lib/uuidv7/uuidv7_example_test.go b/lib/uuidv7/example_test.go index 5a71d69e..86ae7aab 100644 --- a/lib/uuidv7/uuidv7_example_test.go +++ b/lib/uuidv7/example_test.go @@ -47,7 +47,7 @@ func ExampleParse() { // false 019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF 2026-03-09 14:20:01.123 +0000 UTC } -func ExampleUUIDv7_Equal() { +func ExampleUUID_Equal() { id := uuidv7.Parse(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF`) id2 := uuidv7.Parse(`019CD2F8-2AE3-774E-BFFF-FFFFFFFFFFFF`) @@ -66,7 +66,7 @@ func ExampleUUIDv7_Equal() { // uuidv7: not equal, want 019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF, got 019CD2F8-2AE3-774E-BFFF-FFFFFFFFFFFF } -func ExampleUUIDv7_IsEqual() { +func ExampleUUID_IsEqual() { id := uuidv7.Parse(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF`) other := uuidv7.Parse(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFF0`) @@ -78,7 +78,7 @@ func ExampleUUIDv7_IsEqual() { // false } -func ExampleUUIDv7_MarshalBinary() { +func ExampleUUID_MarshalBinary() { now := time.Date(2026, 3, 9, 14, 20, 0, 123456700, time.UTC) uuidv7.Now = func() time.Time { now = now.Add(time.Second) @@ -98,7 +98,7 @@ func ExampleUUIDv7_MarshalBinary() { fmt.Printf("Sent: %s\n", id) dec := gob.NewDecoder(&buf) - var gotID uuidv7.UUIDv7 + var gotID uuidv7.UUID err = dec.Decode(&gotID) if err != nil { log.Fatal(err) @@ -110,7 +110,7 @@ func ExampleUUIDv7_MarshalBinary() { // Receive: 019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF } -func ExampleUUIDv7_MarshalText() { +func ExampleUUID_MarshalText() { now := time.Date(2026, 3, 9, 14, 20, 0, 123456700, time.UTC) uuidv7.Now = func() time.Time { now = now.Add(time.Second) @@ -121,7 +121,7 @@ func ExampleUUIDv7_MarshalText() { } type T struct { - ID uuidv7.UUIDv7 + ID uuidv7.UUID } t := T{ @@ -145,8 +145,8 @@ func ExampleUUIDv7_MarshalText() { // {ID:019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF} } -func ExampleUUIDv7_Scan() { - var id uuidv7.UUIDv7 +func ExampleUUID_Scan() { + var id uuidv7.UUID // Scan the value from the database: // diff --git a/lib/uuidv7/uuidv7.go b/lib/uuidv7/uuid.go index 5aac01bb..203b41cf 100644 --- a/lib/uuidv7/uuidv7.go +++ b/lib/uuidv7/uuid.go @@ -31,9 +31,9 @@ var Rand = func() (v uint64) { return v } -// UUIDv7 defines the container for UUID version 7 that satisfy the +// UUID defines the container for UUID version 7 that satisfy the // [database/sql], [encoding/gob], and [encoding/json]. -type UUIDv7 struct { +type UUID struct { // The high 8 octets store the unix timestamp in milliseconds (6 // octets), the version (4 bits), and clock precision (12 bits). high uint64 @@ -44,13 +44,13 @@ type UUIDv7 struct { } // Generate generates new UUID version 7. -func Generate() (id UUIDv7) { +func Generate() (id UUID) { now := Now() generate(&now, &id) return id } -func generate(t *time.Time, id *UUIDv7) { +func generate(t *time.Time, id *UUID) { unixns := uint64(t.UnixNano()) // 1773066002123456700 millis := uint64(unixns / 1e6) // 1773066002123 id.high = millis << 16 // 0x019c_d2f8_1ecb_0000 @@ -63,15 +63,15 @@ func generate(t *time.Time, id *UUIDv7) { id.low = variantMask | rand // 0xbfff_ffff_ffff_ffff } -// Parse parses the UUIDv7 formated string and return it as non-zero id. -// If the string s is invalid UUIDv7 it will return zero id. -func Parse(s string) (id UUIDv7) { +// Parse parses the UUID formated string and return it as non-zero id. +// If the string s is invalid UUID it will return zero id. +func Parse(s string) (id UUID) { id.UnmarshalText([]byte(s)) return id } // Bytes returns id as a stream of binary. -func (id UUIDv7) Bytes() (data []byte) { +func (id UUID) Bytes() (data []byte) { data = make([]byte, 0, binary.MaxVarintLen64*2) data = binary.BigEndian.AppendUint64(data, id.high) data = binary.BigEndian.AppendUint64(data, id.low) @@ -81,10 +81,10 @@ func (id UUIDv7) Bytes() (data []byte) { // 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) +func (id *UUID) Equal(v any) (err error) { + ptr, ok := v.(*UUID) if !ok { - other, ok := v.(UUIDv7) + other, ok := v.(UUID) if !ok { return fmt.Errorf(`uuidv7: Equal: want type %T, got %T`, &id, v) } @@ -100,29 +100,29 @@ func (id *UUIDv7) Equal(v any) (err error) { } // IsEqual returns true if id equal with other. -func (id UUIDv7) IsEqual(other UUIDv7) bool { +func (id UUID) IsEqual(other UUID) bool { return id.high == other.high && id.low == other.low } // IsZero returns true if all bits is zero. -func (id UUIDv7) IsZero() bool { +func (id UUID) IsZero() bool { return id.high == 0 && id.low == 0 } // MarshalBinary encodes the id to binary for [encoding/gob]. -func (id UUIDv7) MarshalBinary() (data []byte, err error) { +func (id UUID) MarshalBinary() (data []byte, err error) { data = id.Bytes() return data, nil } // MarshalText encodes the id to JSON for [encoding/json]. -func (id UUIDv7) MarshalText() (data []byte, err error) { +func (id UUID) MarshalText() (data []byte, err error) { v := id.String() return []byte(v), nil } // UnmarshalBinary decodes the data into id for [encoding/gob]. -func (id *UUIDv7) UnmarshalBinary(data []byte) (err error) { +func (id *UUID) UnmarshalBinary(data []byte) (err error) { if len(data) != 16 { return fmt.Errorf(`uuidv7: invalid length %d`, len(data)) } @@ -136,7 +136,7 @@ func (id *UUIDv7) UnmarshalBinary(data []byte) (err error) { } // UnmarshalText decodes the JSON data into id for [encoding/json]. -func (id *UUIDv7) UnmarshalText(data []byte) (err error) { +func (id *UUID) UnmarshalText(data []byte) (err error) { src := make([]byte, 0, 32) for _, c := range data { if c == '-' { @@ -172,7 +172,7 @@ func (id *UUIDv7) UnmarshalText(data []byte) (err error) { // Scan scans the raw database value into id. // This method implement [database/sql.Scanner] interface. // Column with NULL value will returns no error set it to zero UUID. -func (id *UUIDv7) Scan(src any) (err error) { +func (id *UUID) Scan(src any) (err error) { switch v := src.(type) { case []byte: err = id.UnmarshalText(v) @@ -189,9 +189,9 @@ func (id *UUIDv7) Scan(src any) (err error) { return nil } -// String returns the human readable format of UUIDv7 in the form of +// String returns the human readable format of UUID in the form of // "0xxxxxxx-xxxx-7xxx-8xxx-xxxxxxxxxxxx". -func (id UUIDv7) String() string { +func (id UUID) String() string { buf := make([]byte, 36) sw := 56 x := 0 @@ -223,8 +223,8 @@ func (id UUIDv7) String() string { return string(buf) } -// Time returns the Unix epoch timestamp in milliseconds stored in the UUIDv7. -func (id *UUIDv7) Time() time.Time { +// Time returns the Unix epoch timestamp in milliseconds stored in the UUID. +func (id *UUID) Time() time.Time { millis := id.high >> 16 t := time.UnixMilli(int64(millis)) return t @@ -232,7 +232,7 @@ func (id *UUIDv7) Time() time.Time { // Value returns the value for sending it to the database. // This method implements the [driver.Valuer] interface. -func (id UUIDv7) Value() (v driver.Value, err error) { +func (id UUID) Value() (v driver.Value, err error) { v = id.String() return v, nil } diff --git a/lib/uuidv7/uuidv7_test.go b/lib/uuidv7/uuid_test.go index 2116b045..126945d3 100644 --- a/lib/uuidv7/uuidv7_test.go +++ b/lib/uuidv7/uuid_test.go @@ -9,7 +9,7 @@ import ( "git.sr.ht/~shulhan/pakakeh.go/lib/test" ) -func TestUUIDv7_Scan(t *testing.T) { +func TestUUID_Scan(t *testing.T) { id := Parse(`019B76DA-A800-7000-8000-00000000001A`) listCase := []struct { @@ -42,7 +42,7 @@ func TestUUIDv7_Scan(t *testing.T) { } } -func TestUUIDv7_UnmarshalBinary(t *testing.T) { +func TestUUID_UnmarshalBinary(t *testing.T) { id := Parse(`019B76DA-A800-7000-8000-00000000001A`) listCase := []struct { @@ -77,7 +77,7 @@ func TestUUIDv7_UnmarshalBinary(t *testing.T) { } } -func TestUUIDv7_UnmarshalText(t *testing.T) { +func TestUUID_UnmarshalText(t *testing.T) { id := Parse(`019B76DA-A800-7000-8000-00000000001A`) listCase := []struct { |
