-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
120 lines (110 loc) · 2.97 KB
/
number.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
116
117
118
119
120
package gophonenumbers
import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
)
const (
CarrierATT = "att.com"
CarrierSprint = "sprint.com"
CarrierTMobile = "t-mobile.com"
CarrierVerizon = "verizon.com"
)
type LineType int
const (
LineLocal LineType = iota
LineMobile
LineTollFree
)
type NumberInfo struct {
NumberE164 string
Components Components
Carrier Carrier
Lookups []NumberLookup
CarrierNamesEach []string
LineTypesEach []string
}
func NewNumberInfo() NumberInfo {
return NumberInfo{
Lookups: []NumberLookup{},
CarrierNamesEach: []string{},
LineTypesEach: []string{}}
}
func (ni *NumberInfo) Inflate() error {
ni.NumberE164 = strings.TrimSpace(ni.NumberE164)
if len(ni.Lookups) > 0 {
lookups := ni.Lookups
if 1 == 0 {
sort.Slice(
lookups,
func(i, j int) bool {
// Ascending
// return lookups[i].LookupTime.Before(lookups[j].LookupTime)
// Descending
return lookups[i].LookupTime.After(lookups[j].LookupTime)
},
)
}
sort.SliceStable(lookups, func(i, j int) bool {
return sortLookupCompareString(lookups[i]) >
sortLookupCompareString(lookups[j])
})
ni.Lookups = lookups
}
carrierNames := []string{}
carrierNamesSources := map[string]int{}
lineTypes := []string{}
lineTypesSources := map[string]int{}
for _, lookup := range ni.Lookups {
lookup.NumberE164 = strings.TrimSpace(lookup.NumberE164)
lookup.Carrier.Name = strings.TrimSpace(lookup.Carrier.Name)
lookup.Carrier.LineType = strings.TrimSpace(lookup.Carrier.LineType)
if len(lookup.NumberE164) == 0 {
return errors.New("E_LOOKUP_NO_E164")
}
// Set Top Level E164 number.s
if len(ni.NumberE164) == 0 {
ni.NumberE164 = lookup.NumberE164
} else if ni.NumberE164 != lookup.NumberE164 {
// E.164 should be the same for all lookups.
return fmt.Errorf("E_LOOKUP_E164_MISMATCH TOP[%v] LOOKUP[%v]",
ni.NumberE164, lookup.NumberE164)
}
// Set Top Level Carrier info.
ni.Carrier.Name = strings.TrimSpace(ni.Carrier.Name)
ni.Carrier.LineType = strings.TrimSpace(ni.Carrier.LineType)
if len(ni.Carrier.Name) == 0 {
ni.Carrier.Name = lookup.Carrier.Name
}
if len(ni.Carrier.LineType) == 0 {
ni.Carrier.LineType = lookup.Carrier.LineType
}
source := strings.TrimSpace(string(lookup.LookupSource))
if _, ok := carrierNamesSources[source]; !ok {
carrierNames = append(carrierNames,
fmt.Sprintf("%s(%s)", ni.Carrier.Name, source))
carrierNamesSources[source] = 1
}
if _, ok := lineTypesSources[source]; !ok {
lineTypes = append(lineTypes,
fmt.Sprintf("%s(%s)", ni.Carrier.LineType, source))
lineTypesSources[source] = 1
}
}
ni.CarrierNamesEach = carrierNames
ni.LineTypesEach = lineTypes
ni.Components = ParseE164(ni.NumberE164)
return nil
}
func (ni *NumberInfo) InflateComponents() {
ni.Components = ParseE164(ni.NumberE164)
}
func E164Atoi(s string) (int, error) {
s = strings.TrimSpace(s)
if strings.Index(s, "+") == 0 {
s = s[1:]
}
return strconv.Atoi(s)
}