Skip to content

Commit 119a3cc

Browse files
committed
Address some linter complaints, rename vars for clarity
1 parent 4a0fd3c commit 119a3cc

File tree

2 files changed

+62
-66
lines changed

2 files changed

+62
-66
lines changed

phonenumbers.go

+59-63
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,53 @@ import (
1717
"google.golang.org/protobuf/proto"
1818
)
1919

20+
func init() {
21+
initMetadata()
22+
}
23+
24+
func initMetadata() {
25+
// load our regions
26+
regionMap, err := loadIntArrayMap(regionData)
27+
if err != nil {
28+
panic(err)
29+
}
30+
countryCodeToRegion = regionMap.Map
31+
32+
// then our metadata
33+
if err = loadMetadataFromFile(); err != nil {
34+
panic(err)
35+
}
36+
37+
for eKey, regionCodes := range countryCodeToRegion {
38+
// We can assume that if the county calling code maps to the
39+
// non-geo entity region code then that's the only region code
40+
// it maps to.
41+
if len(regionCodes) == 1 && REGION_CODE_FOR_NON_GEO_ENTITY == regionCodes[0] {
42+
// This is the subset of all country codes that map to the
43+
// non-geo entity region code.
44+
countryCodesForNonGeographicalRegion[eKey] = true
45+
} else {
46+
// The supported regions set does not include the "001"
47+
// non-geo entity region code.
48+
for _, val := range regionCodes {
49+
supportedRegions[val] = true
50+
}
51+
}
52+
53+
supportedCallingCodes[eKey] = true
54+
}
55+
// If the non-geo entity still got added to the set of supported
56+
// regions it must be because there are entries that list the non-geo
57+
// entity alongside normal regions (which is wrong). If we discover
58+
// this, remove the non-geo entity from the set of supported regions
59+
// and log (or not log).
60+
delete(supportedRegions, REGION_CODE_FOR_NON_GEO_ENTITY)
61+
62+
for _, val := range countryCodeToRegion[NANPA_COUNTRY_CODE] {
63+
writeToNanpaRegions(val, struct{}{})
64+
}
65+
}
66+
2067
const (
2168
// MIN_LENGTH_FOR_NSN is the minimum and maximum length of the national significant number.
2269
MIN_LENGTH_FOR_NSN = 2
@@ -428,7 +475,7 @@ var (
428475
52: true, // Mexico
429476
}
430477

431-
m = sync.Mutex{}
478+
dataLoadMutex = sync.Mutex{}
432479
)
433480

434481
// INTERNATIONAL and NATIONAL formats are consistent with the definition
@@ -676,10 +723,7 @@ func writeToCountryCodeToNonGeographicalMetadataMap(key int, v *PhoneMetadata) {
676723
countryCodeToNonGeographicalMetadataMap[key] = v
677724
}
678725

