-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfaker.go
171 lines (144 loc) · 3.83 KB
/
faker.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
package faker
import (
"fmt"
"strings"
"github.com/Arinzeokeke/tr"
)
const (
namePrefix = "name"
addressPrefix = "address"
commercePrefix = "commerce"
companyPrefix = "company"
databasePrefix = "database"
datePrefix = "date"
internetPrefix = "internet"
hackerPrefix = "hacker"
loremPrefix = "lorem"
)
var (
definitions = map[string][]string{
"name": []string{"first_name", "last_name", "prefix", "suffix", "gender", "title", "male_first_name", "female_first_name", "male_middle_name", "female_middle_name", "male_last_name", "female_last_name"},
"address": []string{"city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "state", "state_abbr", "street_prefix", "postcode", "direction", "direction_abbr"},
"company": []string{"adjective", "noun", "descriptor", "bs_adjective", "bs_noun", "bs_verb", "suffix"},
"lorem": []string{"words"},
"hacker": []string{"abbreviation", "adjective", "noun", "verb", "ingverb", "phrase"},
"phone_number": []string{"formats"},
"finance": []string{"account_type", "transaction_type", "currency", "iban", "credit_card"},
"internet": []string{"avatar_uri", "domain_suffix", "free_email", "example_email", "password"},
"commerce": []string{"color", "department", "product_name", "price", "categories"},
"database": []string{"collation", "column", "engine", "type"},
"system": []string{"mimeTypes"},
"date": []string{"month", "weekday"},
"title": []string{},
"separator": []string{},
}
)
// Fake Object
type Fake struct {
Locale string
DefaultLocale string
Engine *tr.Engine
}
// Faker interface
type Faker interface {
Name() Namer
Lang(l string)
format(s, p string) string
Address() Addresser
Commerce() Commercer
Company() Companier
Database() Databaser
Dater() Dater
Image() Imager
Internet() Internetier
Hacker() Hackier
Lorem() Loremer
}
// Config for Faker
type Config struct {
DefaultLocale string
}
//New creates new faker
func New(c *Config, path string) (*Fake, error) {
e, err := tr.Init(path, c.DefaultLocale)
if err != nil {
return nil, fmt.Errorf("Couldn't load locale, Error: %v", err)
}
fake := &Fake{
Locale: "en",
DefaultLocale: "en",
Engine: e,
}
return fake, nil
}
// Name returns name
func (f *Fake) Name() Namer {
return &Name{f}
}
// Address returns address
func (f *Fake) Address() Addresser {
return &Address{f}
}
// Commerce returns commerce
func (f *Fake) Commerce() Commercer {
return &Commerce{f}
}
// Company returns company
func (f *Fake) Company() Companier {
return &Company{f}
}
// Database returns database
func (f *Fake) Database() Databaser {
return &Database{f}
}
// Date returns date
func (f *Fake) Date() Dater {
return &Date{f}
}
// Image returns image
func (f *Fake) Image() Imager {
return &Image{f}
}
// Internet returns internet
func (f *Fake) Internet() Internetier {
return &Internet{f}
}
// Hacker returns hacker
func (f *Fake) Hacker() Hackier {
return &Hacker{f}
}
// Lorem returns Lorem
func (f *Fake) Lorem() Loremer {
return &Lorem{f}
}
//Lang sets lang
func (f *Fake) Lang(l string) *Fake {
f.Engine.DefaultLocale = f.Engine.Lang(l)
f.Locale = l
return f
}
func (f *Fake) format(s, p string) string {
for strings.Index(s, "#{") != -1 && strings.Index(s, "}") != 1 {
start := strings.Index(s, "#{")
end := strings.Index(s, "}")
token := s[start+2 : end-1]
token = strings.ToLower(token)
token = strings.Replace(s, ".", "/", -1)
var affix string
if strings.Contains(token, "/") {
affix = token
} else {
affix = p + "/" + token
}
out := f.pick(affix)
s = s[:start] + out + s[end+1:]
}
return s
}
func (f *Fake) pick(affix string) string {
v, err := getList(f.Engine, affix, f.DefaultLocale)
if err != nil {
panic(err)
}
return v[random(len(v))]
}