-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
struct.go
190 lines (170 loc) · 4.55 KB
/
struct.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
package faker
import (
"reflect"
"strconv"
)
// Struct is a faker struct for Struct
type Struct struct {
Faker *Faker
}
// Fill elements of a struct with random data
func (s Struct) Fill(v interface{}) {
s.r(reflect.TypeOf(v), reflect.ValueOf(v), "", 0)
}
func (s Struct) r(t reflect.Type, v reflect.Value, function string, size int) {
switch t.Kind() {
case reflect.Ptr:
s.rPointer(t, v, function)
case reflect.Struct:
s.rStruct(t, v)
case reflect.String:
s.rString(t, v, function)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
s.rUint(t, v, function)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s.rInt(t, v, function)
case reflect.Float32, reflect.Float64:
s.rFloat(t, v, function)
case reflect.Bool:
s.rBool(t, v)
case reflect.Array, reflect.Slice:
s.rSlice(t, v, function, size)
}
}
func (s Struct) rStruct(t reflect.Type, v reflect.Value) {
n := t.NumField()
for i := 0; i < n; i++ {
elementT := t.Field(i)
elementV := v.Field(i)
t, ok := elementT.Tag.Lookup("fake")
if ok && t == "skip" {
// Do nothing, skip it
} else if elementV.CanSet() {
// Check if fakesize is set
size := -1 // Set to -1 to indicate fakesize was not set
fs, ok := elementT.Tag.Lookup("fakesize")
if ok {
var err error
size, err = strconv.Atoi(fs)
if err != nil {
size = s.Faker.IntBetween(1, 10)
}
}
s.r(elementT.Type, elementV, t, size)
}
}
}
func (s Struct) rPointer(t reflect.Type, v reflect.Value, function string) {
elemT := t.Elem()
if v.IsNil() {
nv := reflect.New(elemT)
s.r(elemT, nv.Elem(), function, 0)
v.Set(nv)
} else {
s.r(elemT, v.Elem(), function, 0)
}
}
func (s Struct) rSlice(t reflect.Type, v reflect.Value, function string, size int) {
// If you cant even set it dont even try
if !v.CanSet() {
return
}
// Grab original size to use if needed for sub arrays
ogSize := size
// If the value has a cap and is less than the size
// use that instead of the requested size
elemCap := v.Cap()
if elemCap == 0 && size == -1 {
size = s.Faker.IntBetween(1, 10)
} else if elemCap != 0 && (size == -1 || elemCap < size) {
size = elemCap
}
// Get the element type
elemT := t.Elem()
// If values are already set fill them up, otherwise append
if v.Len() != 0 {
// Loop through the elements length and set based upon the index
for i := 0; i < size; i++ {
nv := reflect.New(elemT)
s.r(elemT, nv.Elem(), function, ogSize)
v.Index(i).Set(reflect.Indirect(nv))
}
} else {
// Loop through the size and append and set
for i := 0; i < size; i++ {
nv := reflect.New(elemT)
s.r(elemT, nv.Elem(), function, ogSize)
v.Set(reflect.Append(reflect.Indirect(v), reflect.Indirect(nv)))
}
}
}
func (s Struct) rString(_ 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) rInt(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) rUint(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) rFloat(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) rBool(_ reflect.Type, v reflect.Value) {
v.SetBool(s.Faker.Bool())
}