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

669 lines
13 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"errors"
"math/rand"
"strings"
)
// AddressInfo is a struct full of address information
2024-04-26 19:30:35 +00:00
type AddressInfo struct {
Address string `json:"address" xml:"address"`
Street string `json:"street" xml:"street"`
City string `json:"city" xml:"city"`
State string `json:"state" xml:"state"`
Zip string `json:"zip" xml:"zip"`
Country string `json:"country" xml:"country"`
Latitude float64 `json:"latitude" xml:"latitude"`
2024-04-26 19:30:35 +00:00
Longitude float64 `json:"longitude" xml:"longitude"`
}
// Address will generate a struct of address information
2024-04-26 19:30:35 +00:00
func Address() *AddressInfo { return address(globalFaker.Rand) }
// Address will generate a struct of address information
2024-04-26 19:30:35 +00:00
func (f *Faker) Address() *AddressInfo { return address(f.Rand) }
func address(r *rand.Rand) *AddressInfo {
2024-04-26 19:30:35 +00:00
street := street(r)
2024-04-26 19:30:35 +00:00
city := city(r)
2024-04-26 19:30:35 +00:00
state := state(r)
2024-04-26 19:30:35 +00:00
zip := zip(r)
return &AddressInfo{
Address: street + ", " + city + ", " + state + " " + zip,
Street: street,
City: city,
State: state,
Zip: zip,
Country: country(r),
Latitude: latitude(r),
2024-04-26 19:30:35 +00:00
Longitude: longitude(r),
}
2024-04-26 19:30:35 +00:00
}
// Street will generate a random address street string
2024-04-26 19:30:35 +00:00
func Street() string { return street(globalFaker.Rand) }
// Street will generate a random address street string
2024-04-26 19:30:35 +00:00
func (f *Faker) Street() string { return street(f.Rand) }
func street(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
var street = ""
2024-04-26 19:30:35 +00:00
switch randInt := randIntRange(r, 1, 2); randInt {
2024-04-26 19:30:35 +00:00
case 1:
2024-04-26 19:30:35 +00:00
street = streetNumber(r) + " " + streetPrefix(r) + " " + streetName(r) + streetSuffix(r)
2024-04-26 19:30:35 +00:00
case 2:
2024-04-26 19:30:35 +00:00
street = streetNumber(r) + " " + streetName(r) + streetSuffix(r)
2024-04-26 19:30:35 +00:00
}
return street
2024-04-26 19:30:35 +00:00
}
// StreetNumber will generate a random address street number string
2024-04-26 19:30:35 +00:00
func StreetNumber() string { return streetNumber(globalFaker.Rand) }
// StreetNumber will generate a random address street number string
2024-04-26 19:30:35 +00:00
func (f *Faker) StreetNumber() string { return streetNumber(f.Rand) }
func streetNumber(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return strings.TrimLeft(replaceWithNumbers(r, getRandValue(r, []string{"address", "number"})), "0")
2024-04-26 19:30:35 +00:00
}
// StreetPrefix will generate a random address street prefix string
2024-04-26 19:30:35 +00:00
func StreetPrefix() string { return streetPrefix(globalFaker.Rand) }
// StreetPrefix will generate a random address street prefix string
2024-04-26 19:30:35 +00:00
func (f *Faker) StreetPrefix() string { return streetPrefix(f.Rand) }
func streetPrefix(r *rand.Rand) string { return getRandValue(r, []string{"address", "street_prefix"}) }
// StreetName will generate a random address street name string
2024-04-26 19:30:35 +00:00
func StreetName() string { return streetName(globalFaker.Rand) }
// StreetName will generate a random address street name string
2024-04-26 19:30:35 +00:00
func (f *Faker) StreetName() string { return streetName(f.Rand) }
func streetName(r *rand.Rand) string { return getRandValue(r, []string{"address", "street_name"}) }
// StreetSuffix will generate a random address street suffix string
2024-04-26 19:30:35 +00:00
func StreetSuffix() string { return streetSuffix(globalFaker.Rand) }
// StreetSuffix will generate a random address street suffix string
2024-04-26 19:30:35 +00:00
func (f *Faker) StreetSuffix() string { return streetSuffix(f.Rand) }
func streetSuffix(r *rand.Rand) string { return getRandValue(r, []string{"address", "street_suffix"}) }
// City will generate a random city string
2024-04-26 19:30:35 +00:00
func City() string { return city(globalFaker.Rand) }
// City will generate a random city string
2024-04-26 19:30:35 +00:00
func (f *Faker) City() string { return city(f.Rand) }
func city(r *rand.Rand) string { return getRandValue(r, []string{"address", "city"}) }
// State will generate a random state string
2024-04-26 19:30:35 +00:00
func State() string { return state(globalFaker.Rand) }
// State will generate a random state string
2024-04-26 19:30:35 +00:00
func (f *Faker) State() string { return state(f.Rand) }
func state(r *rand.Rand) string { return getRandValue(r, []string{"address", "state"}) }
// StateAbr will generate a random abbreviated state string
2024-04-26 19:30:35 +00:00
func StateAbr() string { return stateAbr(globalFaker.Rand) }
// StateAbr will generate a random abbreviated state string
2024-04-26 19:30:35 +00:00
func (f *Faker) StateAbr() string { return stateAbr(f.Rand) }
func stateAbr(r *rand.Rand) string { return getRandValue(r, []string{"address", "state_abr"}) }
// Zip will generate a random Zip code string
2024-04-26 19:30:35 +00:00
func Zip() string { return zip(globalFaker.Rand) }
// Zip will generate a random Zip code string
2024-04-26 19:30:35 +00:00
func (f *Faker) Zip() string { return zip(f.Rand) }
func zip(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return replaceWithNumbers(r, getRandValue(r, []string{"address", "zip"}))
2024-04-26 19:30:35 +00:00
}
// Country will generate a random country string
2024-04-26 19:30:35 +00:00
func Country() string { return country(globalFaker.Rand) }
// Country will generate a random country string
2024-04-26 19:30:35 +00:00
func (f *Faker) Country() string { return country(f.Rand) }
func country(r *rand.Rand) string { return getRandValue(r, []string{"address", "country"}) }
// CountryAbr will generate a random abbreviated country string
2024-04-26 19:30:35 +00:00
func CountryAbr() string { return countryAbr(globalFaker.Rand) }
// CountryAbr will generate a random abbreviated country string
2024-04-26 19:30:35 +00:00
func (f *Faker) CountryAbr() string { return countryAbr(f.Rand) }
func countryAbr(r *rand.Rand) string { return getRandValue(r, []string{"address", "country_abr"}) }
// Latitude will generate a random latitude float64
2024-04-26 19:30:35 +00:00
func Latitude() float64 { return latitude(globalFaker.Rand) }
// Latitude will generate a random latitude float64
2024-04-26 19:30:35 +00:00
func (f *Faker) Latitude() float64 { return latitude(f.Rand) }
func latitude(r *rand.Rand) float64 { return toFixed((r.Float64()*180)-90, 6) }
// LatitudeInRange will generate a random latitude within the input range
2024-04-26 19:30:35 +00:00
func LatitudeInRange(min, max float64) (float64, error) {
2024-04-26 19:30:35 +00:00
return latitudeInRange(globalFaker.Rand, min, max)
2024-04-26 19:30:35 +00:00
}
// LatitudeInRange will generate a random latitude within the input range
2024-04-26 19:30:35 +00:00
func (f *Faker) LatitudeInRange(min, max float64) (float64, error) {
2024-04-26 19:30:35 +00:00
return latitudeInRange(f.Rand, min, max)
2024-04-26 19:30:35 +00:00
}
func latitudeInRange(r *rand.Rand, min, max float64) (float64, error) {
2024-04-26 19:30:35 +00:00
if min > max || min < -90 || min > 90 || max < -90 || max > 90 {
2024-04-26 19:30:35 +00:00
return 0, errors.New("invalid min or max range, must be valid floats and between -90 and 90")
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return toFixed(float64Range(r, min, max), 6), nil
2024-04-26 19:30:35 +00:00
}
// Longitude will generate a random longitude float64
2024-04-26 19:30:35 +00:00
func Longitude() float64 { return longitude(globalFaker.Rand) }
// Longitude will generate a random longitude float64
2024-04-26 19:30:35 +00:00
func (f *Faker) Longitude() float64 { return longitude(f.Rand) }
func longitude(r *rand.Rand) float64 { return toFixed((r.Float64()*360)-180, 6) }
// LongitudeInRange will generate a random longitude within the input range
2024-04-26 19:30:35 +00:00
func LongitudeInRange(min, max float64) (float64, error) {
2024-04-26 19:30:35 +00:00
return longitudeInRange(globalFaker.Rand, min, max)
2024-04-26 19:30:35 +00:00
}
// LongitudeInRange will generate a random longitude within the input range
2024-04-26 19:30:35 +00:00
func (f *Faker) LongitudeInRange(min, max float64) (float64, error) {
2024-04-26 19:30:35 +00:00
return longitudeInRange(f.Rand, min, max)
2024-04-26 19:30:35 +00:00
}
func longitudeInRange(r *rand.Rand, min, max float64) (float64, error) {
2024-04-26 19:30:35 +00:00
if min > max || min < -180 || min > 180 || max < -180 || max > 180 {
2024-04-26 19:30:35 +00:00
return 0, errors.New("invalid min or max range, must be valid floats and between -180 and 180")
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return toFixed(float64Range(r, min, max), 6), nil
2024-04-26 19:30:35 +00:00
}
func addAddressLookup() {
2024-04-26 19:30:35 +00:00
AddFuncLookup("address", Info{
Display: "Address",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Residential location including street, city, state, country and postal code",
2024-04-26 19:30:35 +00:00
Example: `{
2024-04-26 19:30:35 +00:00
"address": "364 Unionsville, Norfolk, Ohio 99536",
2024-04-26 19:30:35 +00:00
"street": "364 Unionsville",
2024-04-26 19:30:35 +00:00
"city": "Norfolk",
2024-04-26 19:30:35 +00:00
"state": "Ohio",
2024-04-26 19:30:35 +00:00
"zip": "99536",
2024-04-26 19:30:35 +00:00
"country": "Lesotho",
2024-04-26 19:30:35 +00:00
"latitude": 88.792592,
2024-04-26 19:30:35 +00:00
"longitude": 174.504681
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 address(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("city", Info{
Display: "City",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Part of a country with significant population, often a central hub for culture and commerce",
Example: "Marcelside",
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 city(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("country", Info{
Display: "Country",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Nation with its own government and defined territory",
Example: "United States of America",
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 country(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("countryabr", Info{
Display: "Country Abbreviation",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Shortened 2-letter form of a country's name",
Example: "US",
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 countryAbr(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("state", Info{
Display: "State",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Governmental division within a country, often having its own laws and government",
Example: "Illinois",
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 state(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("stateabr", Info{
Display: "State Abbreviation",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Shortened 2-letter form of a country's state",
Example: "IL",
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 stateAbr(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("street", Info{
Display: "Street",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Public road in a city or town, typically with houses and buildings on each side",
Example: "364 East Rapidsborough",
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 street(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("streetname", Info{
Display: "Street Name",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Name given to a specific road or street",
Example: "View",
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 streetName(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("streetnumber", Info{
Display: "Street Number",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Numerical identifier assigned to a street",
Example: "13645",
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 streetNumber(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("streetprefix", Info{
Display: "Street Prefix",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Directional or descriptive term preceding a street name, like 'East' or 'Main'",
Example: "Lake",
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 streetPrefix(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("streetsuffix", Info{
Display: "Street Suffix",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Designation at the end of a street name indicating type, like 'Avenue' or 'Street'",
Example: "land",
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 streetSuffix(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("zip", Info{
Display: "Zip",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Numerical code for postal address sorting, specific to a geographic area",
Example: "13645",
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 zip(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("latitude", Info{
Display: "Latitude",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Geographic coordinate specifying north-south position on Earth's surface",
Example: "-73.534056",
Output: "float",
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 latitude(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("latituderange", Info{
Display: "Latitude Range",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Latitude number between the given range (default min=0, max=90)",
Example: "22.921026",
Output: "float",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{Field: "min", Display: "Min", Type: "float", Default: "0", Description: "Minimum range"},
2024-04-26 19:30:35 +00:00
{Field: "max", Display: "Max", Type: "float", Default: "90", Description: "Maximum range"},
},
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
min, err := info.GetFloat64(m, "min")
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
}
max, err := info.GetFloat64(m, "max")
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
}
rangeOut, err := latitudeInRange(r, min, max)
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 rangeOut, nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("longitude", Info{
Display: "Longitude",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Geographic coordinate indicating east-west position on Earth's surface",
Example: "-147.068112",
Output: "float",
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 longitude(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("longituderange", Info{
Display: "Longitude Range",
Category: "address",
2024-04-26 19:30:35 +00:00
Description: "Longitude number between the given range (default min=0, max=180)",
Example: "-8.170450",
Output: "float",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{Field: "min", Display: "Min", Type: "float", Default: "0", Description: "Minimum range"},
2024-04-26 19:30:35 +00:00
{Field: "max", Display: "Max", Type: "float", Default: "180", Description: "Maximum range"},
},
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
min, err := info.GetFloat64(m, "min")
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
}
max, err := info.GetFloat64(m, "max")
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
}
rangeOut, err := longitudeInRange(r, min, max)
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 rangeOut, nil
2024-04-26 19:30:35 +00:00
},
})
2024-04-26 19:30:35 +00:00
}