-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfaker.go
71 lines (63 loc) · 1.78 KB
/
faker.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
package faker
import (
"fmt"
"time"
"github.com/Rulox/faker/generator"
"math/rand"
)
const defaultLocale = "en_US"
// Base struct for all generators.
type Faker struct {
// If the generator should return always unique values. Note that this will have some limitations.
// If you want more data than the available unique data, the Generator won't fail but it'll print a warning to
// let you know it was impossible to generate unique data for you.
Unique bool
// Map to store the values in case unique is set to true
used map[string]interface{}
// Locale set. Depending on the locale, the generators will return the data in the selected language
locale string
// Misc data generator
Misc generator.MiscGenerator
// Address data generator
Address generator.AddressGenerator
// Person data generator
Person generator.PersonGenerator
// Phone data generator
Phone generator.PhoneGenerator
}
// Return a new Faker to start working with. It is necessary to use this 'constructor' in order
// to initialize some variables and to run the default locale `en_US`
func NewFaker(l string) (f Faker) {
rand.Seed(time.Now().Unix())
f.locale = l
if len(l) == 0 {
f.locale = defaultLocale
}
f.SetLocale(f.locale)
return f
}
// Set locale for all the generators
func (f *Faker) SetLocale(l string) error {
f.locale = l
err := f.Address.SetLocale(l)
// TODO change this for an array of errors to avoid repeating code and return a custom error
if err != nil {
fmt.Println(err)
return err
}
err = f.Person.SetLocale(l)
if err != nil {
fmt.Println(err)
return err
}
err = f.Phone.SetLocale(l)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
// Clear the unique values storage so values can be repeated again
func (f *Faker) Clear() {
f.used = make(map[string]interface{})
}