github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/faker/address.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package faker 12 13 import ( 14 "fmt" 15 "strconv" 16 17 "golang.org/x/exp/rand" 18 ) 19 20 type addressFaker struct { 21 streetAddress *weightedEntries 22 streetSuffix *weightedEntries 23 24 name nameFaker 25 } 26 27 // StreetAddress returns a random en_US street address. 28 func (f *addressFaker) StreetAddress(rng *rand.Rand) string { 29 return f.streetAddress.Rand(rng).(func(rng *rand.Rand) string)(rng) 30 } 31 32 func (f *addressFaker) buildingNumber(rng *rand.Rand) string { 33 return strconv.Itoa(randInt(rng, 1000, 99999)) 34 } 35 36 func (f *addressFaker) streetName(rng *rand.Rand) string { 37 return fmt.Sprintf(`%s %s`, f.firstOrLastName(rng), f.streetSuffix.Rand(rng)) 38 } 39 40 func (f *addressFaker) firstOrLastName(rng *rand.Rand) string { 41 switch rng.Intn(3) { 42 case 0: 43 return f.name.firstNameFemale.Rand(rng).(string) 44 case 1: 45 return f.name.firstNameMale.Rand(rng).(string) 46 case 2: 47 return f.name.lastName.Rand(rng).(string) 48 } 49 panic(`unreachable`) 50 } 51 52 func secondaryAddress(rng *rand.Rand) string { 53 switch rng.Intn(2) { 54 case 0: 55 return fmt.Sprintf(`Apt. %d`, rng.Intn(100)) 56 case 1: 57 return fmt.Sprintf(`Suite %d`, rng.Intn(100)) 58 } 59 panic(`unreachable`) 60 } 61 62 func newAddressFaker(name nameFaker) addressFaker { 63 f := addressFaker{name: name} 64 f.streetSuffix = streetSuffix() 65 f.streetAddress = makeWeightedEntries( 66 func(rng *rand.Rand) string { 67 return fmt.Sprintf(`%s %s`, f.buildingNumber(rng), f.streetName(rng)) 68 }, 0.5, 69 func(rng *rand.Rand) string { 70 return fmt.Sprintf(`%s %s %s`, 71 f.buildingNumber(rng), f.streetName(rng), secondaryAddress(rng)) 72 }, 0.5, 73 ) 74 return f 75 }