forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
408 lines (346 loc) · 9.77 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
package gofakeit
import (
"encoding/json"
"fmt"
"math/rand"
"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"`
Generate func(r *rand.Rand, m *MapParams, info *Info) (interface{}, 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() {
addAuthLookup()
addAddressLookup()
addBeerLookup()
addCarLookup()
addPersonLookup()
addWordGeneralLookup()
addWordNounLookup()
addWordVerbLookup()
addWordAdverbLookup()
addWordPrepositionLookup()
addWordAdjectiveLookup()
addWordPronounLookup()
addWordConnectiveLookup()
addWordPhraseLookup()
addWordSentenceLookup()
addWordGrammerLookup()
addLoremLookup()
addGenerateLookup()
addMiscLookup()
addColorLookup()
addInternetLookup()
addDateTimeLookup()
addPaymentLookup()
addCompanyLookup()
addHackerLookup()
addHipsterLookup()
addLanguagesLookup()
addFileLookup()
addFileJSONLookup()
addFileXMLLookup()
addFileCSVLookup()
addEmojiLookup()
addImageLookup()
addNumberLookup()
addStringLookup()
addAnimalLookup()
addGameLookup()
addFoodLookup()
addAppLookup()
addWeightedLookup()
addMinecraftLookup()
addCelebrityLookup()
addDatabaseSQLLookup()
}
// 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 []interface{}
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 interface{}
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*m = append(*m, fmt.Sprintf("%v", s))
return nil
}
// 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 {
info, ok := FuncLookups[functionName]
if !ok {
return nil
}
return &info
}
// RemoveFuncLookup will remove a function from lookup
func RemoveFuncLookup(functionName string) {
_, ok := FuncLookups[functionName]
if !ok {
return
}
lockFuncLookups.Lock()
delete(FuncLookups, functionName)
lockFuncLookups.Unlock()
}
// 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 m == nil && 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
}