2024-04-26 19:30:35 +00:00
|
|
|
package gofakeit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/brianvoe/gofakeit/v6/data"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Bool will generate a random boolean value
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func Bool() bool { return boolFunc(globalFaker.Rand) }
|
|
|
|
|
|
|
|
// Bool will generate a random boolean value
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func (f *Faker) Bool() bool { return boolFunc(f.Rand) }
|
|
|
|
|
|
|
|
func boolFunc(r *rand.Rand) bool { return randIntRange(r, 0, 1) == 1 }
|
|
|
|
|
|
|
|
// UUID (version 4) will generate a random unique identifier based upon random numbers
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func UUID() string { return uuid(globalFaker.Rand) }
|
|
|
|
|
|
|
|
// UUID (version 4) will generate a random unique identifier based upon random numbers
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 8-4-4-4-12
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func (f *Faker) UUID() string { return uuid(f.Rand) }
|
|
|
|
|
|
|
|
func uuid(r *rand.Rand) string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
version := byte(4)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
uuid := make([]byte, 16)
|
|
|
|
|
|
|
|
// Commented out due to io.ReadFull not being race condition safe
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// io.ReadFull(r, uuid[:])
|
|
|
|
|
|
|
|
// Read 16 random bytes
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
for i := 0; i < 16; i++ {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
uuid[i] = byte(r.Intn(256))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set version
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
uuid[6] = (uuid[6] & 0x0f) | (version << 4)
|
|
|
|
|
|
|
|
// Set variant
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
uuid[8] = (uuid[8] & 0xbf) | 0x80
|
|
|
|
|
|
|
|
buf := make([]byte, 36)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
hex.Encode(buf[0:8], uuid[0:4])
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
buf[8] = dash
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
hex.Encode(buf[9:13], uuid[4:6])
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
buf[13] = dash
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
hex.Encode(buf[14:18], uuid[6:8])
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
buf[18] = dash
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
hex.Encode(buf[19:23], uuid[8:10])
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
buf[23] = dash
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
hex.Encode(buf[24:], uuid[10:])
|
|
|
|
|
|
|
|
return string(buf)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShuffleAnySlice takes in a slice and outputs it in a random order
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func ShuffleAnySlice(v any) { shuffleAnySlice(globalFaker.Rand, v) }
|
|
|
|
|
|
|
|
// ShuffleAnySlice takes in a slice and outputs it in a random order
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func (f *Faker) ShuffleAnySlice(v any) { shuffleAnySlice(f.Rand, v) }
|
|
|
|
|
|
|
|
func shuffleAnySlice(r *rand.Rand, v any) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if v == nil {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Check type of passed in value, if not a slice return with no action taken
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
typ := reflect.TypeOf(v)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if typ.Kind() != reflect.Slice {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
s := reflect.ValueOf(v)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
n := s.Len()
|
|
|
|
|
|
|
|
if n <= 1 {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
swap := func(i, j int) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
tmp := reflect.ValueOf(s.Index(i).Interface())
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
s.Index(i).Set(s.Index(j))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
s.Index(j).Set(tmp)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//if size is > int32 probably it will never finish, or ran out of entropy
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
i := n - 1
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
for ; i > 0; i-- {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
j := int(r.Int31n(int32(i + 1)))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
swap(i, j)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlipACoin will return a random value of Heads or Tails
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func FlipACoin() string { return flipACoin(globalFaker.Rand) }
|
|
|
|
|
|
|
|
// FlipACoin will return a random value of Heads or Tails
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func (f *Faker) FlipACoin() string { return flipACoin(f.Rand) }
|
|
|
|
|
|
|
|
func flipACoin(r *rand.Rand) string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if boolFunc(r) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return "Heads"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "Tails"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RandomMapKey will return a random key from a map
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func RandomMapKey(mapI any) any { return randomMapKey(globalFaker.Rand, mapI) }
|
|
|
|
|
|
|
|
// RandomMapKey will return a random key from a map
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func (f *Faker) RandomMapKey(mapI any) any { return randomMapKey(f.Rand, mapI) }
|
|
|
|
|
|
|
|
func randomMapKey(r *rand.Rand, mapI any) any {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
keys := reflect.ValueOf(mapI).MapKeys()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return keys[r.Intn(len(keys))].Interface()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Categories will return a map string array of available data categories and sub categories
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func Categories() map[string][]string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
types := make(map[string][]string)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
for category, subCategoriesMap := range data.Data {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
subCategories := make([]string, 0)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
for subType := range subCategoriesMap {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
subCategories = append(subCategories, subType)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
types[category] = subCategories
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return types
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addMiscLookup() {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
AddFuncLookup("uuid", Info{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Display: "UUID",
|
|
|
|
|
|
|
|
Category: "misc",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Description: "128-bit identifier used to uniquely identify objects or entities in computer systems",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Example: "590c1440-9888-45b0-bd51-a817ee07c3f2",
|
|
|
|
|
|
|
|
Output: "string",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return uuid(r), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
AddFuncLookup("bool", Info{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Display: "Boolean",
|
|
|
|
|
|
|
|
Category: "misc",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Description: "Data type that represents one of two possible values, typically true or false",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Example: "true",
|
|
|
|
|
|
|
|
Output: "bool",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return boolFunc(r), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
AddFuncLookup("flipacoin", Info{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Display: "Flip A Coin",
|
|
|
|
|
|
|
|
Category: "misc",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Description: "Decision-making method involving the tossing of a coin to determine outcomes",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Example: "Tails",
|
|
|
|
|
|
|
|
Output: "string",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return flipACoin(r), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
},
|
|
|
|
})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|