-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
helpers.go
374 lines (322 loc) · 8.09 KB
/
helpers.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package gofakeit
import (
"encoding/json"
"fmt"
"math"
"reflect"
"strings"
"unicode"
"github.com/brianvoe/gofakeit/v7/data"
)
const lowerStr = "abcdefghijklmnopqrstuvwxyz"
const upperStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numericStr = "0123456789"
const specialStr = "@#$%&?|!(){}<>=*+-_:;,."
const specialSafeStr = "!@.-_*" // https://github.com/1Password/spg/pull/22
const spaceStr = " "
const allStr = lowerStr + upperStr + numericStr + specialStr + spaceStr
const vowels = "aeiou"
const hashtag = '#'
const questionmark = '?'
const dash = '-'
const base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
const minUint = 0
const maxUint = ^uint(0)
const minInt = -maxInt - 1
const maxInt = int(^uint(0) >> 1)
const is32bit = ^uint(0)>>32 == 0
// Check if in lib
func dataCheck(dataVal []string) bool {
var checkOk bool
if len(dataVal) == 2 {
_, checkOk = data.Data[dataVal[0]]
if checkOk {
_, checkOk = data.Data[dataVal[0]][dataVal[1]]
}
}
return checkOk
}
// Get Random Value
func getRandValue(f *Faker, dataVal []string) string {
if !dataCheck(dataVal) {
return ""
}
return data.Data[dataVal[0]][dataVal[1]][f.IntN(len(data.Data[dataVal[0]][dataVal[1]]))]
}
// Replace # with numbers
func replaceWithNumbers(f *Faker, str string) string {
if str == "" {
return str
}
bytestr := []byte(str)
for i := 0; i < len(bytestr); i++ {
if bytestr[i] == hashtag {
bytestr[i] = byte(randDigit(f))
}
}
if bytestr[0] == '0' {
bytestr[0] = byte(f.IntN(8)+1) + '0'
}
return string(bytestr)
}
// Replace ? with ASCII lowercase letters
func replaceWithLetters(f *Faker, str string) string {
if str == "" {
return str
}
bytestr := []byte(str)
for i := 0; i < len(bytestr); i++ {
if bytestr[i] == questionmark {
bytestr[i] = byte(randLetter(f))
}
}
return string(bytestr)
}
// Replace ? with ASCII lowercase letters between a and f
func replaceWithHexLetters(f *Faker, str string) string {
if str == "" {
return str
}
bytestr := []byte(str)
for i := 0; i < len(bytestr); i++ {
if bytestr[i] == questionmark {
bytestr[i] = byte(randHexLetter(f))
}
}
return string(bytestr)
}
// Generate random lowercase ASCII letter
func randLetter(f *Faker) rune {
allLetters := upperStr + lowerStr
return rune(allLetters[f.IntN(len(allLetters))])
}
func randCharacter(f *Faker, s string) string {
return string(s[f.Int64()%int64(len(s))])
}
// Generate random lowercase ASCII letter between a and f
func randHexLetter(f *Faker) rune {
return rune(byte(f.IntN(6)) + 'a')
}
// Generate random ASCII digit
func randDigit(f *Faker) rune {
return rune(byte(f.IntN(10)) + '0')
}
// Generate random integer between min and max
func randIntRange(f *Faker, min, max int) int {
if min == max {
return min
}
if min > max {
min, max = max, min // Swap if min is greater than max
}
// Use f.IntN to generate a random number in [0, rangeSize) and shift it into [min, max].
return f.IntN(max-min+1) + min
}
// Generate random uint between min and max
func randUintRange(f *Faker, min, max uint) uint {
if min == max {
return min // Immediate return if range is zero
}
if min > max {
min, max = max, min // Swap if min is greater than max
}
// Use f.UintN to generate a random number in [0, rangeSize) and shift it into [min, max].
return f.UintN(max-min+1) + min
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(math.Floor(num*output)) / output
}
func equalSliceString(a, b []string) bool {
sizeA, sizeB := len(a), len(b)
if sizeA != sizeB {
return false
}
for i, va := range a {
vb := b[i]
if va != vb {
return false
}
}
return true
}
func equalSliceInt(a, b []int) bool {
sizeA, sizeB := len(a), len(b)
if sizeA != sizeB {
return false
}
for i, va := range a {
vb := b[i]
if va != vb {
return false
}
}
return true
}
func equalSliceInterface(a, b []any) bool {
sizeA, sizeB := len(a), len(b)
if sizeA != sizeB {
return false
}
for i, va := range a {
if !reflect.DeepEqual(va, b[i]) {
return false
}
}
return true
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func anyToString(a any) string {
if a == nil {
return ""
}
// If it's a slice of bytes or struct, unmarshal it into an interface
if bytes, ok := a.([]byte); ok {
return string(bytes)
}
// If it's a struct, map, or slice, convert to JSON
switch reflect.TypeOf(a).Kind() {
case reflect.Struct, reflect.Map, reflect.Slice:
b, err := json.Marshal(a)
if err == nil {
return string(b)
}
}
return fmt.Sprintf("%v", a)
}
// Title returns a copy of the string s with all Unicode letters that begin words
// mapped to their Unicode title case
func title(s string) string {
// isSeparator reports whether the rune could mark a word boundary
isSeparator := func(r rune) bool {
// ASCII alphanumerics and underscore are not separators
if r <= 0x7F {
switch {
case '0' <= r && r <= '9':
return false
case 'a' <= r && r <= 'z':
return false
case 'A' <= r && r <= 'Z':
return false
case r == '_':
return false
}
return true
}
// Letters and digits are not separators
if unicode.IsLetter(r) || unicode.IsDigit(r) {
return false
}
// Otherwise, all we can do for now is treat spaces as separators.
return unicode.IsSpace(r)
}
prev := ' '
return strings.Map(
func(r rune) rune {
if isSeparator(prev) {
prev = r
return unicode.ToTitle(r)
}
prev = r
return r
},
s)
}
func funcLookupSplit(str string) ([]string, error) {
out := []string{}
for str != "" {
if strings.HasPrefix(str, "[") {
startIndex := strings.Index(str, "[")
endIndex := strings.Index(str, "]")
if endIndex == -1 {
return nil, fmt.Errorf("invalid lookup split missing ending ] bracket")
}
val := str[(startIndex) : endIndex+1]
out = append(out, strings.TrimSpace(val))
str = strings.Replace(str, val, "", 1)
// Trim off comma if it has it
if strings.HasPrefix(str, ",") {
str = strings.Replace(str, ",", "", 1)
}
} else {
strSplit := strings.SplitN(str, ",", 2)
strSplitLen := len(strSplit)
if strSplitLen >= 1 {
out = append(out, strings.TrimSpace(strSplit[0]))
}
if strSplitLen >= 2 {
str = strSplit[1]
} else {
str = ""
}
}
}
return out, nil
}
// Used for parsing the tag in a struct
func parseNameAndParamsFromTag(tag string) (string, string) {
// Trim the curly on the beginning and end
tag = strings.TrimLeft(tag, "{")
tag = strings.TrimRight(tag, "}")
// Check if has params separated by :
fNameSplit := strings.SplitN(tag, ":", 2)
fName := ""
fParams := ""
if len(fNameSplit) >= 1 {
fName = fNameSplit[0]
}
if len(fNameSplit) >= 2 {
fParams = fNameSplit[1]
}
return fName, fParams
}
// Used for parsing map params
func parseMapParams(info *Info, fParams string) (*MapParams, error) {
// Get parameters, make sure params and the split both have values
mapParams := NewMapParams()
paramsLen := len(info.Params)
// If just one param and its a string simply just pass it
if paramsLen == 1 && info.Params[0].Type == "string" {
mapParams.Add(info.Params[0].Field, fParams)
} else if paramsLen > 0 && fParams != "" {
splitVals, err := funcLookupSplit(fParams)
if err != nil {
return nil, err
}
mapParams, err = addSplitValsToMapParams(splitVals, info, mapParams)
if err != nil {
return nil, err
}
}
// If mapParams doesnt have a size then return nil
if mapParams.Size() == 0 {
return nil, nil
}
return mapParams, nil
}
// Used for splitting the values
func addSplitValsToMapParams(splitVals []string, info *Info, mapParams *MapParams) (*MapParams, error) {
for ii := 0; ii < len(splitVals); ii++ {
if len(info.Params)-1 >= ii {
if strings.HasPrefix(splitVals[ii], "[") {
lookupSplits, err := funcLookupSplit(strings.TrimRight(strings.TrimLeft(splitVals[ii], "["), "]"))
if err != nil {
return nil, err
}
for _, v := range lookupSplits {
mapParams.Add(info.Params[ii].Field, v)
}
} else {
mapParams.Add(info.Params[ii].Field, splitVals[ii])
}
}
}
return mapParams, nil
}