-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfakers.go
More file actions
184 lines (165 loc) · 5.85 KB
/
Copy pathfakers.go
File metadata and controls
184 lines (165 loc) · 5.85 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
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
package fako
import (
"math/rand"
"reflect"
"sync"
"time"
"github.com/icrowley/fake"
)
var customGenerators = map[string]func() string{}
var customGeneratorsMutex sync.RWMutex
var typeMapping = map[string]func() string{
"Brand": fake.Brand,
"Character": fake.Character,
"Characters": fake.Characters,
"City": fake.City,
"Color": fake.Color,
"Company": fake.Company,
"Continent": fake.Continent,
"Country": fake.Country,
"CreditCardType": fake.CreditCardType,
"Currency": fake.Currency,
"CurrencyCode": fake.CurrencyCode,
"Digits": fake.Digits,
"DomainName": fake.DomainName,
"DomainZone": fake.DomainZone,
"EmailAddress": fake.EmailAddress,
"EmailBody": fake.EmailBody,
"EmailSubject": fake.EmailSubject,
"FemaleFirstName": fake.FemaleFirstName,
"FemaleFullName": fake.FemaleFullName,
"FemaleFullNameWithPrefix": fake.FemaleFullNameWithPrefix,
"FemaleFullNameWithSuffix": fake.FemaleFullNameWithSuffix,
"FemaleLastName": fake.FemaleLastName,
"FemalePatronymic": fake.FemalePatronymic,
"FirstName": fake.FirstName,
"FullName": fake.FullName,
"FullNameWithPrefix": fake.FullNameWithPrefix,
"FullNameWithSuffix": fake.FullNameWithSuffix,
"Gender": fake.Gender,
"GenderAbbrev": fake.GenderAbbrev,
"HexColor": fake.HexColor,
"HexColorShort": fake.HexColorShort,
"IPv4": fake.IPv4,
"Industry": fake.Industry,
"JobTitle": fake.JobTitle,
"Language": fake.Language,
"LastName": fake.LastName,
"LatitudeDirection": fake.LatitudeDirection,
"LongitudeDirection": fake.LongitudeDirection,
"MaleFirstName": fake.MaleFirstName,
"MaleFullName": fake.MaleFullName,
"MaleFullNameWithPrefix": fake.MaleFullNameWithPrefix,
"MaleFullNameWithSuffix": fake.MaleFullNameWithSuffix,
"MaleLastName": fake.MaleLastName,
"MalePatronymic": fake.MalePatronymic,
"Model": fake.Model,
"Month": fake.Month,
"MonthShort": fake.MonthShort,
"Paragraph": fake.Paragraph,
"Paragraphs": fake.Paragraphs,
"Patronymic": fake.Patronymic,
"Phone": fake.Phone,
"Product": fake.Product,
"ProductName": fake.ProductName,
"Sentence": fake.Sentence,
"Sentences": fake.Sentences,
"SimplePassword": fake.SimplePassword,
"State": fake.State,
"StateAbbrev": fake.StateAbbrev,
"Street": fake.Street,
"StreetAddress": fake.StreetAddress,
"Title": fake.Title,
"TopLevelDomain": fake.TopLevelDomain,
"UserName": fake.UserName,
"WeekDay": fake.WeekDay,
"WeekDayShort": fake.WeekDayShort,
"Word": fake.Word,
"Words": fake.Words,
"Zip": fake.Zip,
}
// Register allows user to add his own data generators for special cases
// that we could not cover with the generators that fako includes by default.
// This function is safe for concurrent use.
func Register(identifier string, generator func() string) {
fakeType := camelize(identifier)
customGeneratorsMutex.Lock()
customGenerators[fakeType] = generator
customGeneratorsMutex.Unlock()
}
// Fuzz Fills passed interface with random data based on the struct field type,
// take a look at fuzzValueFor for details on supported data types.
// This function is safe for concurrent use.
func Fuzz(e any) {
ty := reflect.TypeOf(e)
if ty.Kind() == reflect.Ptr {
ty = ty.Elem()
}
if ty.Kind() == reflect.Struct {
value := reflect.ValueOf(e).Elem()
for i := range ty.NumField() {
field := value.Field(i)
if field.CanSet() {
field.Set(fuzzValueFor(field.Kind()))
}
}
}
}
func allGenerators() map[string]func() string {
dst := make(map[string]func() string)
// Copy typeMapping
for k, v := range typeMapping {
dst[k] = v
}
// Copy customGenerators with read lock
customGeneratorsMutex.RLock()
for k, v := range customGenerators {
dst[k] = v
}
customGeneratorsMutex.RUnlock()
return dst
}
//findFakeFunctionFor returns a faker function for a fako identifier
func findFakeFunctionFor(fako string) func() string {
result := func() string { return "" }
for kind, function := range allGenerators() {
if fako == kind {
result = function
break
}
}
return result
}
// fuzzValueFor Generates random values for the following types:
// string, bool, int, int32, int64, float32, float64
func fuzzValueFor(kind reflect.Kind) reflect.Value {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
switch kind {
case reflect.String:
return reflect.ValueOf(randomStringThreadSafe(25))
case reflect.Int:
return reflect.ValueOf(r.Int())
case reflect.Int32:
return reflect.ValueOf(r.Int31())
case reflect.Int64:
return reflect.ValueOf(r.Int63())
case reflect.Float32:
return reflect.ValueOf(r.Float32())
case reflect.Float64:
return reflect.ValueOf(r.Float64())
case reflect.Bool:
val := r.Intn(2) > 0
return reflect.ValueOf(val)
}
return reflect.ValueOf("")
}
// randomStringThreadSafe generates a random string without using global rand state
func randomStringThreadSafe(strlen int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[r.Intn(len(chars))]
}
return string(result)
}