-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
59 lines (48 loc) · 1.36 KB
/
helper.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
package facto
import (
"math/rand"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/gofrs/uuid"
)
// Helper gets injected into the factory and provides convenience methods
// for the fixtures.
type Helper struct {
// Faker instance to provide the ability to create fake data to the helper.
Faker *gofakeit.Faker
// Index is useful when creating N elements, this
// Allow to differentiate the elements by the index.
Index int
}
// NamedUUID is a helper to create a UUID and keep it in the
// Facto context for later use this could come handy for database relations.
func (h Helper) NamedUUID(name string) uuid.UUID {
name = "uuid_" + name
if v := defaultRegistry.getVariable(name); v != nil {
return v.(uuid.UUID)
}
uuid := uuid.Must(uuid.NewV4())
defaultRegistry.setVariable(name, uuid)
return uuid
}
// One of the passed elements, this method is useful when you
// have enum values and you want the falue of a field to be
// one of the possible values.
// e.g.
// ...
// u := User{
// // here the value of the field is one of the passed elements.
// Status: OneOf(UserStatusActive, UserStatusInactive).(UserStatus)
// }
func (h Helper) OneOf(values ...interface{}) interface{} {
if len(values) == 0 {
return nil
}
rand.Seed(time.Now().Unix())
return values[rand.Intn(len(values))]
}
func NewHelper() Helper {
return Helper{
Faker: gofakeit.New(0),
}
}