@@ -17,6 +17,53 @@ import (
17
17
"google.golang.org/protobuf/proto"
18
18
)
19
19
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
+
20
67
const (
21
68
// MIN_LENGTH_FOR_NSN is the minimum and maximum length of the national significant number.
22
69
MIN_LENGTH_FOR_NSN = 2
@@ -428,7 +475,7 @@ var (
428
475
52 : true , // Mexico
429
476
}
430
477
431
- m = sync.Mutex {}
478
+ dataLoadMutex = sync.Mutex {}
432
479
)
433
480
434
481
// INTERNATIONAL and NATIONAL formats are consistent with the definition
@@ -676,10 +723,7 @@ func writeToCountryCodeToNonGeographicalMetadataMap(key int, v *PhoneMetadata) {
676
723
countryCodeToNonGeographicalMetadataMap [key ] = v
677
724
}
678
725
679
- func loadMetadataFromFile (
680
- regionCode string ,
681
- countryCallingCode int ) error {
682
-
726
+ func loadMetadataFromFile () error {
683
727
metadataCollection , err := MetadataCollection ()
684
728
if err != nil {
685
729
return err
@@ -3319,54 +3363,6 @@ func IsMobileNumberPortableRegion(regionCode string) bool {
3319
3363
return metadata .GetMobileNumberPortableRegion ()
3320
3364
}
3321
3365
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
-
3370
3366
// GetTimezonesForPrefix returns a slice of Timezones corresponding to the number passed
3371
3367
// or error when it is impossible to convert the string to int
3372
3368
// The algorythm tries to match the timezones starting from the maximum
@@ -3409,17 +3405,17 @@ func GetTimezonesForNumber(number *PhoneNumber) ([]string, error) {
3409
3405
return GetTimezonesForPrefix (e164 )
3410
3406
}
3411
3407
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 ()
3415
3411
3416
3412
_ , ok := langMap [language ]
3417
3413
if ok {
3418
3414
return true , nil
3419
3415
}
3420
3416
3421
3417
// do we have data for this language
3422
- data , err := binMap .ReadFile (filename )
3418
+ data , err := dataFS .ReadFile (dir + "/" + language + ".txt.gz" )
3423
3419
if err != nil {
3424
3420
if errors .Is (err , fs .ErrNotExist ) {
3425
3421
return false , nil
@@ -3439,8 +3435,8 @@ func fillLangMap(langMap map[string]*intStringMap, binMap embed.FS, filename, la
3439
3435
return false , err
3440
3436
}
3441
3437
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 )
3444
3440
if ! ok || err != nil {
3445
3441
return "" , 0 , err
3446
3442
}
@@ -3490,7 +3486,7 @@ func GetSafeCarrierDisplayNameForNumber(phoneNumber *PhoneNumber, lang string) (
3490
3486
// GetCarrierWithPrefixForNumber returns the carrier we believe the number belongs to, as well as
3491
3487
// its prefix. Note due to number porting this is only a guess, there is no guarantee to its accuracy.
3492
3488
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 )
3494
3490
if err != nil {
3495
3491
return "" , 0 , err
3496
3492
}
@@ -3499,19 +3495,19 @@ func GetCarrierWithPrefixForNumber(number *PhoneNumber, lang string) (string, in
3499
3495
}
3500
3496
3501
3497
// fallback to english
3502
- return getValueForNumber (carrierPrefixMap , carrierDataPath , carrierData , "en" , 10 , number )
3498
+ return getValueForNumber (carrierPrefixMap , carrierData , carrierDataPath , "en" , 10 , number )
3503
3499
}
3504
3500
3505
3501
// GetGeocodingForNumber returns the location we think the number was first acquired in. This is
3506
3502
// just our best guess, there is no guarantee to its accuracy.
3507
3503
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 )
3509
3505
if err != nil || geocoding != "" {
3510
3506
return geocoding , err
3511
3507
}
3512
3508
3513
3509
// fallback to english
3514
- geocoding , _ , err = getValueForNumber (geocodingPrefixMap , geocodingDataPath , geocodingData , "en" , 10 , number )
3510
+ geocoding , _ , err = getValueForNumber (geocodingPrefixMap , geocodingData , geocodingDataPath , "en" , 10 , number )
3515
3511
if err != nil || geocoding != "" {
3516
3512
return geocoding , err
3517
3513
}
0 commit comments