-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathstruct.go
More file actions
293 lines (249 loc) · 7.08 KB
/
struct.go
File metadata and controls
293 lines (249 loc) · 7.08 KB
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
package faker
import (
"fmt"
"math"
"math/big"
"reflect"
"strconv"
"strings"
"time"
)
// Struct is a faker struct for generating random data for struct fields
type Struct struct {
Faker *Faker
}
// MaxRecursionDepth defines the default maximum depth for recursive structs
const MaxRecursionDepth = 32
type fakerFunction func() interface{}
var functions = map[string]fakerFunction{}
func RegisterFunction(name string, function fakerFunction) {
functions[fmt.Sprintf("fn=%s", name)] = function
}
// Fill populates a struct with random data based on its type and tags
func (s Struct) Fill(v interface{}) {
if v == nil {
return
}
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr || val.IsNil() {
return
}
s.fillValue(val.Elem(), "", -1, 0, MaxRecursionDepth)
}
func (s Struct) FillWithDepth(v interface{}, maxDepth int) {
if v == nil {
return
}
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr || val.IsNil() {
return
}
s.fillValue(val.Elem(), "", -1, 0, maxDepth)
}
// fillValue recursively fills a reflect.Value with random data
func (s Struct) fillValue(v reflect.Value, function string, size int, depth int, maxDepth int) {
if !v.CanSet() || depth > maxDepth {
return
}
if strings.HasPrefix(function, "fn=") && s.fillFunction(v.Type(), v, function) {
return
}
switch v.Kind() {
case reflect.Pointer:
s.fillPointer(v, function, size, depth, maxDepth)
case reflect.Struct:
s.fillStruct(v, depth, maxDepth)
case reflect.String:
s.fillString(v.Type(), v, function)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
s.fillUint(v.Type(), v, function)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s.fillInt(v.Type(), v, function)
case reflect.Float32, reflect.Float64:
s.fillFloat(v.Type(), v, function)
case reflect.Bool:
s.fillBool(v.Type(), v)
case reflect.Array, reflect.Slice:
s.fillSlice(v, function, size, depth, maxDepth)
case reflect.Map:
s.fillMap(v.Type(), v, function, size, depth, maxDepth)
}
}
// fillStruct fills a struct with random data for each field
func (s Struct) fillStruct(v reflect.Value, depth int, maxDepth int) {
if v.Type().ConvertibleTo(reflect.TypeOf(time.Time{})) {
v.Set(reflect.ValueOf(time.Unix(int64(s.Faker.Int32()), 0)).Convert(v.Type()))
return
}
if v.Type().ConvertibleTo(reflect.TypeOf(big.Rat{})) {
v.Set(reflect.ValueOf(*big.NewRat(s.Faker.Int64Between(1, math.MaxInt64), s.Faker.Int64Between(1, math.MaxInt64))).Convert(v.Type()))
return
}
typ := v.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
fieldVal := v.Field(i)
// Skip fields with "skip" tag
if tag, ok := field.Tag.Lookup("fake"); ok && tag == "skip" {
continue
}
// Get size from fakesize tag if present
size := -1
if sizeStr, ok := field.Tag.Lookup("fakesize"); ok {
if parsedSize, err := strconv.Atoi(sizeStr); err == nil {
size = parsedSize
}
}
// Get function from fake tag if present
function := ""
if tag, ok := field.Tag.Lookup("fake"); ok {
function = tag
}
s.fillValue(fieldVal, function, size, depth+1, maxDepth)
}
}
// fillPointer handles pointer types by creating new instances if needed
func (s Struct) fillPointer(v reflect.Value, function string, size int, depth int, maxDepth int) {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
s.fillValue(v.Elem(), function, size, depth+1, maxDepth)
}
// fillSlice handles array and slice types
func (s Struct) fillSlice(v reflect.Value, function string, size int, depth int, maxDepth int) {
if !v.CanSet() {
return
}
// Determine the size to use
actualSize := size
if actualSize == -1 {
actualSize = s.Faker.IntBetween(defaultSliceMinSize, defaultSliceMaxSize)
}
if v.Cap() > 0 && (size == -1 || v.Cap() < size) {
actualSize = v.Cap()
}
// Get element type
elemType := v.Type().Elem()
// Handle existing elements
if v.Len() > 0 {
for i := 0; i < actualSize; i++ {
if i < v.Len() {
s.fillValue(v.Index(i), function, size, depth+1, maxDepth)
} else {
elem := reflect.New(elemType).Elem()
s.fillValue(elem, function, size, depth+1, maxDepth)
v.Set(reflect.Append(v, elem))
}
}
return
}
// Create new elements
for i := 0; i < actualSize; i++ {
elem := reflect.New(elemType).Elem()
s.fillValue(elem, function, size, depth+1, maxDepth)
v.Set(reflect.Append(v, elem))
}
}
func (s Struct) fillString(_ reflect.Type, v reflect.Value, function string) {
if function == "" {
v.SetString(s.Faker.UUID().V4())
return
}
v.SetString(s.Faker.Bothify(function))
}
func (s Struct) fillInt(t reflect.Type, v reflect.Value, function string) {
if function != "" {
i, err := strconv.ParseInt(s.Faker.Numerify(function), 10, 64)
if err == nil {
v.SetInt(i)
return
}
}
// If no function or error converting to int, set with random value
switch t.Kind() {
case reflect.Int:
v.SetInt(s.Faker.Int64())
case reflect.Int8:
v.SetInt(int64(s.Faker.Int8()))
case reflect.Int16:
v.SetInt(int64(s.Faker.Int16()))
case reflect.Int32:
v.SetInt(int64(s.Faker.Int32()))
case reflect.Int64:
v.SetInt(s.Faker.Int64())
}
}
func (s Struct) fillUint(t reflect.Type, v reflect.Value, function string) {
if function != "" {
u, err := strconv.ParseUint(s.Faker.Numerify(function), 10, 64)
if err == nil {
v.SetUint(u)
return
}
}
// If no function or error converting to uint, set with random value
switch t.Kind() {
case reflect.Uint:
v.SetUint(s.Faker.UInt64())
case reflect.Uint8:
v.SetUint(uint64(s.Faker.UInt8()))
case reflect.Uint16:
v.SetUint(uint64(s.Faker.UInt16()))
case reflect.Uint32:
v.SetUint(uint64(s.Faker.UInt32()))
case reflect.Uint64:
v.SetUint(s.Faker.UInt64())
}
}
func (s Struct) fillFloat(t reflect.Type, v reflect.Value, function string) {
if function != "" {
f, err := strconv.ParseFloat(s.Faker.Numerify(function), 64)
if err == nil {
v.SetFloat(f)
return
}
}
// If no function or error converting to float, set with random value
switch t.Kind() {
case reflect.Float64:
v.SetFloat(s.Faker.Float64(2, 0, 100))
case reflect.Float32:
v.SetFloat(s.Faker.Float64(2, 0, 100))
}
}
func (s Struct) fillBool(_ reflect.Type, v reflect.Value) {
v.SetBool(s.Faker.Bool())
}
func (Struct) fillFunction(t reflect.Type, v reflect.Value, function string) bool {
f, ok := functions[function]
if !ok {
return false
}
val := f()
vType := reflect.TypeOf(val)
if !vType.AssignableTo(t) {
return false
}
vVal := reflect.ValueOf(val)
v.Set(vVal)
return true
}
func (s Struct) fillMap(t reflect.Type, v reflect.Value, function string, size int, depth int, maxDepth int) {
if !v.CanSet() {
return
}
mapType := reflect.MapOf(t.Key(), t.Elem())
newMap := reflect.MakeMap(mapType)
newSize := size
if newSize == -1 {
newSize = s.Faker.IntBetween(defaultSliceMinSize, defaultSliceMaxSize)
}
for i := 0; i < newSize; i++ {
mapIndex := reflect.New(t.Key())
s.fillValue(mapIndex.Elem(), function, size, depth+1, maxDepth)
mapValue := reflect.New(t.Elem())
s.fillValue(mapValue.Elem(), function, size, depth+1, maxDepth)
newMap.SetMapIndex(mapIndex.Elem(), mapValue.Elem())
}
v.Set(newMap)
}