-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
120 lines (105 loc) · 3.37 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/brianvoe/gofakeit/v6"
)
func main() {
Sample("Simple", Simple)
Sample("Struct", Struct)
Sample("Custom", Custom)
}
func Simple() {
gofakeit.Seed(0)
fmt.Println("Name:", gofakeit.Name())
fmt.Println("Email:", gofakeit.Email())
fmt.Println("Phone:", gofakeit.Phone())
fmt.Println("BS:", gofakeit.BS())
fmt.Println("BeerName:", gofakeit.BeerName())
fmt.Println("Color:", gofakeit.Color())
fmt.Println("Company:", gofakeit.Company())
fmt.Println("CreditCardNumber:", gofakeit.CreditCardNumber(&gofakeit.CreditCardOptions{}))
fmt.Println("HackerPhrase:", gofakeit.HackerPhrase())
fmt.Println("JobTitle:", gofakeit.JobTitle())
fmt.Println("CurrencyShort:", gofakeit.CurrencyShort())
}
func Struct() {
type Foo struct {
Bar string
Int int
Pointer *int
Name string `fake:"{firstname}"` // Any available function all lowercase
Sentence string `fake:"{sentence:3}"` // Can call with parameters
RandStr string `fake:"{randomstring:[hello,world]}"`
Number string `fake:"{number:1,10}"` // Comma separated for multiple values
Regex string `fake:"{regex:[abcdef]{5}}"` // Generate string from regex
Skip *string `fake:"skip"` // Set to "skip" to not generate data for
Created time.Time // Can take in a fake tag as well as a format tag
CreatedFormat time.Time `fake:"{year}-{month}-{day}" format:"2006-01-02"`
}
type FooBar struct {
Bars []string `fake:"{name}"` // Array of random size (1-10) with fake function applied
Foos []Foo `fakesize:"3"` // Array of size specified with faked struct
FooBars []Foo `fake:"{name}" fakesize:"3"` // Array of size 3 with fake function applied
}
var f Foo
gofakeit.Struct(&f)
fmt.Println(f.Bar)
fmt.Println(f.Int)
fmt.Println(*f.Pointer)
fmt.Println(f.Name)
fmt.Println(f.Sentence)
fmt.Println(f.RandStr)
fmt.Println(f.Number)
fmt.Println(f.Regex)
fmt.Println(f.Skip)
fmt.Println(f.Created.String())
var fb FooBar
gofakeit.Struct(&fb)
fmt.Println(fb.Bars)
fmt.Println(fb.Foos)
}
func Custom() {
gofakeit.AddFuncLookup("friendname", gofakeit.Info{
Category: "custom",
Description: "Random friend name",
Example: "bill",
Output: "string",
Generate: func(r *rand.Rand, m *gofakeit.MapParams, info *gofakeit.Info) (interface{}, error) {
return gofakeit.RandomString([]string{"bill", "bob", "sally"}), nil
},
})
gofakeit.AddFuncLookup("jumbleword", gofakeit.Info{
Category: "jumbleword",
Description: "Take a word and jumple it up",
Example: "loredlowlh",
Output: "string",
Params: []gofakeit.Param{
{Field: "word", Type: "int", Description: "Word you want to jumble"},
},
Generate: func(r *rand.Rand, m *gofakeit.MapParams, info *gofakeit.Info) (interface{}, error) {
word, err := info.GetString(m, "word")
if err != nil {
return nil, err
}
split := strings.Split(word, "")
gofakeit.ShuffleStrings(split)
return strings.Join(split, ""), nil
},
})
type Foo struct {
FriendName string `fake:"{friendname}"`
JumbleWord string `fake:"{jumbleword:helloworld}"`
}
var f Foo
gofakeit.Struct(&f)
fmt.Printf("%s", f.FriendName) // bill
fmt.Printf("%s", f.JumbleWord) // loredlowlh
}
func Sample(name string, fn func()) {
fmt.Println(">", name)
fn()
fmt.Println()
}