-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaker.go
325 lines (255 loc) · 6.08 KB
/
faker.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package faker
import (
"bytes"
"math"
"math/rand"
"reflect"
"strconv"
"strings"
"text/template"
"time"
)
type I18nLanguage int
var (
I18nLanguageNil I18nLanguage = 0 // 无关语言的默认
I18nLanguageEnUs I18nLanguage = 1
I18nLanguageZhCn I18nLanguage = 2
)
// 字符串集合
var whitespace = []byte(" \\t\\n\\r\\v\\f")
var asciiLowercase = []byte("abcdefghijklmnopqrstuvwxyz")
var asciiUppercase = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
var asciiLetters = append(asciiLowercase, asciiUppercase...)
var digits = []byte("0123456789")
var hexigits = append(digits, []byte("abcdefABCDEF")...)
var octdigits = []byte("01234567")
var punctuation = []byte("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
var printable []byte
func init() {
printable = append(digits, asciiLetters...)
printable = append(printable, punctuation...)
printable = append(printable, whitespace...)
}
type Faker struct {
Generator *rand.Rand
Language I18nLanguage
ProviderMap map[I18nLanguage]*Provider
}
// 基础的随机选择
func (f *Faker) Choice(itemList interface{}) interface{} {
ref := reflect.ValueOf(itemList)
if ref.Kind() != reflect.Slice {
panic("itemList is not slice")
}
if ref.Len() == 0 {
ref = reflect.MakeSlice(ref.Type(), 1, 1)
return ref.Index(0).Interface()
}
return ref.Index(f.Generator.Intn(ref.Len())).Interface()
}
func (f *Faker) RandomDigit() int {
return f.Generator.Int() % 10
}
func (f *Faker) RandomDigitNot(ignore ...int) int {
inSlice := func(el int, list []int) bool {
for i := range list {
if i == el {
return true
}
}
return false
}
for {
current := f.RandomDigit()
if inSlice(current, ignore) {
return current
}
}
}
func (f *Faker) RandomDigitNotNull() int {
return f.Generator.Int()%8 + 1
}
func (f *Faker) RandomNumber(size int) int {
if size == 1 {
return f.RandomDigit()
}
var min = int(math.Pow10(size - 1))
var max = int(math.Pow10(size)) - 1
return f.IntBetween(min, max)
}
func (f *Faker) FloatBetween(min, max float32) float32 {
if min == 0 && max == 0 {
return 0
}
if min >= max {
return max
}
return min + (f.Generator.Float32() * (max - min))
}
func (f *Faker) Float32() float32 {
max := float32(1 << 24)
min := -max - 1
return f.FloatBetween(min, max)
}
func (f *Faker) Float64() float64 {
return float64(f.Float32())
}
func (f *Faker) Int() int {
return f.Generator.Int()
}
func (f *Faker) Uint() uint {
return uint(f.Int())
}
func (f *Faker) Int64() int64 {
return int64(f.Int())
}
func (f *Faker) Uint64() uint64 {
return uint64(f.Int())
}
func (f *Faker) Int32() int32 {
return int32(f.Int())
}
func (f *Faker) Uint32() uint32 {
return uint32(f.Int())
}
func (f *Faker) IntBetween(min, max int) int {
if min == 0 && max == 0 {
return 0
}
if min >= max {
return max
}
return min + (f.Generator.Intn(max - min))
}
func (f *Faker) Int64Between(min, max int64) int64 {
return int64(f.IntBetween(int(min), int(max)))
}
func (f *Faker) Int32Between(min, max int32) int32 {
return int32(f.IntBetween(int(min), int(max)))
}
func (f *Faker) Unt32Between(min, max uint32) uint32 {
return uint32(f.IntBetween(int(min), int(max)))
}
func (f *Faker) Unt64Between(min, max uint64) uint64 {
return uint64(f.IntBetween(int(min), int(max)))
}
func (f *Faker) Letter() string {
return f.RandomLetter()
}
func (f *Faker) RandomLetter() string {
return string(f.Choice(asciiLetters).(byte))
}
func (f *Faker) RandomLowLetter() string {
return string(f.Choice(asciiLowercase).(byte))
}
func (f *Faker) RandomUpperLetter() string {
return string(f.Choice(asciiUppercase).(byte))
}
func (f *Faker) RandomLetterString(length int) string {
return f.RandomString(asciiLetters, length)
}
func (f *Faker) RandomLowLetterString(length int) string {
return f.RandomString(asciiLowercase, length)
}
func (f *Faker) RandomUpperLetterString(length int) string {
return f.RandomString(asciiUppercase, length)
}
func (f *Faker) RandomString(dataset []byte, length int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
bytes := make([]byte, length)
for i := range bytes {
bytes[i] = dataset[r.Intn(len(dataset))]
}
return string(bytes)
}
func (f *Faker) RandomBytesElement(s []byte) byte {
return f.Choice(s).(byte)
}
func (f *Faker) RandomStringElement(s []string) string {
return f.Choice(s).(string)
}
func (f *Faker) RandomIntElement(a []int) int {
return f.Choice(a).(int)
}
func (f *Faker) ShuffleString(s string) string {
orig := strings.Split(s, "")
dest := make([]string, len(orig))
for i := 0; i < len(orig); i++ {
dest[i] = orig[len(orig)-i-1]
}
return strings.Join(dest, "")
}
func (f *Faker) Bool() bool {
return f.IntBetween(0, 100) > 50
}
// 创建新的
func (f *Faker) InitGenerator() {
f.SetSeed(time.Now().UnixNano())
}
func (f *Faker) SetSeed(seed int64) {
f.Generator = rand.New(rand.NewSource(seed))
}
func (f *Faker) SetLanguage(i18n I18nLanguage) {
f.Language = i18n
}
func New() *Faker {
f := &Faker{
Generator: rand.New(rand.NewSource(time.Now().UnixNano())),
Language: I18nLanguageEnUs,
}
f.InitProviderMap()
return f
}
func NewWithLanguage(language I18nLanguage) *Faker {
f := New()
f.SetLanguage(language)
return f
}
// 格式化数据
func (f *Faker) Format(fmt string, args map[string]interface{}) (out string) {
var msg bytes.Buffer
tmpl, err := template.New("").Parse(fmt)
if err != nil {
return fmt
}
err = tmpl.Execute(&msg, args)
if err != nil {
return fmt
}
out = msg.String()
out = f.Bothify(out)
out = f.Asciify(out)
return out
}
func (f *Faker) Numerify(in string) (out string) {
for _, c := range strings.Split(in, "") {
if c == "#" {
c = strconv.Itoa(f.RandomDigit())
}
out = out + c
}
return
}
func (f *Faker) Lexify(in string) (out string) {
for _, c := range strings.Split(in, "") {
if c == "?" {
c = f.RandomLetter()
}
out = out + c
}
return
}
func (f *Faker) Bothify(in string) (out string) {
out = f.Lexify(in)
out = f.Numerify(out)
return
}
func (f *Faker) Asciify(in string) (out string) {
for _, c := range strings.Split(in, "") {
if c == "*" {
c = string(f.IntBetween(97, 126))
}
out = out + c
}
return
}