-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid.go
64 lines (51 loc) · 1.08 KB
/
id.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
package stream
import (
"fmt"
"strings"
)
type ID struct {
uuid UUID
typ Type
}
func NewID[T any](s string) (d ID, err error) {
if d.uuid = NewUUID(s); d.uuid.IsEmpty() {
return d, Err("id uuid is empty")
}
if d.typ, err = NewType[T](); err != nil {
return d, Err("creating id type failed %w", err)
}
return d, nil
}
func ParseID(s string) (ID, error) {
var p []string
var id ID
if p = strings.Split(s, "."); len(p) < 2 {
return id, Err("parse id `%s` invalid format, please use <id>.<type> ie `N8hY13fsd.Chat`", s)
}
id.uuid, id.typ = NewUUID(p[0]), Type(p[1])
return id, nil
}
func (id ID) UUID() UUID {
return id.uuid
}
func (id ID) Type() Type {
return id.typ
}
func (id ID) Hash() UUID {
return NewUUID(fmt.Sprintf("%s:%s", id.uuid, id.typ))
}
func (id ID) IsEmpty() bool {
return id.uuid.IsEmpty()
}
func (id ID) String() string {
return fmt.Sprintf("%s.%s", id.uuid.Short(), id.typ)
}
func (id ID) Resource() Resource {
return Resource{
ID: id.uuid.String(),
Name: id.typ.String(),
}
}
func (id ID) Sequence() Sequence {
return Sequence{id: id}
}