2024-04-26 19:30:35 +00:00
|
|
|
package gofakeit
|
|
|
|
|
|
|
|
import (
|
|
|
|
crand "crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create global variable to deal with global function call.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
var globalFaker *Faker = New(0)
|
|
|
|
|
|
|
|
// Faker struct is the primary struct for using localized.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
type Faker struct {
|
|
|
|
Rand *rand.Rand
|
|
|
|
}
|
|
|
|
|
|
|
|
type lockedSource struct {
|
2024-07-24 23:45:04 +00:00
|
|
|
lk sync.Mutex
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
src rand.Source64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *lockedSource) Int63() (n int64) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
r.lk.Lock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
n = r.src.Int63()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
r.lk.Unlock()
|
|
|
|
|
|
|
|
return
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *lockedSource) Uint64() (n uint64) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
r.lk.Lock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
n = r.src.Uint64()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
r.lk.Unlock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *lockedSource) Seed(seed int64) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
r.lk.Lock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
r.src.Seed(seed)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
r.lk.Unlock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cryptoRand struct {
|
|
|
|
sync.Mutex
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptoRand) Seed(seed int64) {}
|
|
|
|
|
|
|
|
func (c *cryptoRand) Uint64() uint64 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// Lock to make reading thread safe
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
c.Lock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
crand.Read(c.buf)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return binary.BigEndian.Uint64(c.buf)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptoRand) Int63() int64 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return int64(c.Uint64() & ^uint64(1<<63))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New will utilize math/rand for concurrent random usage.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// Setting seed to 0 will use crypto/rand for the initial seed number.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func New(seed int64) *Faker {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// If passing 0 create crypto safe int64 for initial seed number
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if seed == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
binary.Read(crand.Reader, binary.BigEndian, &seed)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Faker{Rand: rand.New(&lockedSource{src: rand.NewSource(seed).(rand.Source64)})}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUnlocked will utilize math/rand for non concurrent safe random usage.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// Setting seed to 0 will use crypto/rand for the initial seed number.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// NewUnlocked is more performant but not safe to run concurrently.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func NewUnlocked(seed int64) *Faker {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// If passing 0 create crypto safe int64 for initial seed number
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if seed == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
binary.Read(crand.Reader, binary.BigEndian, &seed)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Faker{Rand: rand.New(rand.NewSource(seed))}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCrypto will utilize crypto/rand for concurrent random usage.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func NewCrypto() *Faker {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return &Faker{Rand: rand.New(&cryptoRand{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
buf: make([]byte, 8),
|
|
|
|
})}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCustom will utilize a custom rand.Source64 for concurrent random usage
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// See https://golang.org/src/math/rand/rand.go for required interface methods
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func NewCustom(source rand.Source64) *Faker {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return &Faker{Rand: rand.New(source)}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetGlobalFaker will allow you to set what type of faker is globally used. Defailt is math/rand
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func SetGlobalFaker(faker *Faker) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
globalFaker = faker
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|