-
Notifications
You must be signed in to change notification settings - Fork 0
/
fictitiousgenerator.go
115 lines (99 loc) · 3.15 KB
/
fictitiousgenerator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gophonenumbers
import (
"github.com/grokify/mogo/crypto/randutil"
)
const (
fakeLineNumberMin = uint16(100)
fakeLineNumberMax = uint16(199)
)
type FakeNumberGenerator struct {
AreaCodes []uint16
}
func NewFakeNumberGenerator(areacodes []uint16) FakeNumberGenerator {
fng := FakeNumberGenerator{
AreaCodes: areacodes,
}
return fng
}
// RandomAreaCode generates a random area code.
func (fng *FakeNumberGenerator) RandomAreaCode() uint16 {
num := randutil.Int64n(uint(len(fng.AreaCodes)))
return fng.AreaCodes[num]
}
// RandomLineNumber generates a random line number
func (fng *FakeNumberGenerator) RandomLineNumber() uint16 {
return fng.RandomLineNumberMinMax(fakeLineNumberMin, fakeLineNumberMax)
}
// RandomLineNumber generates a random line number
func (fng *FakeNumberGenerator) RandomLineNumberMinMax(min, max uint16) uint16 {
num := randutil.Int64n(uint(max) - uint(min))
return uint16(num) + min
}
// RandomLocalNumberUS returns a US E.164 number
// AreaCode + Prefix + Line Number
func (fng *FakeNumberGenerator) RandomLocalNumberUS() (uint64, error) {
num := fng.RandomLineNumber()
num2 := fng.RandomAreaCode()
return fng.LocalNumberUS(num2, num), nil
}
// RandomLocalNumberUS returns a US E.164 number
// AreaCode + Prefix + Line Number
func (fng *FakeNumberGenerator) RandomLocalNumberUSAreaCodes(acs []uint16) uint64 {
idx := randutil.Int64n(uint(len(acs)))
num := fng.RandomLineNumber()
return fng.LocalNumberUS(acs[idx], num)
}
// LocalNumberUS returns a US E.164 number given an areacode and line number
func (fng *FakeNumberGenerator) LocalNumberUS(ac uint16, ln uint16) uint64 {
return 10000000000 + (uint64(ac) * 10000000) + (5550000) + uint64(ln)
}
// RandomLocalNumberUSUnique returns a US E.164 number
// AreaCode + Prefix + Line Number
func (fng *FakeNumberGenerator) RandomLocalNumberUSUnique(set map[uint64]int8) (uint64, map[uint64]int8, error) {
try, err := fng.RandomLocalNumberUS()
if err != nil {
return 0, set, err
}
_, ok := set[try]
for ok {
try, err := fng.RandomLocalNumberUS()
if err != nil {
return 0, set, err
}
_, ok = set[try]
}
set[try] = 1
return try, set, nil
}
// RandomLocalNumberUSUnique returns a US E.164 number
// AreaCode + Prefix + Line Number
func (fng *FakeNumberGenerator) RandomLocalNumberUSUniqueAreaCodeSet(set map[uint64]int8, acs []uint16) (uint64, map[uint64]int8, error) {
try := fng.RandomLocalNumberUSAreaCodes(acs)
_, ok := set[try]
for ok {
try = fng.RandomLocalNumberUSAreaCodes(acs)
}
set[try] = 1
return try, set, nil
}
// LocalNumberUS returns a US E.164 number given an areacode and line number
func LocalNumberUS(ac uint16, ln uint16) uint64 {
return 10000000000 + (uint64(ac) * 10000000) + (5550000) + uint64(ln)
}
type AreaCodeIncrementor struct {
Counter map[uint16]uint16
Base uint16
}
func NewAreaCodeIncrementor(base uint16) AreaCodeIncrementor {
return AreaCodeIncrementor{Counter: map[uint16]uint16{}, Base: base}
}
func (aci *AreaCodeIncrementor) GetNext(ac uint16) uint64 {
if count, ok := aci.Counter[ac]; ok {
count++
aci.Counter[ac] = count
return LocalNumberUS(ac, count)
}
count := aci.Base + 1
aci.Counter[ac] = count
return LocalNumberUS(ac, count)
}