-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
136 lines (116 loc) · 3.42 KB
/
data.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
/*
Package data contains all the raw data used by the Faker, like Names, Streets, Countries and Companies.
You can use it directly if you have your own selection criteria or a different random generator.
*/
package data
import (
"github.com/pkg/errors"
)
// ErrNotFound is triggered when a category of Data is not found
var ErrNotFound = errors.New("not found")
// Intn is a part of the randomizer package. It is used to select an item from the database.
type Intn interface {
Intn(n int) int
}
// Data consists of the main set of fake information
var Data = map[string]map[string][]string{
"person": Person,
"contact": Contact,
"address": Address,
"company": Company,
"job": Job,
"lorem": Lorem,
"language": Languages,
"internet": Internet,
"file": Files,
"color": Colors,
"computer": Computer,
"payment": Payment,
"hipster": Hipster,
"beer": Beer,
"hacker": Hacker,
"currency": Currency,
"log_level": LogLevels,
"timezone": TimeZone,
"vehicle": Vehicle,
"": {"": nil},
}
// IntData consists of the main set of fake information (integer only)
var IntData = map[string]map[string][]int{
"status_code": StatusCodes,
}
// Check is used to see if a [category, subcategory] exists in Data.
func Check(dataVal []string) bool {
var checkOk bool
if len(dataVal) == 2 {
_, checkOk = Data[dataVal[0]]
if checkOk {
_, checkOk = Data[dataVal[0]][dataVal[1]]
}
}
return checkOk
}
// Check if in lib
func intDataCheck(dataVal []string) bool {
if len(dataVal) != 2 {
return false
}
_, checkOk := IntData[dataVal[0]]
if checkOk {
_, checkOk = IntData[dataVal[0]][dataVal[1]]
}
return checkOk
}
// GetRandValue from a [category, subcategory]. Returns error if not found.
func GetRandValue(f Intn, dataVal []string) (string, error) {
if !Check(dataVal) {
return "", ErrNotFound
}
return Data[dataVal[0]][dataVal[1]][f.Intn(len(Data[dataVal[0]][dataVal[1]]))], nil
}
// GetRandIntValue Value from a [category, subcategory]. Returns error if not found.
func GetRandIntValue(f Intn, dataVal []string) (int, error) {
if !intDataCheck(dataVal) {
return 0, ErrNotFound
}
return IntData[dataVal[0]][dataVal[1]][f.Intn(len(IntData[dataVal[0]][dataVal[1]]))], nil
}
// Categories will return a map string array of available data categories and sub categories
func Categories() map[string][]string {
types := make(map[string][]string)
for category, subCategoriesMap := range Data {
subCategories := make([]string, 0)
for subType := range subCategoriesMap {
subCategories = append(subCategories, subType)
}
types[category] = subCategories
}
return types
}
// SetCache serve the same function as the global GetRandValue functionality
// but internally keeps a reference to a specific Category/Subcategory list
// improving consecutive calls performance (no more runtime.mapaccess1_faststr)
type SetCache interface {
GetRandValue() string
}
// NewDataListCache creates a new reference to a Data category set
// for faster access
func NewDataListCache(f Intn, dataVal []string) (SetCache, error) {
if f == nil || !Check(dataVal) {
return nil, ErrNotFound
}
res := &simpleList{}
res.randomizer = f
res.db = Data[dataVal[0]][dataVal[1]]
if len(res.db) == 0 {
return nil, errors.New("empty category list")
}
return res, nil
}
type simpleList struct {
randomizer Intn
db []string
}
func (w *simpleList) GetRandValue() string {
return w.db[w.randomizer.Intn(len(w.db))]
}