-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
lookup.go
515 lines (436 loc) · 12.1 KB
/
lookup.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package gofakeit
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"sync"
)
// FuncLookups is the primary map array with mapping to all available data
var FuncLookups map[string]Info
var lockFuncLookups sync.Mutex
// MapParams is the values to pass into a lookup generate
type MapParams map[string]MapParamsValue
type MapParamsValue []string
// Info structures fields to better break down what each one generates
type Info struct {
Display string `json:"display"`
Category string `json:"category"`
Description string `json:"description"`
Example string `json:"example"`
Output string `json:"output"`
ContentType string `json:"content_type"`
Params []Param `json:"params"`
Any any `json:"any"`
Generate func(f *Faker, m *MapParams, info *Info) (any, error) `json:"-"`
}
// Param is a breakdown of param requirements and type definition
type Param struct {
Field string `json:"field"`
Display string `json:"display"`
Type string `json:"type"`
Optional bool `json:"optional"`
Default string `json:"default"`
Options []string `json:"options"`
Description string `json:"description"`
}
// Field is used for defining what name and function you to generate for file outuputs
type Field struct {
Name string `json:"name"`
Function string `json:"function"`
Params MapParams `json:"params"`
}
func init() { initLookup() }
// init will add all the functions to MapLookups
func initLookup() {
addAddressLookup()
addAnimalLookup()
addAppLookup()
addAuthLookup()
addBeerLookup()
addBookLookup()
addCarLookup()
addCelebrityLookup()
addColorLookup()
addCompanyLookup()
addDatabaseSQLLookup()
addDateTimeLookup()
addEmojiLookup()
addErrorLookup()
addFileCSVLookup()
addFileJSONLookup()
addFileLookup()
addFileXMLLookup()
addFinanceLookup()
addFoodLookup()
addGameLookup()
addGenerateLookup()
addHackerLookup()
addHipsterLookup()
addHtmlLookup()
addImageLookup()
addInternetLookup()
addLanguagesLookup()
addLoremLookup()
addMinecraftLookup()
addMiscLookup()
addMovieLookup()
addNumberLookup()
addPaymentLookup()
addPersonLookup()
addProductLookup()
addSchoolLookup()
addStringLookup()
addTemplateLookup()
addWeightedLookup()
addWordAdjectiveLookup()
addWordAdverbLookup()
addWordConnectiveLookup()
addWordGeneralLookup()
addWordGrammerLookup()
addWordNounLookup()
addWordPhraseLookup()
addWordPrepositionLookup()
addWordPronounLookup()
addWordSentenceLookup()
addWordVerbLookup()
addWordCommentLookup()
addWordMiscLookup()
}
// internalFuncLookups is the internal map array with mapping to all available data
var internalFuncLookups map[string]Info = map[string]Info{
"fields": {
Description: "Example fields for generating csv, json, xml, etc",
Output: "gofakeit.Field",
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) {
function, _ := GetRandomSimpleFunc(f)
return Field{
Name: function,
Function: function,
}, nil
},
},
}
// NewMapParams will create a new MapParams
func NewMapParams() *MapParams {
return &MapParams{}
}
// Add will take in a field and value and add it to the map params type
func (m *MapParams) Add(field string, value string) {
_, ok := (*m)[field]
if !ok {
(*m)[field] = []string{value}
return
}
(*m)[field] = append((*m)[field], value)
}
// Get will return the array of string from the provided field
func (m *MapParams) Get(field string) []string {
return (*m)[field]
}
// Size will return the total size of the underlying map
func (m *MapParams) Size() int {
size := 0
for range *m {
size++
}
return size
}
// UnmarshalJSON will unmarshal the json into the []string
func (m *MapParamsValue) UnmarshalJSON(data []byte) error {
// check if the data is an array
// if so, marshal it into m
if data[0] == '[' {
var values []any
err := json.Unmarshal(data, &values)
if err != nil {
return err
}
// convert the values to array of strings
for _, value := range values {
typeOf := reflect.TypeOf(value).Kind().String()
if typeOf == "map" {
v, err := json.Marshal(value)
if err != nil {
return err
}
*m = append(*m, string(v))
} else {
*m = append(*m, fmt.Sprintf("%v", value))
}
}
return nil
}
// if not, then convert into a string and add it to m
var s any
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*m = append(*m, fmt.Sprintf("%v", s))
return nil
}
func GetRandomSimpleFunc(f *Faker) (string, Info) {
// Loop through all the functions and add them to a slice
var keys []string
for k, info := range FuncLookups {
// Only grab simple functions
if info.Params == nil {
keys = append(keys, k)
}
}
// Randomly grab a function from the slice
randomKey := randomString(f, keys)
// Return the function name and info
return randomKey, FuncLookups[randomKey]
}
// AddFuncLookup takes a field and adds it to map
func AddFuncLookup(functionName string, info Info) {
if FuncLookups == nil {
FuncLookups = make(map[string]Info)
}
// Check content type
if info.ContentType == "" {
info.ContentType = "text/plain"
}
lockFuncLookups.Lock()
FuncLookups[functionName] = info
lockFuncLookups.Unlock()
}
// GetFuncLookup will lookup
func GetFuncLookup(functionName string) *Info {
var info Info
var ok bool
// Check internal functions first
info, ok = internalFuncLookups[functionName]
if ok {
return &info
}
info, ok = FuncLookups[functionName]
if ok {
return &info
}
return nil
}
// RemoveFuncLookup will remove a function from lookup
func RemoveFuncLookup(functionName string) {
_, ok := FuncLookups[functionName]
if !ok {
return
}
lockFuncLookups.Lock()
delete(FuncLookups, functionName)
lockFuncLookups.Unlock()
}
// GetAny will retrieve Any field from Info
func (i *Info) GetAny(m *MapParams, field string) (any, error) {
_, value, err := i.GetField(m, field)
if err != nil {
return nil, err
}
// Make sure value[0] exists
if len(value) == 0 {
return nil, fmt.Errorf("could not find field: %s", field)
}
var anyValue any
// Try to convert to int
valueInt, err := strconv.ParseInt(value[0], 10, 64)
if err == nil {
return int(valueInt), nil
}
// Try to convert to float
valueFloat, err := strconv.ParseFloat(value[0], 64)
if err == nil {
return valueFloat, nil
}
// Try to convert to boolean
valueBool, err := strconv.ParseBool(value[0])
if err == nil {
return valueBool, nil
}
err = json.Unmarshal([]byte(value[0]), &anyValue)
if err == nil {
return valueBool, nil
}
return value[0], nil
}
// GetMap will retrieve map[string]any field from data
func (i *Info) GetMap(m *MapParams, field string) (map[string]any, error) {
_, value, err := i.GetField(m, field)
if err != nil {
return nil, err
}
var mapValue map[string]any
err = json.Unmarshal([]byte(value[0]), &mapValue)
if err != nil {
return nil, fmt.Errorf("%s field could not parse to map[string]any", field)
}
return mapValue, nil
}
// GetField will retrieve field from data
func (i *Info) GetField(m *MapParams, field string) (*Param, []string, error) {
// Get param
var p *Param
for _, param := range i.Params {
if param.Field == field {
p = ¶m
break
}
}
if p == nil {
return nil, nil, fmt.Errorf("could not find param field %s", field)
}
// Get value from map
if m != nil {
value, ok := (*m)[field]
if !ok {
// If default isnt empty use default
if p.Default != "" {
return p, []string{p.Default}, nil
}
return nil, nil, fmt.Errorf("could not find field: %s", field)
}
return p, value, nil
} else if p.Default != "" {
// If p.Type is []uint, then we need to convert it to []string
if strings.HasPrefix(p.Default, "[") {
// Remove [] from type
defaultClean := p.Default[1 : len(p.Default)-1]
// Split on comma
defaultSplit := strings.Split(defaultClean, ",")
return p, defaultSplit, nil
}
// If default isnt empty use default
return p, []string{p.Default}, nil
}
return nil, nil, fmt.Errorf("could not find field: %s", field)
}
// GetBool will retrieve boolean field from data
func (i *Info) GetBool(m *MapParams, field string) (bool, error) {
p, value, err := i.GetField(m, field)
if err != nil {
return false, err
}
// Try to convert to boolean
valueBool, err := strconv.ParseBool(value[0])
if err != nil {
return false, fmt.Errorf("%s field could not parse to bool value", p.Field)
}
return valueBool, nil
}
// GetInt will retrieve int field from data
func (i *Info) GetInt(m *MapParams, field string) (int, error) {
p, value, err := i.GetField(m, field)
if err != nil {
return 0, err
}
// Try to convert to int
valueInt, err := strconv.ParseInt(value[0], 10, 64)
if err != nil {
return 0, fmt.Errorf("%s field could not parse to int value", p.Field)
}
return int(valueInt), nil
}
// GetUint will retrieve uint field from data
func (i *Info) GetUint(m *MapParams, field string) (uint, error) {
p, value, err := i.GetField(m, field)
if err != nil {
return 0, err
}
// Try to convert to int
valueUint, err := strconv.ParseUint(value[0], 10, 64)
if err != nil {
return 0, fmt.Errorf("%s field could not parse to int value", p.Field)
}
return uint(valueUint), nil
}
// GetFloat32 will retrieve int field from data
func (i *Info) GetFloat32(m *MapParams, field string) (float32, error) {
p, value, err := i.GetField(m, field)
if err != nil {
return 0, err
}
// Try to convert to float
valueFloat, err := strconv.ParseFloat(value[0], 32)
if err != nil {
return 0, fmt.Errorf("%s field could not parse to float value", p.Field)
}
return float32(valueFloat), nil
}
// GetFloat64 will retrieve int field from data
func (i *Info) GetFloat64(m *MapParams, field string) (float64, error) {
p, value, err := i.GetField(m, field)
if err != nil {
return 0, err
}
// Try to convert to float
valueFloat, err := strconv.ParseFloat(value[0], 64)
if err != nil {
return 0, fmt.Errorf("%s field could not parse to float value", p.Field)
}
return valueFloat, nil
}
// GetString will retrieve string field from data
func (i *Info) GetString(m *MapParams, field string) (string, error) {
_, value, err := i.GetField(m, field)
if err != nil {
return "", err
}
return value[0], nil
}
// GetStringArray will retrieve []string field from data
func (i *Info) GetStringArray(m *MapParams, field string) ([]string, error) {
_, values, err := i.GetField(m, field)
if err != nil {
return nil, err
}
return values, nil
}
// GetIntArray will retrieve []int field from data
func (i *Info) GetIntArray(m *MapParams, field string) ([]int, error) {
_, value, err := i.GetField(m, field)
if err != nil {
return nil, err
}
var ints []int
for i := 0; i < len(value); i++ {
valueInt, err := strconv.ParseInt(value[i], 10, 64)
if err != nil {
return nil, fmt.Errorf("%s value could not parse to int", value[i])
}
ints = append(ints, int(valueInt))
}
return ints, nil
}
// GetUintArray will retrieve []uint field from data
func (i *Info) GetUintArray(m *MapParams, field string) ([]uint, error) {
_, value, err := i.GetField(m, field)
if err != nil {
return nil, err
}
var uints []uint
for i := 0; i < len(value); i++ {
valueUint, err := strconv.ParseUint(value[i], 10, 64)
if err != nil {
return nil, fmt.Errorf("%s value could not parse to uint", value[i])
}
uints = append(uints, uint(valueUint))
}
return uints, nil
}
// GetFloat32Array will retrieve []float field from data
func (i *Info) GetFloat32Array(m *MapParams, field string) ([]float32, error) {
_, value, err := i.GetField(m, field)
if err != nil {
return nil, err
}
var floats []float32
for i := 0; i < len(value); i++ {
valueFloat, err := strconv.ParseFloat(value[i], 32)
if err != nil {
return nil, fmt.Errorf("%s value could not parse to float", value[i])
}
floats = append(floats, float32(valueFloat))
}
return floats, nil
}