-
Notifications
You must be signed in to change notification settings - Fork 0
/
faker.go
63 lines (49 loc) · 1.46 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
package faker
import (
"fmt"
"math/rand"
"time"
"github.com/dimiro1/faker/locales"
)
// DefaultLocale is a constant that defines the default locale used in the default configuration.
const DefaultLocale = "en"
// Faker is the main API
// it also holds some config
type Faker struct {
CurrentLocaleString string
locales map[string]locales.Locale
}
// Options is used to pass configuration options to Faker
type Options struct {
Locale string
Seed int64
}
// CurrentLocale returns the current locale object
func (f Faker) CurrentLocale() locales.Locale {
return f.locales[f.CurrentLocaleString]
}
// NewDefault creates a new Faker with default configuration
func NewDefault() Faker {
f, err := NewForLocale(DefaultLocale)
if err != nil {
panic(fmt.Sprintf("NewDefaultFaker: NewFaker returned an error with valid options %+v\n", err))
}
return f
}
// NewForLocale creates a new Faker with the specified locale and random seed
func NewForLocale(locale string) (Faker, error) {
return New(Options{Locale: locale, Seed: time.Now().UTC().UnixNano()})
}
// New creates a new Faker with configuration passed in options
func New(options Options) (Faker, error) {
// Validate Locale
if !locales.IsValid(options.Locale) {
return Faker{}, fmt.Errorf("NewFaker: %s is an invalid locale", options.Locale)
}
// Seeding the random
rand.Seed(options.Seed)
return Faker{
CurrentLocaleString: options.Locale,
locales: locales.Supported(),
}, nil
}