/
types.go
55 lines (45 loc) · 981 Bytes
/
types.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
package modelcollections
import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/google/uuid"
)
type CollectionID uuid.UUID
var (
_ fmt.Stringer = CollectionID{}
)
func (c CollectionID) String() string {
// TODO maybe add some prefix like "c:"
return uuid.UUID(c).String()
}
var (
_ driver.Valuer = CollectionID{}
_ sql.Scanner = &CollectionID{}
)
func (c CollectionID) Value() (driver.Value, error) {
return c.String(), nil
}
func (c *CollectionID) Scan(value interface{}) error {
_, isStringType := value.(string)
if !isStringType {
return fmt.Errorf("db.model: invalid type, expect string")
}
cid, err := Parse(value.(string))
if err != nil {
return err
}
(*c) = cid
return nil
}
func NewCollectionID() CollectionID {
uid, _ := uuid.NewV7()
return CollectionID(uid)
}
func Parse(s string) (CollectionID, error) {
innerUuid, err := uuid.Parse(s)
if err != nil {
return CollectionID{}, err
}
return CollectionID(innerUuid), nil
}