-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfaker.go
284 lines (265 loc) · 7.92 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
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
// Package faker is a random data generator and struct fake data generator.
package faker
import (
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
const (
tagName = "faker"
skipTag = "-"
uniqueTag = "unique"
)
var (
tagFnCallRegexp = regexp.MustCompile(`(.+?)\((.+?)\)`)
tagLenRegexp = regexp.MustCompile(`len=(\d+)`)
tagSkipIfRegexp = regexp.MustCompile(`skip\s+if\s+(\w+)`)
)
type fakerTag struct {
funcName string
skipIf string
uniqueGroup string
length int
params []string
}
func (tag *fakerTag) mustSkip() bool {
return tag.funcName == skipTag
}
// func decodeTag(tagString string) *fakerTag {
func decodeTag(structReflectType reflect.Type, fieldIndex int) *fakerTag {
fieldReflectType := structReflectType.Field(fieldIndex)
tagString := fieldReflectType.Tag.Get(tagName)
tag := &fakerTag{}
for _, token := range strings.Split(tagString, ";") {
if token == skipTag {
tag.funcName = skipTag
return tag
}
if m := tagSkipIfRegexp.FindStringSubmatch(token); len(m) == 2 {
tag.skipIf = m[1]
continue
}
if token == uniqueTag {
tag.uniqueGroup = fmt.Sprintf("%s-%s", structReflectType.Name(), fieldReflectType.Name)
continue
}
if m := tagLenRegexp.FindStringSubmatch(token); len(m) == 2 {
tag.length, _ = strconv.Atoi(m[1])
continue
}
if tag.funcName == "" {
if m := tagFnCallRegexp.FindStringSubmatch(token); len(m) == 3 {
tag.funcName = m[1]
tag.params = strings.Split(m[2], ",")
continue
}
tag.funcName = token
}
}
return tag
}
// Build fills in exported elements of a struct with random data based on the
// value of `faker` tag of exported elements. The faker tag value can be any
// available function (case insensitive).
//
// Use `faker:"-"` to explicitly skip an element.
//
// Use `faker:"skip if FieldName"` to explicitly skip this field if another
// field (FieldName) is not empty.
//
// Use `faker:"unique"` to guarantee a unique value.
//
// Use `faker:"len=x"` to specify the length of a slice or the size of a map
// (if ommitted, will be generated a slice or map with random size between 1
// and 8).
//
// Built-in supported types are: bool, int, int8, int16, int32, int64, uint,
// uint8, uint16, uint32, uint64, float32, float64, string. Other standard
// library supported types are time.Time and time.Duration. But is really easy
// to extend faker to add other builders to support other types and or
// customize faker's behavior (see RegisterBuilder function).
func Build(input interface{}) error {
inputReflectType := reflect.TypeOf(input)
if inputReflectType == nil {
return errors.New("faker.Build input interface{} not allowed")
}
if inputReflectType.Kind() != reflect.Ptr {
return errors.New("faker.Build input is not a pointer")
}
parentReflectTypes := make(map[reflect.Type]struct{})
inputReflectValue := reflect.ValueOf(input)
err := build(inputReflectValue, parentReflectTypes, &fakerTag{})
if err != nil {
return err
}
return nil
}
func build(inputReflectValue reflect.Value, parentReflectTypes map[reflect.Type]struct{}, tag *fakerTag) error {
inputReflectType := inputReflectValue.Type()
kind := inputReflectType.Kind()
var (
fn builderFunc
found bool
key string
)
key = builderKey(tag.funcName, inputReflectType.String())
fn, found = builders[key]
if found {
if !inputReflectValue.IsZero() {
return nil
}
var (
value interface{}
err error
)
if tag.uniqueGroup != "" {
value, err = Uniq(tag.uniqueGroup, 0, func() (interface{}, error) {
return fn(tag.params...)
})
} else {
value, err = fn(tag.params...)
}
if err != nil {
return err
}
inputReflectValue.Set(reflect.ValueOf(value))
return nil
}
switch kind {
case reflect.Ptr:
return buildPtr(inputReflectValue, inputReflectType, parentReflectTypes, tag)
case reflect.Struct:
return buildStruct(inputReflectValue, inputReflectType, parentReflectTypes, tag)
case reflect.Slice:
return buildSlice(inputReflectValue, inputReflectType, parentReflectTypes, tag)
case reflect.Map:
return buildMap(inputReflectValue, inputReflectType, parentReflectTypes, tag)
default:
if tag.funcName != "" {
return fmt.Errorf("invalid faker function '%s' for type '%s'", tag.funcName, inputReflectType.String())
}
return nil
}
}
func buildPtr(inputReflectValue reflect.Value, inputReflectType reflect.Type, parentReflectTypes map[reflect.Type]struct{}, tag *fakerTag) error {
if detectInfiniteLoopRecursion(parentReflectTypes, inputReflectType) {
return nil
}
if inputReflectValue.IsNil() {
newVar := reflect.New(inputReflectType.Elem())
inputReflectValue.Set(newVar)
}
return build(inputReflectValue.Elem(), parentReflectTypes, tag)
}
func buildStruct(inputReflectValue reflect.Value, inputReflectType reflect.Type, parentReflectTypes map[reflect.Type]struct{}, tag *fakerTag) error {
if detectInfiniteLoopRecursion(parentReflectTypes, inputReflectType) {
return nil
}
parentReflectTypesCopy := make(map[reflect.Type]struct{})
for k, v := range parentReflectTypes {
parentReflectTypesCopy[k] = v
}
parentReflectTypesCopy[inputReflectType] = struct{}{}
for i := 0; i < inputReflectValue.NumField(); i++ {
fieldTag := decodeTag(inputReflectType, i)
if fieldTag.mustSkip() {
continue
}
if fieldTag.skipIf != "" {
skipIfReflectValue := inputReflectValue.FieldByName(fieldTag.skipIf)
if (skipIfReflectValue != reflect.Value{}) && !skipIfReflectValue.IsZero() {
continue
}
}
if !inputReflectValue.Field(i).CanSet() {
continue // to avoid panic to set on unexported field in struct
}
err := build(inputReflectValue.Field(i), parentReflectTypesCopy, fieldTag)
if err != nil {
return err
}
}
return nil
}
func buildSlice(inputReflectValue reflect.Value, inputReflectType reflect.Type, parentReflectTypes map[reflect.Type]struct{}, tag *fakerTag) error {
if detectInfiniteLoopRecursion(parentReflectTypes, inputReflectType.Elem()) {
return nil
}
if inputReflectValue.IsNil() {
var sliceLen int
if tag != nil && tag.length != 0 {
sliceLen = tag.length
} else {
sliceLen = IntInRange(1, 8)
}
newSlice := reflect.MakeSlice(inputReflectType, sliceLen, sliceLen)
inputReflectValue.Set(newSlice)
return buildSliceElems(inputReflectValue, parentReflectTypes, tag)
}
if len(parentReflectTypes) == 0 {
return buildSliceElems(inputReflectValue, parentReflectTypes, tag)
}
return nil
}
func buildMap(inputReflectValue reflect.Value, inputReflectType reflect.Type, parentReflectTypes map[reflect.Type]struct{}, tag *fakerTag) error {
keyReflectType := inputReflectType.Key()
if detectInfiniteLoopRecursion(parentReflectTypes, keyReflectType) {
return nil
}
elemReflectType := inputReflectType.Elem()
if detectInfiniteLoopRecursion(parentReflectTypes, elemReflectType) {
return nil
}
if inputReflectValue.IsNil() {
var (
mapLen int
key reflect.Value
elem reflect.Value
err error
)
if tag != nil && tag.length != 0 {
mapLen = tag.length
} else {
mapLen = IntInRange(1, 8)
}
newMap := reflect.MakeMap(inputReflectType)
for i := 0; i < mapLen; i++ {
key = reflect.New(keyReflectType).Elem()
elem = reflect.New(elemReflectType).Elem()
err = build(key, parentReflectTypes, tag)
if err != nil {
return err
}
err = build(elem, parentReflectTypes, tag)
if err != nil {
return err
}
newMap.SetMapIndex(key, elem)
}
inputReflectValue.Set(newMap)
}
return nil
}
func buildSliceElems(inputReflectValue reflect.Value, parentReflectTypes map[reflect.Type]struct{}, tag *fakerTag) error {
for i := 0; i < inputReflectValue.Len(); i++ {
err := build(inputReflectValue.Index(i), parentReflectTypes, tag)
if err != nil {
return err
}
}
return nil
}
func detectInfiniteLoopRecursion(parentReflectTypes map[reflect.Type]struct{}, reflectType reflect.Type) bool {
for {
if reflectType.Kind() == reflect.Ptr {
reflectType = reflectType.Elem()
} else {
break
}
}
_, found := parentReflectTypes[reflectType]
return found
}