-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid.go
77 lines (60 loc) · 1.32 KB
/
uuid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package uuid
import (
"database/sql/driver"
"encoding/hex"
"errors"
"fmt"
"strings"
)
var Nil UUID
type UUID [16]byte
func Parse(str string) (UUID, error) {
if len(str) != 36 {
return Nil, errors.New("UUID string must be 36 bytes in length")
}
if !validV7.MatchString(str) && !validV4.MatchString(str) {
return Nil, errors.New("invalid UUID format")
}
if str == Nil.String() {
return Nil, errors.New("UUID string is nil")
}
str = strings.ReplaceAll(str, "-", "")
decoded, err := hex.DecodeString(str)
if err != nil {
return Nil, fmt.Errorf("decode hex id string: %w", err)
}
return UUID(*(*[16]byte)(decoded)), nil
}
func (id UUID) IsNil() bool {
return id == Nil
}
func (id UUID) String() string {
return fmt.Sprintf("%x-%x-%x-%x-%x", id[:4], id[4:6], id[6:8], id[8:10], id[10:])
}
func (id *UUID) Scan(src any) error {
switch src := src.(type) {
case nil:
*id = Nil
case string:
u, err := Parse(src)
if err != nil {
return fmt.Errorf("parse %T: %w", src, err)
}
*id = u
case []byte:
u, err := Parse(string(src))
if err != nil {
return fmt.Errorf("parse %T: %w", src, err)
}
*id = u
default:
return fmt.Errorf("unable to scan %T into UUID", src)
}
return nil
}
func (id UUID) Value() (driver.Value, error) {
if id.IsNil() {
return nil, nil
}
return id.String(), nil
}