679-
func loadMetadataFromFile(
680-
regionCode string,
681-
countryCallingCode int) error {
682-
726+
func loadMetadataFromFile() error {
683727
metadataCollection, err := MetadataCollection()
684728
if err != nil {
685729
return err
@@ -3319,54 +3363,6 @@ func IsMobileNumberPortableRegion(regionCode string) bool {
33193363
return metadata.GetMobileNumberPortableRegion()
33203364
}
33213365

3322-
func initMetadata() {
3323-
// load our regions
3324-
regionMap, err := loadIntArrayMap(regionData)
3325-
if err != nil {
3326-
panic(err)
3327-
}
3328-
countryCodeToRegion = regionMap.Map
3329-
3330-
// then our metadata
3331-
err = loadMetadataFromFile("US", 1)
3332-
if err != nil {
3333-
panic(err)
3334-
}
3335-
3336-
for eKey, regionCodes := range countryCodeToRegion {
3337-
// We can assume that if the county calling code maps to the
3338-
// non-geo entity region code then that's the only region code
3339-
// it maps to.
3340-
if len(regionCodes) == 1 && REGION_CODE_FOR_NON_GEO_ENTITY == regionCodes[0] {
3341-
// This is the subset of all country codes that map to the
3342-
// non-geo entity region code.
3343-
countryCodesForNonGeographicalRegion[eKey] = true
3344-
} else {
3345-
// The supported regions set does not include the "001"
3346-
// non-geo entity region code.
3347-
for _, val := range regionCodes {
3348-
supportedRegions[val] = true
3349-
}
3350-
}
3351-
3352-
supportedCallingCodes[eKey] = true
3353-
}
3354-
// If the non-geo entity still got added to the set of supported
3355-
// regions it must be because there are entries that list the non-geo
3356-
// entity alongside normal regions (which is wrong). If we discover
3357-
// this, remove the non-geo entity from the set of supported regions
3358-
// and log (or not log).
3359-
delete(supportedRegions, REGION_CODE_FOR_NON_GEO_ENTITY)
3360-
3361-
for _, val := range countryCodeToRegion[NANPA_COUNTRY_CODE] {
3362-
writeToNanpaRegions(val, struct{}{})
3363-
}
3364-
}
3365-
3366-
func init() {
3367-
initMetadata()
3368-
}
3369-
33703366
// GetTimezonesForPrefix returns a slice of Timezones corresponding to the number passed
33713367
// or error when it is impossible to convert the string to int
33723368
// The algorythm tries to match the timezones starting from the maximum
@@ -3409,17 +3405,17 @@ func GetTimezonesForNumber(number *PhoneNumber) ([]string, error) {
34093405
return GetTimezonesForPrefix(e164)
34103406
}
34113407

3412-
func fillLangMap(langMap map[string]*intStringMap, binMap embed.FS, filename, language string) (bool, error) {
3413-
m.Lock()
3414-
defer m.Unlock()
3408+
func fillLangMap(langMap map[string]*intStringMap, dataFS embed.FS, dir, language string) (bool, error) {
3409+
dataLoadMutex.Lock()
3410+
defer dataLoadMutex.Unlock()
34153411

34163412
_, ok := langMap[language]
34173413
if ok {
34183414
return true, nil
34193415
}
34203416

34213417
// do we have data for this language
3422-
data, err := binMap.ReadFile(filename)
3418+
data, err := dataFS.ReadFile(dir + "/" + language + ".txt.gz")
34233419
if err != nil {
34243420
if errors.Is(err, fs.ErrNotExist) {
34253421
return false, nil
@@ -3439,8 +3435,8 @@ func fillLangMap(langMap map[string]*intStringMap, binMap embed.FS, filename, la
34393435
return false, err
34403436
}
34413437

3442-
func getValueForNumber(langMap map[string]*intStringMap, dir string, binMap embed.FS, language string, maxLength int, number *PhoneNumber) (string, int, error) {
3443-
ok, err := fillLangMap(langMap, binMap, dir+"/"+language+".txt.gz", language)
3438+
func getValueForNumber(langMap map[string]*intStringMap, dataFS embed.FS, dir, language string, maxLength int, number *PhoneNumber) (string, int, error) {
3439+
ok, err := fillLangMap(langMap, dataFS, dir, language)
34443440
if !ok || err != nil {
34453441
return "", 0, err
34463442
}
@@ -3490,7 +3486,7 @@ func GetSafeCarrierDisplayNameForNumber(phoneNumber *PhoneNumber, lang string) (
34903486
// GetCarrierWithPrefixForNumber returns the carrier we believe the number belongs to, as well as
34913487
// its prefix. Note due to number porting this is only a guess, there is no guarantee to its accuracy.
34923488
func GetCarrierWithPrefixForNumber(number *PhoneNumber, lang string) (string, int, error) {
3493-
carrier, prefix, err := getValueForNumber(carrierPrefixMap, carrierDataPath, carrierData, lang, 10, number)
3489+
carrier, prefix, err := getValueForNumber(carrierPrefixMap, carrierData, carrierDataPath, lang, 10, number)
34943490
if err != nil {
34953491
return "", 0, err
34963492
}
@@ -3499,19 +3495,19 @@ func GetCarrierWithPrefixForNumber(number *PhoneNumber, lang string) (string, in
34993495
}
35003496

35013497
// fallback to english
3502-
return getValueForNumber(carrierPrefixMap, carrierDataPath, carrierData, "en", 10, number)
3498+
return getValueForNumber(carrierPrefixMap, carrierData, carrierDataPath, "en", 10, number)
35033499
}
35043500

35053501
// GetGeocodingForNumber returns the location we think the number was first acquired in. This is
35063502
// just our best guess, there is no guarantee to its accuracy.
35073503
func GetGeocodingForNumber(number *PhoneNumber, lang string) (string, error) {
3508-
geocoding, _, err := getValueForNumber(geocodingPrefixMap, geocodingDataPath, geocodingData, lang, 10, number)
3504+
geocoding, _, err := getValueForNumber(geocodingPrefixMap, geocodingData, geocodingDataPath, lang, 10, number)
35093505
if err != nil || geocoding != "" {
35103506
return geocoding, err
35113507
}
35123508

35133509
// fallback to english
3514-
geocoding, _, err = getValueForNumber(geocodingPrefixMap, geocodingDataPath, geocodingData, "en", 10, number)
3510+
geocoding, _, err = getValueForNumber(geocodingPrefixMap, geocodingData, geocodingDataPath, "en", 10, number)
35153511
if err != nil || geocoding != "" {
35163512
return geocoding, err
35173513
}

serialize.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func loadPrefixMap(data []byte) (*intStringMap, error) {
5050
maxLength := 0
5151
mappings := make(map[int]string, mappingCount)
5252
prefix := 0
53-
for i := 0; i < int(mappingCount); i++ {
53+
for range int(mappingCount) {
5454
// first read our diff
5555
diff, err := binary.ReadUvarint(reader)
5656
if err != nil {
@@ -133,7 +133,7 @@ func loadIntArrayMap(data []byte) (*intStringArrayMap, error) {
133133
maxLength := 0
134134
mappings := make(map[int][]string, mappingCount)
135135
key := 0
136-
for i := 0; i < int(mappingCount); i++ {
136+
for range int(mappingCount) {
137137
// first read our diff
138138
diff, err := binary.ReadUvarint(reader)
139139
if err != nil {
@@ -149,7 +149,7 @@ func loadIntArrayMap(data []byte) (*intStringArrayMap, error) {
149149
}
150150

151151
keyValues := make([]string, valueCount)
152-
for i := 0; i < int(valueCount); i++ {
152+
for i := range int(valueCount) {
153153
var valueIntern uint16
154154
err = binary.Read(reader, binary.LittleEndian, &valueIntern)
155155
if err != nil || int(valueIntern) >= len(values) {

0 commit comments

Comments
 (0)