forked from bxcodec/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_custom_faker_test.go
60 lines (51 loc) · 1.31 KB
/
example_custom_faker_test.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
package faker_test
import (
"fmt"
"reflect"
"github.com/bxcodec/faker/v3"
)
// Gondoruwo ...
type Gondoruwo struct {
Name string
Locatadata int
}
// custom type that aliases over slice of byte
type CustomUUID []byte
// Sample ...
type Sample struct {
ID int64 `faker:"customIdFaker"`
Gondoruwo Gondoruwo `faker:"gondoruwo"`
Danger string `faker:"danger"`
UUID CustomUUID `faker:"customUUID"`
}
// CustomGenerator ...
func CustomGenerator() {
_ = faker.AddProvider("customIdFaker", func(v reflect.Value) (interface{}, error) {
return int64(43), nil
})
_ = faker.AddProvider("danger", func(v reflect.Value) (interface{}, error) {
return "danger-ranger", nil
})
_ = faker.AddProvider("gondoruwo", func(v reflect.Value) (interface{}, error) {
obj := Gondoruwo{
Name: "Power",
Locatadata: 324,
}
return obj, nil
})
_ = faker.AddProvider("customUUID", func(v reflect.Value) (interface{}, error) {
s := CustomUUID{
0, 8, 7, 2, 3,
}
return s, nil
})
}
// You can also add your own generator function to your own defined tags.
func Example_customFaker() {
CustomGenerator()
var sample Sample
_ = faker.FakeData(&sample, true)
fmt.Printf("%+v", sample)
// Output:
// {ID:43 Gondoruwo:{Name:Power Locatadata:324} Danger:danger-ranger UUID:[0 8 7 2 3]}
}