-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
79 lines (65 loc) · 1.71 KB
/
utils.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
package main
import (
"encoding/json"
"io"
"math/rand"
"os"
"time"
"github.com/bxcodec/faker/v3"
)
func openFileAppendly(filename string) (io.Writer, func()) {
f, err := os.OpenFile(filename,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
return f, func() {
err := f.Close()
if err != nil {
panic(err)
}
}
}
func randomIntInclusiveRange(min, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min+1) + min
}
type void struct{}
func randomTimer(earliest, latest int) chan void {
ch := make(chan void)
go func() {
var loop func()
loop = func() {
n := randomIntInclusiveRange(earliest, latest)
time.Sleep(time.Duration(n) * time.Second)
ch <- void{}
loop()
}
loop()
}()
return ch
}
type FakeUser struct {
UUID string `json:"uuid" faker:"uuid_digit"`
Name string `json:"name" faker:"name"`
PhoneNumber string `json:"phone_number" faker:"phone_number"`
Email string `faker:"email" json:"email"`
Latitude float32 `faker:"lat" json:"latitude"`
Longitude float32 `faker:"long" json:"longitude"`
DomainName string `faker:"domain_name" json:"domain_name"`
IPV4 string `faker:"ipv4" json:"ipv4"`
IPV6 string `faker:"ipv6" json:"ipv6"`
CreditCardNumber string `faker:"cc_number" json:"cc_number"`
CreditCardType string `faker:"cc_type" json:"cc_type"`
PaymentMethod string `faker:"oneof: cc, paypal, check, money order" json:"payment_method"` // oneof will randomly pick one of the comma-separated values supplied in the tag
Comment string `faker:"sentence" json:"comment"`
}
func FakeData() string {
a := FakeUser{}
err := faker.FakeData(&a)
if err != nil {
panic(err)
}
d, _ := json.Marshal(a)
return string(d)
}