-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.go
65 lines (58 loc) · 1.56 KB
/
lookup.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
package gophonenumbers
import (
"errors"
"fmt"
"sort"
"time"
)
type Lookup struct {
CarrierNumberInfo CarrierNumberInfo `json:"carrierNumberInfo"`
LookupSource string `json:"lookupSource"`
LookupTime time.Time `json:"lookupTime"`
LookupResponse map[string]interface{} `json:"lookupResponse"`
}
type LookupSet struct {
LookupMap map[string]Lookup `json:"lookupMap"`
}
func NewLookupSet() LookupSet {
return LookupSet{
LookupMap: map[string]Lookup{}}
}
func (set LookupSet) Add(lookup Lookup) {
if set.LookupMap == nil {
set.LookupMap = map[string]Lookup{}
}
set.LookupMap[lookup.LookupTime.Format(time.RFC3339)] = lookup
}
func (set LookupSet) Latest(source string) (Lookup, error) {
if set.LookupMap == nil || len(set.LookupMap) == 0 {
return Lookup{}, errors.New("no latest lookup. lookupSet is empty")
}
times := []string{}
for dtKey, lookup := range set.LookupMap {
if len(source) > 0 && lookup.LookupSource != source {
continue
}
times = append(times, dtKey)
}
if len(times) == 0 {
return Lookup{}, fmt.Errorf("no lookup for source [%s]", source)
}
if len(times) == 1 {
return set.LookupMap[times[0]], nil
}
sort.Strings(times)
return set.LookupMap[times[len(times)-1]], nil
}
func (set LookupSet) Validate() {
if set.LookupMap == nil || len(set.LookupMap) == 0 {
return
}
for dtKey, lookup := range set.LookupMap {
_, err := time.Parse(time.RFC3339, dtKey)
if err != nil {
delete(set.LookupMap, dtKey)
set.LookupMap[lookup.LookupTime.Format(time.RFC3339)] = lookup
}
}
}