Skip to content

Commit

Permalink
New countryphone package (Vonage#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni-vonage authored Jul 26, 2024
1 parent 712437f commit 072b6a3
Show file tree
Hide file tree
Showing 10 changed files with 1,155 additions and 27 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.98.1
1.99.0
10 changes: 5 additions & 5 deletions examples/service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.5
replace github.com/Vonage/gosrvlib => ../..

require (
github.com/Vonage/gosrvlib v1.98.1
github.com/Vonage/gosrvlib v1.99.0
github.com/golang/mock v1.6.0
github.com/jstemmer/go-junit-report/v2 v2.1.0
github.com/prometheus/client_golang v1.19.1
Expand Down Expand Up @@ -35,7 +35,7 @@ require (
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand Down Expand Up @@ -109,9 +109,9 @@ require (
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/api v0.189.0 // indirect
google.golang.org/genproto v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
18 changes: 10 additions & 8 deletions examples/service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ cloud.google.com/go/beyondcorp v1.0.9 h1:1UeDTSkNGk0XaXoQ9Z76PxtB8cJPmBZRnW2HzeK
cloud.google.com/go/beyondcorp v1.0.9/go.mod h1:xa0eU8tIbYVraMOpRh5V9PirdYROvTUcPayJW9UlSNs=
cloud.google.com/go/bigquery v1.62.0 h1:SYEA2f7fKqbSRRBHb7g0iHTtZvtPSPYdXfmqsjpsBwo=
cloud.google.com/go/bigquery v1.62.0/go.mod h1:5ee+ZkF1x/ntgCsFQJAQTM3QkAZOecfCmvxhkJsWRSA=
cloud.google.com/go/bigtable v1.27.2-0.20240725222120-ce31365acc54 h1:KOjHlKEA0UzDD9asy7vFmv/mOwj/UH8IDjKl5LfEJXU=
cloud.google.com/go/bigtable v1.27.2-0.20240725222120-ce31365acc54/go.mod h1:NmJ2jfoB34NxQyk4w7UCchopqE9r+a186ewvGrM79TI=
cloud.google.com/go/billing v1.18.8 h1:F0QnYXQ/o86zkuLMwJe+yLlPj/kgfrNCukH+bOhAT2I=
cloud.google.com/go/billing v1.18.8/go.mod h1:oFsuKhKiuxK7dDQ4a8tt5/1cScEo4IzhssWj6TTdi6k=
cloud.google.com/go/binaryauthorization v1.8.6 h1:eMjakmqpE0UPmyki0eGZQfyBSyfNRhGzb64lE32T+ik=
Expand Down Expand Up @@ -365,8 +367,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4=
github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -849,14 +851,14 @@ google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJ
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20240723171418-e6d459c13d2a h1:hPbLwHFm59QoSKUT0uGaL19YN4U9W5lY4+iNXlUBNj0=
google.golang.org/genproto v0.0.0-20240723171418-e6d459c13d2a/go.mod h1:+7gIV7FP6jBo5hiY2lsWA//NkNORQVj0J1Isc/4HzR4=
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a h1:YIa/rzVqMEokBkPtydCkx1VLmv3An1Uw7w1P1m6EhOY=
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a/go.mod h1:AHT0dDg3SoMOgZGnZk29b5xTbPHMoEC8qthmBLJCpys=
google.golang.org/genproto v0.0.0-20240725223205-93522f1f2a9f h1:htT2I9bZvGm+110zq8bIErMX+WgBWxCzV3ChwbvnKnc=
google.golang.org/genproto v0.0.0-20240725223205-93522f1f2a9f/go.mod h1:Sk3mLpoDFTAp6R4OvlcUgaG4ISTspKeFsIAXMn9Bm4Y=
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f h1:b1Ln/PG8orm0SsBbHZWke8dDp2lrCD4jSmfglFpTZbk=
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f/go.mod h1:AHT0dDg3SoMOgZGnZk29b5xTbPHMoEC8qthmBLJCpys=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20240722135656-d784300faade h1:fc+h2kSr2nW2DHxAdGYeX3bnkr4iFsKHUu9Fi6Rh4Y8=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20240722135656-d784300faade/go.mod h1:5/MT647Cn/GGhwTpXC7QqcaR5Cnee4v4MKCU1/nwnIQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a h1:hqK4+jJZXCU4pW7jsAdGOVFIfLHQeV7LaizZKnZ84HI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f h1:RARaIm8pxYuxyNPbBQf5igT7XdOyCNtat1qAT2ZxjU4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ require (
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand Down Expand Up @@ -137,9 +137,9 @@ require (
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/api v0.189.0 // indirect
google.golang.org/genproto v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ cloud.google.com/go/beyondcorp v1.0.9 h1:1UeDTSkNGk0XaXoQ9Z76PxtB8cJPmBZRnW2HzeK
cloud.google.com/go/beyondcorp v1.0.9/go.mod h1:xa0eU8tIbYVraMOpRh5V9PirdYROvTUcPayJW9UlSNs=
cloud.google.com/go/bigquery v1.62.0 h1:SYEA2f7fKqbSRRBHb7g0iHTtZvtPSPYdXfmqsjpsBwo=
cloud.google.com/go/bigquery v1.62.0/go.mod h1:5ee+ZkF1x/ntgCsFQJAQTM3QkAZOecfCmvxhkJsWRSA=
cloud.google.com/go/bigtable v1.27.2-0.20240725222120-ce31365acc54 h1:KOjHlKEA0UzDD9asy7vFmv/mOwj/UH8IDjKl5LfEJXU=
cloud.google.com/go/bigtable v1.27.2-0.20240725222120-ce31365acc54/go.mod h1:NmJ2jfoB34NxQyk4w7UCchopqE9r+a186ewvGrM79TI=
cloud.google.com/go/billing v1.18.8 h1:F0QnYXQ/o86zkuLMwJe+yLlPj/kgfrNCukH+bOhAT2I=
cloud.google.com/go/billing v1.18.8/go.mod h1:oFsuKhKiuxK7dDQ4a8tt5/1cScEo4IzhssWj6TTdi6k=
cloud.google.com/go/binaryauthorization v1.8.6 h1:eMjakmqpE0UPmyki0eGZQfyBSyfNRhGzb64lE32T+ik=
Expand Down Expand Up @@ -397,8 +399,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4=
github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -991,14 +993,14 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20220503193339-ba3ae3f07e29/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/genproto v0.0.0-20240723171418-e6d459c13d2a h1:hPbLwHFm59QoSKUT0uGaL19YN4U9W5lY4+iNXlUBNj0=
google.golang.org/genproto v0.0.0-20240723171418-e6d459c13d2a/go.mod h1:+7gIV7FP6jBo5hiY2lsWA//NkNORQVj0J1Isc/4HzR4=
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a h1:YIa/rzVqMEokBkPtydCkx1VLmv3An1Uw7w1P1m6EhOY=
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a/go.mod h1:AHT0dDg3SoMOgZGnZk29b5xTbPHMoEC8qthmBLJCpys=
google.golang.org/genproto v0.0.0-20240725223205-93522f1f2a9f h1:htT2I9bZvGm+110zq8bIErMX+WgBWxCzV3ChwbvnKnc=
google.golang.org/genproto v0.0.0-20240725223205-93522f1f2a9f/go.mod h1:Sk3mLpoDFTAp6R4OvlcUgaG4ISTspKeFsIAXMn9Bm4Y=
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f h1:b1Ln/PG8orm0SsBbHZWke8dDp2lrCD4jSmfglFpTZbk=
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f/go.mod h1:AHT0dDg3SoMOgZGnZk29b5xTbPHMoEC8qthmBLJCpys=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20240722135656-d784300faade h1:fc+h2kSr2nW2DHxAdGYeX3bnkr4iFsKHUu9Fi6Rh4Y8=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20240722135656-d784300faade/go.mod h1:5/MT647Cn/GGhwTpXC7QqcaR5Cnee4v4MKCU1/nwnIQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a h1:hqK4+jJZXCU4pW7jsAdGOVFIfLHQeV7LaizZKnZ84HI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f h1:RARaIm8pxYuxyNPbBQf5igT7XdOyCNtat1qAT2ZxjU4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
Expand Down
2 changes: 1 addition & 1 deletion pkg/countrycode/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Data struct {
}

// New generates the country data, including various indexes.
// If the cdata parameter is nil, the default data is used.
// If the cdata parameter is nil, the embedded default data is used.
// The generated object should be reused to avoid copying the data.
//
// Default data sources (updated at: 2024-07-17):
Expand Down
239 changes: 239 additions & 0 deletions pkg/countryphone/countryphone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
Package countryphone provides geographical information of phone numbers. Country
phone codes are defined by the International Telecommunication Union (ITU) in
ITU-T standards E.123 and E.164.
The default phone prefixes data can be overridden with a custom dataset. The
data is stored in a trie for fast prefix matching.
*/
package countryphone

import (
"fmt"

"github.com/Vonage/gosrvlib/pkg/numtrie"
)

// InPrefixGroup stores the type and geographical information of a group of phone
// number prefixes.
type InPrefixGroup struct {
// Name is the name of the group or geographical area.
Name string `json:"name"`

// Type is the type of group or geographical area:
// - 0 = ""
// - 1 = "state"
// - 2 = "province or territory"
// - 3 = "nation or territory"
// - 4 = "non-geographic"
// - 5 = "other"
Type int `json:"type"`

// PrefixType is the type of phone number prefix:
// - 0 = ""
// - 1 = "landline"
// - 2 = "mobile"
// - 3 = "pager"
// - 4 = "satellite"
// - 5 = "special service"
// - 6 = "virtual"
// - 7 = "other"
PrefixType int `json:"prefixType"`

// Prefixes is a list of phone number prefixes (including the Country Code).
Prefixes []string `json:"prefixes"`
}

// InCountryData stores all the phone number prefixes information for a country.
type InCountryData struct {
// CC is the Country Calling code (e.g. "1" for "US" and "CA").
CC string `json:"cc"`

// Groups is a list of phone prefixes information grouped by geographical
// area or type.
Groups []InPrefixGroup `json:"groups"`
}

// InData is a type alias for a map of country Alpha-2 codes to InCountryData.
type InData = map[string]*InCountryData

// GeoInfo stores geographical information of a phone number.
type GeoInfo struct {
// Alpha2 is the ISO-3166 Alpha-2 Country Code.
Alpha2 string `json:"alpha2"`

// Area is the geographical area.
Area string `json:"area"`

// Type is the type of area:
// - 0 = ""
// - 1 = "state"
// - 2 = "province or territory"
// - 3 = "nation or territory"
// - 4 = "non-geographic"
// - 5 = "other"
Type int `json:"type"`
}

// NumInfo stores the number type and geographical information of a phone number.
type NumInfo struct {
// Type is the type of number:
// - 0 = ""
// - 1 = "landline"
// - 2 = "mobile"
// - 3 = "pager"
// - 4 = "satellite"
// - 5 = "special service"
// - 6 = "virtual"
// - 7 = "other"
Type int `json:"type"`

// Geo is the geographical information.
Geo []*GeoInfo `json:"geo"`
}

// PrefixData is a type alias for a map of phone number prefixes to NumData.
type PrefixData = map[string]*NumInfo

// Data is the main data structure that stores phone number prefixes and their
// information.
type Data struct {
enumNumberType [8]string
enumAreaType [6]string
trie *numtrie.Node[NumInfo]
}

// New initialize the search trie with the given data.
// If data is nil, the embedded default dataset is used.
func New(data InData) *Data {
d := &Data{}

d.loadEnums()

if data == nil {
data = defaultData()
}

d.loadData(data)

return d
}

// NumberInfo returns the number type and geographical information for the given
// phone number prefix.
//
// NOTE: see the "github.com/Vonage/gosrvlib/pkg/countrycode" package to get the
// country information from the Alpha2 code.
func (d *Data) NumberInfo(num string) (*NumInfo, error) {
data, status := d.trie.Get(num)

if status < 0 || data == nil {
return nil, fmt.Errorf("no match for prefix %s", num)
}

return data, nil
}

// NumberType returns the string representation of the number type.
func (d *Data) NumberType(t int) (string, error) {
if t < 0 || t >= len(d.enumNumberType) {
return "", fmt.Errorf("invalid number type %d", t)
}

return d.enumNumberType[t], nil
}

// AreaType returns the string representation of the area type.
func (d *Data) AreaType(t int) (string, error) {
if t < 0 || t >= len(d.enumAreaType) {
return "", fmt.Errorf("invalid area type %d", t)
}

return d.enumAreaType[t], nil
}

// loadEnums initializes the enumeration arrays.
func (d *Data) loadEnums() {
d.enumNumberType = [...]string{
"",
"landline",
"mobile",
"pager",
"satellite",
"special service",
"virtual",
"other",
}

d.enumAreaType = [...]string{
"",
"state",
"province or territory",
"nation or territory",
"non-geographic",
"other",
}
}

func (d *Data) insertPrefix(prefix string, info *NumInfo) {
v, status := d.trie.Get(prefix)

if (status == numtrie.StatusMatchFull || status == numtrie.StatusMatchPartial) &&
(v != nil) && (len(v.Geo) > 0) {
// the node already exists > merge the data
if len(info.Geo) > 0 {
info.Geo = append(v.Geo, info.Geo...)
}
}

d.trie.Add(prefix, info)
}

func (d *Data) insertGroups(a2, cc string, data []InPrefixGroup) {
if len(data) == 0 {
return
}

for _, g := range data {
info := &NumInfo{
Type: g.PrefixType,
Geo: []*GeoInfo{
{
Alpha2: a2,
Area: g.Name,
Type: g.Type,
},
},
}

if len(g.Prefixes) == 0 {
d.insertPrefix(cc, info)
continue
}

for _, p := range g.Prefixes {
d.insertPrefix(p, info)
}
}
}

// loadData loads the phone number prefixes and their data into the trie.
func (d *Data) loadData(data InData) {
d.trie = numtrie.New[NumInfo]()

for k, v := range data {
info := &NumInfo{
Type: 0,
Geo: []*GeoInfo{
{
Alpha2: k,
Area: "",
Type: 0,
},
},
}

d.insertPrefix(v.CC, info)
d.insertGroups(k, v.CC, v.Groups)
}
}
Loading

0 comments on commit 072b6a3

Please sign in to comment.