-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprovider.go
146 lines (127 loc) · 3.56 KB
/
provider.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
package dbanon
import (
"reflect"
"strconv"
"strings"
"syreclabs.com/go/faker"
)
var fakeEmail = faker.Internet().Email
var rawProviders = map[string]interface{} {
"Lorem()": faker.Lorem(),
"Internet()": faker.Internet(),
"Commerce()": faker.Commerce(),
"Code()": faker.Code(),
"Number()": faker.Number(),
}
type Provider struct{
providedUniqueEmails map[string]int
}
func NewProvider() *Provider {
made := make(map[string]int)
p := &Provider{providedUniqueEmails: made}
return p
}
type ProviderInterface interface {
Get(s string) string
}
func (p Provider) Get(s string) string {
i := strings.Index(s, "faker.")
if i == 0 {
return p.raw(s)
}
switch s {
case "firstname":
return faker.Name().FirstName()
case "lastname":
return faker.Name().LastName()
case "fullname":
return faker.Name().FirstName() + " " + faker.Name().LastName()
case "email":
return faker.Internet().Email()
case "unique_email":
new := fakeEmail()
_, exists := p.providedUniqueEmails[new]
if !exists {
p.providedUniqueEmails[new] = 0
return new
}
p.providedUniqueEmails[new]++
ret := strings.Join([]string{strconv.Itoa(p.providedUniqueEmails[new]), new}, "")
return ret
case "username":
return faker.Internet().UserName()
case "password":
return faker.Internet().Password(8, 14)
case "datetime":
return faker.Date().Birthday(0, 40).Format("2006-01-02 15:04:05")
case "customer_suffix":
return faker.Name().Suffix()
case "ipv4":
return faker.Internet().IpV4Address()
case "state":
return faker.Address().State()
case "city":
return faker.Address().City()
case "postcode":
return faker.Address().Postcode()
case "street":
return faker.Address().StreetAddress()
case "telephone":
return faker.PhoneNumber().PhoneNumber()
case "title":
return faker.Name().Prefix()
case "company":
return faker.Company().Name()
case "md5":
return faker.Lorem().Characters(32)
case "note255":
return faker.Lorem().Characters(50)
case "region_id":
// https://github.com/meanbee/magedbm2/blob/fc8bbf9a97db2c27d0cd8a1153dda8c95b6f5996/src/Anonymizer/Formatter/Address/RegionId.php#L24
return faker.Number().Between(1, 550)
case "gender":
// https://github.com/meanbee/magedbm2/blob/fc8bbf9a97db2c27d0cd8a1153dda8c95b6f5996/src/Anonymizer/Formatter/Person/Gender.php#L20
return faker.Number().Between(1, 3)
case "country_code":
return faker.Address().CountryCode()
case "vat_number":
// https://github.com/meanbee/magedbm2/blob/fc8bbf9a97db2c27d0cd8a1153dda8c95b6f5996/src/Anonymizer/Formatter/Company/VatNumber.php#L21
return "GB" + faker.Number().Between(100000000, 999999999)
}
return ""
}
func (p Provider) raw (s string) string {
parts := strings.Split(s, ".")
className, ok := rawProviders[parts[1]]
if !ok {
// TODO: Cover this with logging
return ""
}
class := reflect.ValueOf(className)
methodName := strings.Split(parts[2], "(")[0]
method := class.MethodByName(methodName)
if !method.IsValid() {
// TODO: Cover this with logging
return ""
}
argsStart := strings.Index(parts[2], "(")
argsEnd := strings.Index(parts[2], ")")
if argsStart == -1 || argsEnd == -1 || argsEnd < argsStart {
// TODO: Cover this with logging
return ""
}
args := parts[2][argsStart + 1 : argsEnd]
if args == "" {
out := method.Call(nil)
return out[0].String()
} else {
argsArray := strings.Split(args, ",")
in := make([]reflect.Value, len(argsArray))
for i, param := range argsArray {
intVal, _ := strconv.Atoi(strings.Replace(param, " ", "", -1))
in[i] = reflect.ValueOf(intVal)
}
out := method.Call(in)
return out[0].String()
}
}