diff options
| -rw-r--r-- | lib/uuidv7/uuidv7.go | 7 | ||||
| -rw-r--r-- | lib/uuidv7/uuidv7_test.go | 36 |
2 files changed, 34 insertions, 9 deletions
diff --git a/lib/uuidv7/uuidv7.go b/lib/uuidv7/uuidv7.go index 21277e55..e2185c24 100644 --- a/lib/uuidv7/uuidv7.go +++ b/lib/uuidv7/uuidv7.go @@ -153,19 +153,20 @@ func (id *UUIDv7) UnmarshalText(data []byte) (err error) { if err != nil { return fmt.Errorf(`uuidv7: %w`, err) } - id.high = binary.BigEndian.Uint64(dst) + high := binary.BigEndian.Uint64(dst) _, err = hex.Decode(dst, src[16:]) if err != nil { return fmt.Errorf(`uuidv7: %w`, err) } + id.high = high id.low = binary.BigEndian.Uint64(dst) return nil } // Scan scans the raw database value into id. // This method implement [database/sql.Scanner] interface. -// Column with NULL value will returns no error but zero UUID. +// Column with NULL value will returns no error set it to zero UUID. func (id *UUIDv7) Scan(src any) (err error) { switch v := src.(type) { case []byte: @@ -174,6 +175,8 @@ func (id *UUIDv7) Scan(src any) (err error) { return err } case nil: + id.high = 0 + id.low = 0 return nil default: return fmt.Errorf(`uuidv7: Scan: invalid type %T`, src) diff --git a/lib/uuidv7/uuidv7_test.go b/lib/uuidv7/uuidv7_test.go index f9d90226..2116b045 100644 --- a/lib/uuidv7/uuidv7_test.go +++ b/lib/uuidv7/uuidv7_test.go @@ -10,84 +10,106 @@ import ( ) func TestUUIDv7_Scan(t *testing.T) { + id := Parse(`019B76DA-A800-7000-8000-00000000001A`) + listCase := []struct { desc string data any expError string - exp UUIDv7 + exp string }{{ desc: `With empty data`, data: `017F22E2-79B0-7CC3-98C4-DC0C0C07398F`, expError: `uuidv7: Scan: invalid type string`, + exp: `019B76DA-A800-7000-8000-00000000001A`, }, { desc: `With invalid version`, data: []byte(`5c146b14-3c52-4afd-938a-375d0df1fbf6`), expError: `uuidv7: invalid version 2`, + exp: `019B76DA-A800-7000-8000-00000000001A`, }, { desc: `With nil`, data: nil, expError: ``, + exp: `00000000-0000-0000-0000-000000000000`, }} for _, tc := range listCase { - var id UUIDv7 err := id.Scan(tc.data) if err != nil { test.Assert(t, tc.desc, tc.expError, err.Error()) - continue } + test.Assert(t, tc.desc, tc.exp, id.String()) } } func TestUUIDv7_UnmarshalBinary(t *testing.T) { + id := Parse(`019B76DA-A800-7000-8000-00000000001A`) + listCase := []struct { desc string expError string + exp string data []byte }{{ desc: `With empty data`, expError: `uuidv7: invalid length 0`, + exp: `019B76DA-A800-7000-8000-00000000001A`, }, { desc: `With non-version 7`, data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 1, 2, 3, 4}, expError: `uuidv7: invalid version 0`, + exp: `019B76DA-A800-7000-8000-00000000001A`, + }, { + desc: `OK`, + data: []byte{0xf, 0xf, 0xf, 0xf, + 0xf, 0xf, + 0x70, 0xf, + 0xf, 0xf, + 0xf, 0xf, 0xf, 0xf, 0xf, 0xf}, + exp: `0F0F0F0F-0F0F-700F-0F0F-0F0F0F0F0F0F`, }} for _, tc := range listCase { - var id UUIDv7 err := id.UnmarshalBinary(tc.data) if err != nil { test.Assert(t, tc.desc, tc.expError, err.Error()) - continue } + test.Assert(t, tc.desc, tc.exp, id.String()) } } func TestUUIDv7_UnmarshalText(t *testing.T) { + id := Parse(`019B76DA-A800-7000-8000-00000000001A`) + listCase := []struct { desc string expError string + exp string data []byte }{{ desc: `With empty data`, expError: `uuidv7: invalid length 0`, + exp: `019B76DA-A800-7000-8000-00000000001A`, }, { desc: `With non-version 7`, data: []byte(`5c146b14-3c52-4afd-938a-375d0df1fbf6`), expError: `uuidv7: invalid version 2`, + exp: `019B76DA-A800-7000-8000-00000000001A`, }, { desc: `With invalid hex (high)`, data: []byte(`X17F22E2-79B0-7CC3-98C4-DC0C0C07398F`), expError: `uuidv7: encoding/hex: invalid byte: U+0058 'X'`, + exp: `019B76DA-A800-7000-8000-00000000001A`, }, { desc: `With invalid hex (low)`, data: []byte(`017F22E2-79B0-7CC3-98C4-DC0C0C07398X`), expError: `uuidv7: encoding/hex: invalid byte: U+0058 'X'`, + exp: `019B76DA-A800-7000-8000-00000000001A`, }} for _, tc := range listCase { - var id UUIDv7 err := id.UnmarshalText(tc.data) if err != nil { test.Assert(t, tc.desc, tc.expError, err.Error()) - continue } + test.Assert(t, tc.desc, tc.exp, id.String()) } } |
