forked from gorpher/gone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid.go
More file actions
95 lines (78 loc) · 1.98 KB
/
Copy pathid.go
File metadata and controls
95 lines (78 loc) · 1.98 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
package gone
import (
"math/rand"
"strings"
"time"
uuid "github.com/satori/go.uuid"
"github.com/bwmarrin/snowflake"
"github.com/rs/xid"
)
// UUID 16 bytes 36 chars configuration free, not sortable
// shortuuid 16 bytes 22 chars configuration free, not sortable
// Snowflake 8 bytes up to 20 chars needs machine/DC configuration, needs central server, sortable
// MongoID 12 bytes 24 chars configuration free, sortable
// xid 12 bytes 20 chars configuration free, sortable
type IDGenerator interface {
Snowflake() snowflake.ID
XID() xid.ID
UUID4() uuid.UUID
SInt64() int64
SString() string
XString() string
UString() string
RandString(int) string
}
type id struct {
n *snowflake.Node
}
var ID id
func (i *id) Snowflake() snowflake.ID {
return i.n.Generate()
}
func (i *id) XID() xid.ID {
return xid.New()
}
func (i *id) UUID4() uuid.UUID {
return uuid.NewV4()
}
func (i *id) SInt64() int64 {
return i.n.Generate().Int64()
}
func (i *id) SString() string {
return i.n.Generate().String()
}
func (i *id) XString() string {
return xid.New().String()
}
func (i *id) UString() string {
return uuid.NewV4().String()
}
// RandString 最大64个字母.
func (i *id) RandString(count int) string {
if count > 62 {
count = 62
}
a := []byte{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
}
var r strings.Builder
for j := 0; j < count; j++ {
k := RandInt32(0, int32(len(a)))
r.WriteRune(rune(a[int(k)]))
a = append(a[:k], a[k+1:]...)
}
return r.String()
}
func init() { //nolint
snowflake.Epoch = time.Now().Unix()
rand.Seed(rand.Int63n(time.Now().UnixNano())) // nolint
node := 110 + rand.Int63n(1023-110) // nolint
n, _ := snowflake.NewNode(node) // nolint
ID = id{
n: n,
}
}