1
1
package getters
2
2
3
- import (
4
- "fmt"
5
- "math"
6
- "math/rand"
7
- "time"
8
-
9
- "github.com/icrowley/fake"
10
- )
11
-
12
3
const (
13
4
nilFrequency = 10
14
5
oneYear = int64 (60 * 60 * 24 * 365 )
@@ -18,278 +9,3 @@ type Getter interface {
18
9
Value () interface {}
19
10
String () string
20
11
}
21
-
22
- type RandomInt struct {
23
- name string
24
- mask int64
25
- allowNull bool
26
- }
27
-
28
- func (r * RandomInt ) Value () interface {} {
29
- return rand .Int63n (r .mask )
30
- }
31
-
32
- func (r * RandomInt ) String () string {
33
- return fmt .Sprintf ("%d" , r .Value ())
34
- }
35
-
36
- func NewRandomInt (name string , mask int64 , allowNull bool ) Getter {
37
- return & RandomInt {name , mask , allowNull }
38
- }
39
-
40
- type RandomIntRange struct {
41
- name string
42
- min int64
43
- max int64
44
- allowNull bool
45
- }
46
-
47
- func (r * RandomIntRange ) Value () interface {} {
48
- limit := r .max - r .min + 1
49
- return r .min + rand .Int63n (limit )
50
- }
51
-
52
- func (r * RandomIntRange ) String () string {
53
- return fmt .Sprintf ("%d" , r .Value ())
54
- }
55
-
56
- func NewRandomIntRange (name string , min , max int64 , allowNull bool ) Getter {
57
- return & RandomIntRange {name , min , max , allowNull }
58
- }
59
-
60
- type RandomDecimal struct {
61
- name string
62
- size int64
63
- allowNull bool
64
- }
65
-
66
- func (r * RandomDecimal ) Value () interface {} {
67
- f := rand .Float64 () * float64 (rand .Int63n (int64 (math .Pow10 (int (r .size )))))
68
- return f
69
- }
70
-
71
- func (r * RandomDecimal ) String () string {
72
- return fmt .Sprintf ("%0f" , r .Value ())
73
- }
74
-
75
- func NewRandomDecimal (name string , size int64 , allowNull bool ) Getter {
76
- return & RandomDecimal {name , size , allowNull }
77
- }
78
-
79
- // RandomString getter
80
- type RandomString struct {
81
- name string
82
- maxSize int64
83
- allowNull bool
84
- }
85
-
86
- func (r * RandomString ) Value () interface {} {
87
- if r .allowNull && rand .Int63n (100 ) < nilFrequency {
88
- return nil
89
- }
90
- var s string
91
- maxSize := uint64 (r .maxSize )
92
- if maxSize == 0 {
93
- maxSize = uint64 (rand .Int63n (100 ))
94
- }
95
-
96
- if maxSize <= 10 {
97
- s = fake .FirstName ()
98
- } else if maxSize < 30 {
99
- s = fake .FullName ()
100
- } else {
101
- s = fake .Sentence ()
102
- }
103
- if len (s ) > int (maxSize ) {
104
- s = s [:int (maxSize )]
105
- }
106
- return s
107
- }
108
-
109
- func (r * RandomString ) String () string {
110
- v := r .Value ()
111
- if v == nil {
112
- return "NULL"
113
- }
114
- return fmt .Sprintf ("%q" , v )
115
- }
116
-
117
- func NewRandomString (name string , maxSize int64 , allowNull bool ) Getter {
118
- return & RandomString {name , maxSize , allowNull }
119
- }
120
-
121
- type RandomDate struct {
122
- name string
123
- allowNull bool
124
- }
125
-
126
- func (r * RandomDate ) Value () interface {} {
127
- var randomSeconds time.Duration
128
- for i := 0 ; i < 10 && randomSeconds != 0 ; i ++ {
129
- randomSeconds = time .Duration (rand .Int63n (int64 (oneYear )) + rand .Int63n (100 ))
130
- }
131
- d := time .Now ().Add (- 1 * randomSeconds )
132
- return d
133
- }
134
-
135
- func (r * RandomDate ) String () string {
136
- d := r .Value ().(time.Time )
137
- return fmt .Sprintf ("'%s'" , d .Format ("2006-01-02 15:03:04" ))
138
- }
139
-
140
- func NewRandomDate (name string , allowNull bool ) Getter {
141
- return & RandomDate {name , allowNull }
142
- }
143
-
144
- type RandomDateInRange struct {
145
- name string
146
- min string
147
- max string
148
- allowNull bool
149
- }
150
-
151
- func (r * RandomDateInRange ) Value () interface {} {
152
- rand .Seed (time .Now ().UnixNano ())
153
- var randomSeconds int64
154
- randomSeconds = rand .Int63n (oneYear ) + rand .Int63n (100 )
155
- d := time .Now ().Add (- 1 * time .Duration (randomSeconds ) * time .Second )
156
- return d
157
- }
158
-
159
- func (r * RandomDateInRange ) String () string {
160
- d := r .Value ().(time.Time )
161
- return fmt .Sprintf ("'%s'" , d .Format ("2006-01-02 15:03:04" ))
162
- }
163
-
164
- func NewRandomDateInRange (name string , min , max string , allowNull bool ) Getter {
165
- if min == "" {
166
- t := time .Now ().Add (- 1 * time .Duration (oneYear ) * time .Second )
167
- min = t .Format ("2006-01-02" )
168
- }
169
- return & RandomDateInRange {name , min , max , allowNull }
170
- }
171
-
172
- type RandomDateTimeInRange struct {
173
- min string
174
- max string
175
- allowNull bool
176
- }
177
-
178
- func (r * RandomDateTimeInRange ) Value () interface {} {
179
- rand .Seed (time .Now ().UnixNano ())
180
- randomSeconds := rand .Int63n (oneYear )
181
- d := time .Now ().Add (- 1 * time .Duration (randomSeconds ) * time .Second )
182
- return d
183
- }
184
-
185
- func (r * RandomDateTimeInRange ) String () string {
186
- d := r .Value ().(time.Time )
187
- return fmt .Sprintf ("'%s'" , d .Format ("2006-01-02 15:03:04" ))
188
- }
189
-
190
- func NewRandomDateTimeInRange (name string , min , max string , allowNull bool ) Getter {
191
- if min == "" {
192
- t := time .Now ().Add (- 1 * time .Duration (oneYear ) * time .Second )
193
- min = t .Format ("2006-01-02" )
194
- }
195
- return & RandomDateInRange {name , min , max , allowNull }
196
- }
197
-
198
- func NewRandomDateTime (name string , allowNull bool ) Getter {
199
- return & RandomDateInRange {name , "" , "" , allowNull }
200
- }
201
-
202
- // RandomTime Getter
203
- type RandomTime struct {
204
- allowNull bool
205
- }
206
-
207
- func (r * RandomTime ) Value () interface {} {
208
- h := rand .Int63n (24 )
209
- m := rand .Int63n (60 )
210
- s := rand .Int63n (60 )
211
- return fmt .Sprintf ("'%02d:%02d:%02d'" , h , m , s )
212
- }
213
-
214
- func (r * RandomTime ) String () string {
215
- return r .Value ().(string )
216
- }
217
-
218
- func NewRandomTime (allowNull bool ) Getter {
219
- return & RandomTime {allowNull }
220
- }
221
-
222
- // RandomEnum Getter
223
- type RandomEnum struct {
224
- allowedValues []string
225
- allowNull bool
226
- }
227
-
228
- func (r * RandomEnum ) Value () interface {} {
229
- //rand.Seed(time.Now().UnixNano())
230
- if r .allowNull && rand .Int63n (100 ) < nilFrequency {
231
- return nil
232
- }
233
- i := rand .Int63n (int64 (len (r .allowedValues )))
234
- return r .allowedValues [i ]
235
- }
236
-
237
- func (r * RandomEnum ) String () string {
238
- if v := r .Value (); v != nil {
239
- return fmt .Sprintf ("%q" , v )
240
- }
241
- return "NULL"
242
- }
243
-
244
- func NewRandomEnum (allowedValues []string , allowNull bool ) Getter {
245
- return & RandomEnum {allowedValues , allowNull }
246
- }
247
-
248
- type RandomSample struct {
249
- name string
250
- samples []interface {}
251
- allowNull bool
252
- }
253
-
254
- func (r * RandomSample ) Value () interface {} {
255
- if r .allowNull && rand .Int63n (100 ) < nilFrequency {
256
- return nil
257
- }
258
- pos := rand .Int63n (int64 (len (r .samples )))
259
- return r .samples [pos ]
260
- }
261
-
262
- func (r * RandomSample ) String () string {
263
- v := r .Value ()
264
- if v == nil {
265
- return "NULL"
266
- }
267
- switch v .(type ) {
268
- case string :
269
- return fmt .Sprintf ("%q" , v )
270
- default :
271
- return fmt .Sprintf ("%v" , v )
272
- }
273
- }
274
-
275
- func NewRandomSample (name string , samples []interface {}, allowNull bool ) * RandomSample {
276
- r := & RandomSample {name , samples , allowNull }
277
- return r
278
- }
279
-
280
- // Constant Getter. Used for debugging
281
- type Constant struct {
282
- value interface {}
283
- }
284
-
285
- func (r * Constant ) Value () interface {} {
286
- return r .value
287
- }
288
-
289
- func (r * Constant ) String () string {
290
- return fmt .Sprintf ("%q" , r .Value ())
291
- }
292
-
293
- func NewConstant (value interface {}) Getter {
294
- return & Constant {value }
295
- }
0 commit comments