forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/brianvoe/gofakeit/v6/person.go

690 lines
12 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"math"
"math/rand"
"strconv"
"strings"
)
// PersonInfo is a struct of person information
2024-04-26 19:30:35 +00:00
type PersonInfo struct {
FirstName string `json:"first_name" xml:"first_name"`
LastName string `json:"last_name" xml:"last_name"`
Gender string `json:"gender" xml:"gender"`
SSN string `json:"ssn" xml:"ssn"`
Image string `json:"image" xml:"image"`
Hobby string `json:"hobby" xml:"hobby"`
Job *JobInfo `json:"job" xml:"job"`
Address *AddressInfo `json:"address" xml:"address"`
Contact *ContactInfo `json:"contact" xml:"contact"`
2024-04-26 19:30:35 +00:00
CreditCard *CreditCardInfo `json:"credit_card" xml:"credit_card"`
}
// Person will generate a struct with person information
2024-04-26 19:30:35 +00:00
func Person() *PersonInfo { return person(globalFaker.Rand) }
// Person will generate a struct with person information
2024-04-26 19:30:35 +00:00
func (f *Faker) Person() *PersonInfo { return person(f.Rand) }
func person(r *rand.Rand) *PersonInfo {
2024-04-26 19:30:35 +00:00
return &PersonInfo{
FirstName: firstName(r),
LastName: lastName(r),
Gender: gender(r),
SSN: ssn(r),
Image: imageURL(r, number(r, 100, 500), number(r, 100, 500)),
Hobby: hobby(r),
Job: job(r),
Address: address(r),
Contact: contact(r),
2024-04-26 19:30:35 +00:00
CreditCard: creditCard(r),
}
2024-04-26 19:30:35 +00:00
}
// Name will generate a random First and Last Name
2024-04-26 19:30:35 +00:00
func Name() string { return name(globalFaker.Rand) }
// Name will generate a random First and Last Name
2024-04-26 19:30:35 +00:00
func (f *Faker) Name() string { return name(f.Rand) }
func name(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return getRandValue(r, []string{"person", "first"}) + " " + getRandValue(r, []string{"person", "last"})
2024-04-26 19:30:35 +00:00
}
// FirstName will generate a random first name
2024-04-26 19:30:35 +00:00
func FirstName() string { return firstName(globalFaker.Rand) }
// FirstName will generate a random first name
2024-04-26 19:30:35 +00:00
func (f *Faker) FirstName() string { return firstName(f.Rand) }
func firstName(r *rand.Rand) string { return getRandValue(r, []string{"person", "first"}) }
// MiddleName will generate a random middle name
2024-04-26 19:30:35 +00:00
func MiddleName() string { return middleName(globalFaker.Rand) }
// MiddleName will generate a random middle name
2024-04-26 19:30:35 +00:00
func (f *Faker) MiddleName() string { return middleName(f.Rand) }
func middleName(r *rand.Rand) string { return getRandValue(r, []string{"person", "middle"}) }
// LastName will generate a random last name
2024-04-26 19:30:35 +00:00
func LastName() string { return lastName(globalFaker.Rand) }
// LastName will generate a random last name
2024-04-26 19:30:35 +00:00
func (f *Faker) LastName() string { return lastName(f.Rand) }
func lastName(r *rand.Rand) string { return getRandValue(r, []string{"person", "last"}) }
// NamePrefix will generate a random name prefix
2024-04-26 19:30:35 +00:00
func NamePrefix() string { return namePrefix(globalFaker.Rand) }
// NamePrefix will generate a random name prefix
2024-04-26 19:30:35 +00:00
func (f *Faker) NamePrefix() string { return namePrefix(f.Rand) }
func namePrefix(r *rand.Rand) string { return getRandValue(r, []string{"person", "prefix"}) }
// NameSuffix will generate a random name suffix
2024-04-26 19:30:35 +00:00
func NameSuffix() string { return nameSuffix(globalFaker.Rand) }
// NameSuffix will generate a random name suffix
2024-04-26 19:30:35 +00:00
func (f *Faker) NameSuffix() string { return nameSuffix(f.Rand) }
func nameSuffix(r *rand.Rand) string { return getRandValue(r, []string{"person", "suffix"}) }
// SSN will generate a random Social Security Number
2024-04-26 19:30:35 +00:00
func SSN() string { return ssn(globalFaker.Rand) }
// SSN will generate a random Social Security Number
2024-04-26 19:30:35 +00:00
func (f *Faker) SSN() string { return ssn(f.Rand) }
func ssn(r *rand.Rand) string { return strconv.Itoa(randIntRange(r, 100000000, 999999999)) }
// Gender will generate a random gender string
2024-04-26 19:30:35 +00:00
func Gender() string { return gender(globalFaker.Rand) }
// Gender will generate a random gender string
2024-04-26 19:30:35 +00:00
func (f *Faker) Gender() string { return gender(f.Rand) }
func gender(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
if boolFunc(r) {
2024-04-26 19:30:35 +00:00
return "male"
2024-04-26 19:30:35 +00:00
}
return "female"
2024-04-26 19:30:35 +00:00
}
// Hobby will generate a random hobby string
2024-04-26 19:30:35 +00:00
func Hobby() string { return hobby(globalFaker.Rand) }
// Hobby will generate a random hobby string
2024-04-26 19:30:35 +00:00
func (f *Faker) Hobby() string { return hobby(f.Rand) }
func hobby(r *rand.Rand) string { return getRandValue(r, []string{"person", "hobby"}) }
// ContactInfo struct full of contact info
2024-04-26 19:30:35 +00:00
type ContactInfo struct {
Phone string `json:"phone" xml:"phone"`
2024-04-26 19:30:35 +00:00
Email string `json:"email" xml:"email"`
}
// Contact will generate a struct with information randomly populated contact information
2024-04-26 19:30:35 +00:00
func Contact() *ContactInfo { return contact(globalFaker.Rand) }
// Contact will generate a struct with information randomly populated contact information
2024-04-26 19:30:35 +00:00
func (f *Faker) Contact() *ContactInfo { return contact(f.Rand) }
func contact(r *rand.Rand) *ContactInfo {
2024-04-26 19:30:35 +00:00
return &ContactInfo{
2024-04-26 19:30:35 +00:00
Phone: phone(r),
2024-04-26 19:30:35 +00:00
Email: email(r),
}
2024-04-26 19:30:35 +00:00
}
// Phone will generate a random phone number string
2024-04-26 19:30:35 +00:00
func Phone() string { return phone(globalFaker.Rand) }
// Phone will generate a random phone number string
2024-04-26 19:30:35 +00:00
func (f *Faker) Phone() string { return phone(f.Rand) }
func phone(r *rand.Rand) string { return replaceWithNumbers(r, "##########") }
// PhoneFormatted will generate a random phone number string
2024-04-26 19:30:35 +00:00
func PhoneFormatted() string { return phoneFormatted(globalFaker.Rand) }
// PhoneFormatted will generate a random phone number string
2024-04-26 19:30:35 +00:00
func (f *Faker) PhoneFormatted() string { return phoneFormatted(f.Rand) }
func phoneFormatted(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return replaceWithNumbers(r, getRandValue(r, []string{"person", "phone"}))
2024-04-26 19:30:35 +00:00
}
// Email will generate a random email string
2024-04-26 19:30:35 +00:00
func Email() string { return email(globalFaker.Rand) }
// Email will generate a random email string
2024-04-26 19:30:35 +00:00
func (f *Faker) Email() string { return email(f.Rand) }
func email(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
email := getRandValue(r, []string{"person", "first"}) + getRandValue(r, []string{"person", "last"})
2024-04-26 19:30:35 +00:00
email += "@"
2024-04-26 19:30:35 +00:00
email += getRandValue(r, []string{"person", "last"}) + "." + getRandValue(r, []string{"internet", "domain_suffix"})
return strings.ToLower(email)
2024-04-26 19:30:35 +00:00
}
// Teams takes in an array of people and team names and randomly places the people into teams as evenly as possible
2024-04-26 19:30:35 +00:00
func Teams(peopleArray []string, teamsArray []string) map[string][]string {
2024-04-26 19:30:35 +00:00
return teams(globalFaker.Rand, peopleArray, teamsArray)
2024-04-26 19:30:35 +00:00
}
// Teams takes in an array of people and team names and randomly places the people into teams as evenly as possible
2024-04-26 19:30:35 +00:00
func (f *Faker) Teams(peopleArray []string, teamsArray []string) map[string][]string {
2024-04-26 19:30:35 +00:00
return teams(f.Rand, peopleArray, teamsArray)
2024-04-26 19:30:35 +00:00
}
func teams(r *rand.Rand, people []string, teams []string) map[string][]string {
2024-04-26 19:30:35 +00:00
// Shuffle the people if more than 1
2024-04-26 19:30:35 +00:00
if len(people) > 1 {
2024-04-26 19:30:35 +00:00
shuffleStrings(r, people)
2024-04-26 19:30:35 +00:00
}
peopleIndex := 0
2024-04-26 19:30:35 +00:00
teamsOutput := make(map[string][]string)
2024-04-26 19:30:35 +00:00
numPer := math.Ceil(float64(len(people)) / float64(len(teams)))
2024-04-26 19:30:35 +00:00
for _, team := range teams {
2024-04-26 19:30:35 +00:00
teamsOutput[team] = []string{}
2024-04-26 19:30:35 +00:00
for i := 0.00; i < numPer; i++ {
2024-04-26 19:30:35 +00:00
if peopleIndex < len(people) {
2024-04-26 19:30:35 +00:00
teamsOutput[team] = append(teamsOutput[team], people[peopleIndex])
2024-04-26 19:30:35 +00:00
peopleIndex++
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
return teamsOutput
2024-04-26 19:30:35 +00:00
}
func addPersonLookup() {
2024-04-26 19:30:35 +00:00
AddFuncLookup("person", Info{
Display: "Person",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "Personal data, like name and contact details, used for identification and communication",
2024-04-26 19:30:35 +00:00
Example: `{
2024-04-26 19:30:35 +00:00
"first_name": "Markus",
2024-04-26 19:30:35 +00:00
"last_name": "Moen",
2024-04-26 19:30:35 +00:00
"gender": "male",
2024-04-26 19:30:35 +00:00
"ssn": "275413589",
2024-04-26 19:30:35 +00:00
"image": "https://picsum.photos/208/500",
2024-04-26 19:30:35 +00:00
"hobby": "Lacrosse",
2024-04-26 19:30:35 +00:00
"job": {
2024-04-26 19:30:35 +00:00
"company": "Intermap Technologies",
2024-04-26 19:30:35 +00:00
"title": "Developer",
2024-04-26 19:30:35 +00:00
"descriptor": "Direct",
2024-04-26 19:30:35 +00:00
"level": "Paradigm"
2024-04-26 19:30:35 +00:00
},
2024-04-26 19:30:35 +00:00
"address": {
2024-04-26 19:30:35 +00:00
"address": "369 North Cornerbury, Miami, North Dakota 24259",
2024-04-26 19:30:35 +00:00
"street": "369 North Cornerbury",
2024-04-26 19:30:35 +00:00
"city": "Miami",
2024-04-26 19:30:35 +00:00
"state": "North Dakota",
2024-04-26 19:30:35 +00:00
"zip": "24259",
2024-04-26 19:30:35 +00:00
"country": "Ghana",
2024-04-26 19:30:35 +00:00
"latitude": -6.662595,
2024-04-26 19:30:35 +00:00
"longitude": 23.921575
2024-04-26 19:30:35 +00:00
},
2024-04-26 19:30:35 +00:00
"contact": {
2024-04-26 19:30:35 +00:00
"phone": "3023202027",
2024-04-26 19:30:35 +00:00
"email": "lamarkoelpin@heaney.biz"
2024-04-26 19:30:35 +00:00
},
2024-04-26 19:30:35 +00:00
"credit_card": {
2024-04-26 19:30:35 +00:00
"type": "Maestro",
2024-04-26 19:30:35 +00:00
"number": "39800889982276",
2024-04-26 19:30:35 +00:00
"exp": "01/29",
2024-04-26 19:30:35 +00:00
"cvv": "932"
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}`,
Output: "map[string]any",
2024-04-26 19:30:35 +00:00
ContentType: "application/json",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return person(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("name", Info{
Display: "Name",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "The given and family name of an individual",
Example: "Markus Moen",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return name(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("nameprefix", Info{
Display: "Name Prefix",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "A title or honorific added before a person's name",
Example: "Mr.",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return namePrefix(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("namesuffix", Info{
Display: "Name Suffix",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "A title or designation added after a person's name",
Example: "Jr.",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return nameSuffix(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("firstname", Info{
Display: "First Name",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "The name given to a person at birth",
Example: "Markus",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return firstName(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("middlename", Info{
Display: "Middle Name",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "Name between a person's first name and last name",
Example: "Belinda",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return middleName(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("lastname", Info{
Display: "Last Name",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "The family name or surname of an individual",
Example: "Daniel",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return lastName(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("gender", Info{
Display: "Gender",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "Classification based on social and cultural norms that identifies an individual",
Example: "male",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return gender(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("ssn", Info{
Display: "SSN",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "Unique nine-digit identifier used for government and financial purposes in the United States",
Example: "296446360",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return ssn(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("hobby", Info{
Display: "Hobby",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "An activity pursued for leisure and pleasure",
Example: "Swimming",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return hobby(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("email", Info{
Display: "Email",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "Electronic mail used for sending digital messages and communication over the internet",
Example: "markusmoen@pagac.net",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return email(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("phone", Info{
Display: "Phone",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "Numerical sequence used to contact individuals via telephone or mobile devices",
Example: "6136459948",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return phone(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("phoneformatted", Info{
Display: "Phone Formatted",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "Formatted phone number of a person",
Example: "136-459-9489",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return phoneFormatted(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("teams", Info{
Display: "Teams",
Category: "person",
2024-04-26 19:30:35 +00:00
Description: "Randomly split people into teams",
2024-04-26 19:30:35 +00:00
Example: `{
2024-04-26 19:30:35 +00:00
"Team 1": [
2024-04-26 19:30:35 +00:00
"Justin",
2024-04-26 19:30:35 +00:00
"Connor",
2024-04-26 19:30:35 +00:00
"Jeff"
2024-04-26 19:30:35 +00:00
],
2024-04-26 19:30:35 +00:00
"Team 2": [
2024-04-26 19:30:35 +00:00
"Sharon",
2024-04-26 19:30:35 +00:00
"Fabian",
2024-04-26 19:30:35 +00:00
"Billy"
2024-04-26 19:30:35 +00:00
],
2024-04-26 19:30:35 +00:00
"Team 3": [
2024-04-26 19:30:35 +00:00
"Steve",
2024-04-26 19:30:35 +00:00
"Robert"
2024-04-26 19:30:35 +00:00
]
2024-04-26 19:30:35 +00:00
}`,
Output: "map[string][]string",
2024-04-26 19:30:35 +00:00
ContentType: "application/json",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{Field: "people", Display: "Strings", Type: "[]string", Description: "Array of people"},
2024-04-26 19:30:35 +00:00
{Field: "teams", Display: "Strings", Type: "[]string", Description: "Array of teams"},
},
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
people, err := info.GetStringArray(m, "people")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
teamsArray, err := info.GetStringArray(m, "teams")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
return teams(r, people, teamsArray), nil
2024-04-26 19:30:35 +00:00
},
})
2024-04-26 19:30:35 +00:00
}