-
Notifications
You must be signed in to change notification settings - Fork 38
/
faker.go
334 lines (291 loc) · 10.2 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
326
327
328
329
330
331
332
333
334
/*
Package faker is a library for generating fake data such as names, addresses, and phone numbers.
It is a (mostly) API-compatible port of Ruby Faker gem (https://github.com/stympy/faker) to Go.
*/
package faker // import "syreclabs.com/go/faker"
import (
"fmt"
"math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"time"
"syreclabs.com/go/faker/locales"
)
const (
digits = "0123456789"
uLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
dLetters = "abcdefghijklmnopqrstuvwxyz"
letters = uLetters + dLetters
digitsAndLetters = digits + letters
)
var (
aryDigits = strings.Split(digits, "")
aryULetters = strings.Split(uLetters, "")
aryDLetters = strings.Split(dLetters, "")
aryLetters = strings.Split(letters, "")
)
// Generate locales
//go:generate go run cmd/generate.go yaml/de-AT.yml locales/de-at.go
//go:generate go run cmd/generate.go yaml/de-CH.yml locales/de-ch.go
//go:generate go run cmd/generate.go yaml/de.yml locales/de.go
//go:generate go run cmd/generate.go yaml/en-au-ocker.yml locales/en-au-ocker.go
//go:generate go run cmd/generate.go yaml/en-AU.yml locales/en-au.go
//go:generate go run cmd/generate.go yaml/en-BORK.yml locales/en-bork.go
//go:generate go run cmd/generate.go yaml/en-CA.yml locales/en-ca.go
//go:generate go run cmd/generate.go yaml/en-GB.yml locales/en-gb.go
//go:generate go run cmd/generate.go yaml/en-IND.yml locales/en-ind.go
//go:generate go run cmd/generate.go yaml/en-NEP.yml locales/en-nep.go
//go:generate go run cmd/generate.go yaml/en-US.yml locales/en-us.go
//go:generate go run cmd/generate.go yaml/en.yml locales/en.go
//go:generate go run cmd/generate.go yaml/es.yml locales/es.go
//go:generate go run cmd/generate.go yaml/fa.yml locales/fa.go
//go:generate go run cmd/generate.go yaml/fr.yml locales/fr.go
//go:generate go run cmd/generate.go yaml/it.yml locales/it.go
//go:generate go run cmd/generate.go yaml/ja.yml locales/ja.go
//go:generate go run cmd/generate.go yaml/ko.yml locales/ko.go
//go:generate go run cmd/generate.go yaml/nb-NO.yml locales/nb-no.go
//go:generate go run cmd/generate.go yaml/nl.yml locales/nl.go
//go:generate go run cmd/generate.go yaml/pl.yml locales/pl.go
//go:generate go run cmd/generate.go yaml/pt-BR.yml locales/pt-br.go
//go:generate go run cmd/generate.go yaml/ru.yml locales/ru.go
//go:generate go run cmd/generate.go yaml/sk.yml locales/sk.go
//go:generate go run cmd/generate.go yaml/sv.yml locales/sv.go
//go:generate go run cmd/generate.go yaml/vi.yml locales/vi.go
//go:generate go run cmd/generate.go yaml/zh-CN.yml locales/zh-cn.go
//go:generate go run cmd/generate.go yaml/zh-TW.yml locales/zh-tw.go
// Locale holds the default locale.
var Locale = locales.En
var recurseRegex = regexp.MustCompile(`#\{([A-Za-z]+\.[^\}]+)\}`)
// RandomInt returns random int in [min, max] range.
func RandomInt(min, max int) int {
if max <= min {
// degenerate case, return min
return min
}
return min + localRand.Intn(max-min+1)
}
// RandomInt64 returns random int64 in [min, max] range.
func RandomInt64(min, max int64) int64 {
if max <= min {
// degenerate case, return min
return min
}
return min + localRand.Int63n(max-min+1)
}
// RandomString returns a random alphanumeric string with length n.
func RandomString(n int) string {
bytes := make([]byte, n)
localRand.Read(bytes)
for i, b := range bytes {
bytes[i] = digitsAndLetters[b%byte(len(digitsAndLetters))]
}
return string(bytes)
}
// RandomRepeat returns a new string consisting of random number of copies of the string s.
func RandomRepeat(s string, min, max int) string {
return strings.Repeat(s, RandomInt(min, max))
}
// RandomChoice returns random string from slice of strings.
func RandomChoice(ss []string) string {
return ss[localRand.Intn(len(ss))]
}
func includesString(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}
// Numerify replaces pattern like '##-###' with randomly generated digits.
func Numerify(s string) string {
first := true
for _, sm := range regexp.MustCompile(`#`).FindAllStringSubmatch(s, -1) {
if first {
// make sure result does not start with zero
s = strings.Replace(s, sm[0], RandomChoice(aryDigits[1:]), 1)
first = false
} else {
s = strings.Replace(s, sm[0], RandomChoice(aryDigits), 1)
}
}
return s
}
// Letterify replaces pattern like '??? ??' with randomly generated uppercase letters
func Letterify(s string) string {
for _, sm := range regexp.MustCompile(`\?`).FindAllStringSubmatch(s, -1) {
s = strings.Replace(s, sm[0], RandomChoice(aryULetters), 1)
}
return s
}
// NumerifyAndLetterify both numerifies and letterifies s.
func NumerifyAndLetterify(s string) string {
return Letterify(Numerify(s))
}
// Regexify attempts to generate a string that would match given regular expression.
// It does not handle ., *, unbounded ranges such as {1,},
// extensions such as (?=), character classes, some abbreviations for character classes,
// and nested parentheses.
func Regexify(s string) (string, error) {
// ditch the anchors
res := regexp.MustCompile(`^\/?\^?`).ReplaceAllString(s, "")
res = regexp.MustCompile(`\$?\/?$`).ReplaceAllString(res, "")
// all {2} become {2,2} and ? become {0,1}
res = regexp.MustCompile(`\{(\d+)\}`).ReplaceAllString(res, `{$1,$1}`)
res = regexp.MustCompile(`\?`).ReplaceAllString(res, `{0,1}`)
// [12]{1,2} becomes [12] or [12][12]
for _, sm := range regexp.MustCompile(`(\[[^\]]+\])\{(\d+),(\d+)\}`).FindAllStringSubmatch(res, -1) {
min, _ := strconv.Atoi(sm[2])
max, _ := strconv.Atoi(sm[3])
res = strings.Replace(res, sm[0], RandomRepeat(sm[1], min, max), 1)
}
// (12|34){1,2} becomes (12|34) or (12|34)(12|34)
for _, sm := range regexp.MustCompile(`(\([^\)]+\))\{(\d+),(\d+)\}`).FindAllStringSubmatch(res, -1) {
min, _ := strconv.Atoi(sm[2])
max, _ := strconv.Atoi(sm[3])
res = strings.Replace(res, sm[0], RandomRepeat(sm[1], min, max), 1)
}
// A{1,2} becomes A or AA or \d{3} becomes \d\d\d
for _, sm := range regexp.MustCompile(`(\\?.)\{(\d+),(\d+)\}`).FindAllStringSubmatch(res, -1) {
min, _ := strconv.Atoi(sm[2])
max, _ := strconv.Atoi(sm[3])
res = strings.Replace(res, sm[0], RandomRepeat(sm[1], min, max), 1)
}
// (this|that) becomes 'this' or 'that'
for _, sm := range regexp.MustCompile(`\((.*?)\)`).FindAllStringSubmatch(res, -1) {
res = strings.Replace(res, sm[0], RandomChoice(strings.Split(sm[1], "|")), 1)
}
// all A-Z inside of [] become C (or X, or whatever)
for _, sm := range regexp.MustCompile(`\[([^\]]+)\]`).FindAllStringSubmatch(res, -1) {
cls := sm[1]
// find and replace all ranges in character class cls
for _, subsm := range regexp.MustCompile(`(\w\-\w)`).FindAllStringSubmatch(cls, -1) {
rng := strings.Split(subsm[1], "-")
repl := string(rune(RandomInt(int(rng[0][0]), int(rng[1][0]))))
cls = strings.Replace(cls, subsm[0], repl, 1)
}
res = strings.Replace(res, sm[1], cls, 1)
}
// all [ABC] become B (or A or C)
for _, sm := range regexp.MustCompile(`\[([^\]]+)\]`).FindAllStringSubmatch(res, -1) {
res = strings.Replace(res, sm[0], RandomChoice(strings.Split(sm[1], "")), 1)
}
// all \d become random digits
res = regexp.MustCompile(`\\d`).ReplaceAllStringFunc(res, func(s string) string {
return RandomChoice(aryDigits)
})
// all \w become random letters
res = regexp.MustCompile(`\\d`).ReplaceAllStringFunc(res, func(s string) string {
return RandomChoice(aryLetters)
})
return res, nil
}
func localeValueAt(path string, locale map[string]interface{}) (interface{}, bool) {
var val interface{} = locale
for _, key := range strings.Split(path, ".") {
v, ok := val.(map[string]interface{})
if !ok {
// all nodes are expected to have map[string]interface{} type
panic(fmt.Sprintf("%v: invalid value type %v", path, reflect.TypeOf(val)))
}
val, ok = v[key]
if !ok {
// given path does not exists in given locale
return nil, false
}
}
return val, true
}
func valueAt(path string) interface{} {
val, ok := localeValueAt(path, Locale)
if !ok {
// path does not exist in given locale, fallback to En
val, ok = localeValueAt(path, locales.En)
if !ok {
// not in En either, give up
panic(fmt.Sprintf("%v: invalid path", path))
}
}
return val
}
// Fetch returns a value at given key path in default locale. If key path holds an array,
// it returns random array element. If value looks like a regex, it attempts to regexify it.
func Fetch(path string) string {
var res string
switch val := valueAt(path).(type) {
case [][]string:
// slice of string slices - select random element and join
choices := make([]string, len(val))
for i, slice := range val {
choices[i] = RandomChoice(slice)
}
res = strings.Join(choices, " ")
case []string:
// slice of strings - select random element
res = RandomChoice(val)
case string:
// plain string
res = val
default:
// not supported
panic(fmt.Sprintf("%v: invalid value type %v", path, reflect.TypeOf(val)))
}
// recursively substitute #{...} value references
for _, sm := range recurseRegex.FindAllStringSubmatch(res, -1) {
path := strings.ToLower(sm[1])
res = strings.Replace(res, sm[0], Fetch(path), 1)
}
// if res looks like regex, regexify
if strings.HasPrefix(res, "/") && strings.HasSuffix(res, "/") {
res, err := Regexify(res)
if err != nil {
panic(fmt.Sprintf("failed to regexify %v: %v", res, err))
}
return res
}
return res
}
// from https://github.com/golang/go/blob/go1.10.3/src/math/rand/rand.go#L371
type lockedSource struct {
lk sync.Mutex
src rand.Source64
}
func (r *lockedSource) Int63() (n int64) {
r.lk.Lock()
n = r.src.Int63()
r.lk.Unlock()
return
}
func (r *lockedSource) Uint64() (n uint64) {
r.lk.Lock()
n = r.src.Uint64()
r.lk.Unlock()
return
}
func (r *lockedSource) Seed(seed int64) {
r.lk.Lock()
r.src.Seed(seed)
r.lk.Unlock()
}
// lockedReadRand provides Rand.Read that is safe for concurrent use.
type lockedReadRand struct {
lk sync.Mutex
*rand.Rand
}
func (lr *lockedReadRand) Read(p []byte) (n int, err error) {
lr.lk.Lock()
n, err = lr.Rand.Read(p)
lr.lk.Unlock()
return
}
var (
localSource = &lockedSource{src: rand.NewSource(time.Now().UTC().UnixNano()).(rand.Source64)}
localRand = &lockedReadRand{Rand: rand.New(localSource)}
)
// Seed uses the provided seed value to initialize the random source to a deterministic state.
func Seed(seed int64) {
localRand.Seed(seed)
}