-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
73 lines (59 loc) · 2.04 KB
/
service.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
package ulidutil
import (
"io"
"time"
"github.com/oklog/ulid/v2"
)
// NewService returns ULID service with some handy functions
func NewService() Service {
s := service{}
s.entropy = SecureEntropy()
// Faster but not secure
// s.entropy = ulid.DefaultEntropy()
return &s
}
// An ULID is a 16 byte Universally Unique Lexicographically Sortable Identifier
type ULID = ulid.ULID
// Service represents the interface for the ULID utils
type Service interface {
// NewString returns an ULID as a string, with the current time. Panic on failure.
NewString() string
// NewStringWithTime returns an ULID as a string, with the given time. Panic on failure.
NewStringWithTime(t time.Time) string
// New returns an ULID with the current time
New() (ULID, error)
// MustNew returns an ULID with the current time. Panic on failure.
MustNew() ULID
// NewWithTime returns an ULID with given time
NewWithTime(t time.Time) (ULID, error)
// MustNewWithTime returns an ULID with given time. Panic on failure.
MustNewWithTime(t time.Time) ULID
}
// service implements the Service interface
type service struct {
entropy io.Reader
}
// NewString returns an ULID as a string, with the current time. Panic on failure.
func (s *service) NewString() string {
return s.MustNew().String()
}
// NewStringWithTime returns an ULID as a string, with the given time. Panic on failure.
func (s *service) NewStringWithTime(t time.Time) string {
return s.MustNewWithTime(t).String()
}
// New returns a new ULID with the current time
func (s *service) New() (ULID, error) {
return s.NewWithTime(time.Now())
}
// MustNew returns a new ULID with the current time. Panic on failure.
func (s *service) MustNew() ULID {
return s.MustNewWithTime(time.Now())
}
// NewWithTime returns a new ULID with given time
func (s *service) NewWithTime(t time.Time) (ULID, error) {
return ulid.New(ulid.Timestamp(t), s.entropy)
}
// MustNewWithTime returns a new ULID with given time. Panic on failure.
func (s *service) MustNewWithTime(t time.Time) ULID {
return ulid.MustNew(ulid.Timestamp(t), s.entropy)
}