-
Notifications
You must be signed in to change notification settings - Fork 5
/
id.go
83 lines (68 loc) · 1.44 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package request
import (
"bytes"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/oklog/ulid/v2"
)
// ID is an identifier to uniquely identify a request
type ID struct {
timestamp uint64
randomID string
}
func (i ID) String() string {
var id string
if !i.IsEmpty() {
id = fmt.Sprintf("%d-%s", i.timestamp, i.randomID)
}
return id
}
// IsEmpty returns true if there is no
// timestamp or randomID in Request ID @i
func (i ID) IsEmpty() bool {
return i.timestamp == 0 && i.randomID == ""
}
// UnmarshalJSON parses an ID from a json
func (i *ID) UnmarshalJSON(b []byte) error {
b = bytes.Trim(b, `"`)
id, err := Parse(string(b))
if err != nil {
return err
}
*i = id
return nil
}
// MarshalJSON converts ID to a string.
func (i ID) MarshalJSON() ([]byte, error) {
return json.Marshal(i.String())
}
// Parse read the given @id into an request ID.
func Parse(id string) (ID, error) {
slices := strings.Split(id, ("-"))
if len(slices) != 2 {
return ID{}, errors.New("wrong format for request id")
}
timestamp, err := strconv.Atoi(slices[0])
if err != nil {
return ID{}, err
}
return ID{
timestamp: uint64(timestamp),
randomID: slices[1],
}, nil
}
// IsEmpty returns true if an request ID is in it's zero-value format
func IsEmpty(id ID) bool {
return (id == ID{})
}
func newID() ID {
now := ulid.Now()
return ID{
timestamp: now,
randomID: ulid.MustNew(now, rand.Reader).String(),
}
}