-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusername_gen.go
More file actions
135 lines (105 loc) · 2.81 KB
/
Copy pathusername_gen.go
File metadata and controls
135 lines (105 loc) · 2.81 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package usernaemgen
import (
"math/rand/v2"
"strconv"
"strings"
)
type PostfixType byte
const (
// Do not use any postfix
NoPostfix PostfixType = iota
// Use the provided number as postfix for the result in
// case the number overflows the total possible values
UseProvidedNumberAfterOverflow PostfixType = iota
// Use the provided number as postfix for the result
UseProvidedNumber PostfixType = iota
)
type usernameGen struct {
dicts []DictType
delimiter string
postfixType PostfixType
totalPossibleValues int
}
func NewUsernameGen() *usernameGen {
return NewUsernameGenWithOptions("_", UseProvidedNumberAfterOverflow, Adjectives, Colors, Animals)
}
func NewUsernameGenWithOptions(delimiter string, postfixType PostfixType, dicts ...DictType) *usernameGen {
if len(dicts) == 0 {
panic("the dicts can not be empty")
}
return &usernameGen{
dicts: dicts,
delimiter: delimiter,
postfixType: postfixType,
}
}
func (u *usernameGen) SetDelimiter(delimiter string) {
u.delimiter = delimiter
}
func (u *usernameGen) SetPostfixType(postfixType PostfixType) {
u.postfixType = postfixType
}
func (u *usernameGen) SetDicts(dicts ...DictType) {
if len(dicts) == 0 {
panic("the dicts can not be empty")
}
u.totalPossibleValues = 0
u.dicts = dicts
}
func (u usernameGen) CalcTotalPossibleValues() int {
if u.totalPossibleValues != 0 {
return u.totalPossibleValues
}
var t int = 1
for _, dictType := range u.dicts {
t *= DictsLen[dictType]
}
u.totalPossibleValues = t
return t
}
func (u usernameGen) GenerateRand() string {
return u.Generate(int64(rand.Int32()))
}
func (u usernameGen) Generate(randNum int64) string {
dictsCount := len(u.dicts)
indices := u.generateIndices(randNum)
if len(indices) != dictsCount {
panic("the lenguth of te array of indices should equal the dictsCount")
}
sb := strings.Builder{}
for i, dictType := range u.dicts {
word := dicts[dictType][indices[i]]
sb.WriteString(word)
if i != dictsCount-1 {
sb.WriteString(u.delimiter)
}
}
u.addPostfix(&sb, randNum)
return sb.String()
}
func (u usernameGen) generateIndices(randNum int64) []int64 {
dictsCount := len(u.dicts)
indexArr := make([]int64, 0, dictsCount)
carry := randNum
for _, dictType := range u.dicts {
dictLen := int64(DictsLen[dictType])
index := carry % dictLen
indexArr = append(indexArr, index)
carry = carry / dictLen
}
return indexArr
}
func (u usernameGen) addPostfix(sb *strings.Builder, randNum int64) {
switch u.postfixType {
case NoPostfix:
return
case UseProvidedNumber:
sb.WriteString(u.delimiter)
sb.WriteString(strconv.FormatInt(randNum, 10))
case UseProvidedNumberAfterOverflow:
if randNum >= int64(u.CalcTotalPossibleValues()) {
sb.WriteString(u.delimiter)
sb.WriteString(strconv.FormatInt(randNum, 10))
}
}
}