diff --git a/country/code_fetcher.go b/country/code_fetcher.go index ccd5650..c5daa63 100644 --- a/country/code_fetcher.go +++ b/country/code_fetcher.go @@ -10,25 +10,33 @@ import ( var ( Alpha2CodeFetcher = &IndexedCodeFetcher{ NormalizeKey: strings.ToLower, - ExtractKey: func(cc CountryCode) string { return cc.Alpha2 }, + ExtractKeys: func(cc CountryCode) []string { return []string{cc.Alpha2} }, } Alpha3CodeFetcher = &IndexedCodeFetcher{ NormalizeKey: strings.ToLower, - ExtractKey: func(cc CountryCode) string { return cc.Alpha3 }, + ExtractKeys: func(cc CountryCode) []string { return []string{cc.Alpha3} }, } NameCodeFetcher = &IndexedCodeFetcher{ NormalizeKey: func(k string) string { return strings.ToLower(stringutil.DecodeToASCII(k)) }, - ExtractKey: func(cc CountryCode) string { return cc.Name }, + ExtractKeys: func(cc CountryCode) []string { return []string{cc.Name} }, + } + + AlternateNameCodeFetcher = &IndexedCodeFetcher{ + NormalizeKey: func(k string) string { + return strings.ToLower(stringutil.DecodeToASCII(k)) + }, + ExtractKeys: func(cc CountryCode) []string { return cc.AlternateNames }, } DefaultCodeFetcher = MultiCodeFetcher{ Alpha2CodeFetcher, Alpha3CodeFetcher, NameCodeFetcher, + AlternateNameCodeFetcher, } ) @@ -38,7 +46,7 @@ type CodeFetcher interface { type IndexedCodeFetcher struct { NormalizeKey func(string) string - ExtractKey func(CountryCode) string + ExtractKeys func(CountryCode) []string // If left nil it will use DefaultCountryCodes CountryCodes []CountryCode @@ -62,7 +70,9 @@ func (icf *IndexedCodeFetcher) Fetch(key string) (CountryCode, bool) { icf.indexedCountryCodes = make(map[string]CountryCode, len(ccs)) for _, cc := range ccs { - icf.indexedCountryCodes[icf.NormalizeKey(icf.ExtractKey(cc))] = cc + for _, k := range icf.ExtractKeys(cc) { + icf.indexedCountryCodes[icf.NormalizeKey(k)] = cc + } } }) diff --git a/country/code_fetcher_test.go b/country/code_fetcher_test.go index 88981f6..db8dabe 100644 --- a/country/code_fetcher_test.go +++ b/country/code_fetcher_test.go @@ -7,15 +7,19 @@ import ( ) func TestCodeFetcher(t *testing.T) { - mustFetch := func(k string) CountryCode { - cc, ok := Alpha2CodeFetcher.Fetch(k) + var ( + zeroValue CountryCode - if !ok { - panic("not found") - } + mustFetch = func(k string) CountryCode { + cc, ok := Alpha2CodeFetcher.Fetch(k) - return cc - } + if !ok { + panic("not found") + } + + return cc + } + ) for _, tt := range []struct { k string @@ -46,11 +50,24 @@ func TestCodeFetcher(t *testing.T) { k: "cote d'ivoire", want: mustFetch("CI"), }, + { + k: "russia", + want: mustFetch("RU"), + }, + { + k: "Türkiye", + want: mustFetch("TR"), + }, {k: ""}, } { cc, ok := DefaultCodeFetcher.Fetch(tt.k) - assert.Equal(t, tt.want != CountryCode{}, ok) assert.Equal(t, tt.want, cc) + + if ok { + assert.NotEqual(t, zeroValue, cc) + } else { + assert.Equal(t, zeroValue, cc) + } } } diff --git a/country/country_code.go b/country/country_code.go index 3e87994..49fdb49 100644 --- a/country/country_code.go +++ b/country/country_code.go @@ -260,12 +260,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Bosnia and Herzegovina", - Alpha2: "BA", - Alpha3: "BIH", - Numeric: 70, - DialingCode: "+387", - Assignment: OfficiallyAssigned, + Name: "Bosnia and Herzegovina", + Alpha2: "BA", + Alpha3: "BIH", + Numeric: 70, + DialingCode: "+387", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"Bosnia & Herzegovina"}, }, /** @@ -386,12 +387,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Saint Barth\u00E9lemy", - Alpha2: "BL", - Alpha3: "BLM", - Numeric: 652, - DialingCode: "+590", - Assignment: OfficiallyAssigned, + Name: "Saint Barth\u00E9lemy", + Alpha2: "BL", + Alpha3: "BLM", + Numeric: 652, + DialingCode: "+590", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"St. Barth\u00E9lemy"}, }, /** @@ -1453,12 +1455,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Hong Kong", - Alpha2: "HK", - Alpha3: "HKG", - Numeric: 344, - DialingCode: "+852", - Assignment: OfficiallyAssigned, + Name: "Hong Kong", + Alpha2: "HK", + Alpha3: "HKG", + Numeric: 344, + DialingCode: "+852", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"HongKong"}, }, /** @@ -1649,12 +1652,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Iran, Islamic Republic of", - Alpha2: "IR", - Alpha3: "IRN", - Numeric: 364, - DialingCode: "+98", - Assignment: OfficiallyAssigned, + Name: "Iran, Islamic Republic of", + Alpha2: "IR", + Alpha3: "IRN", + Numeric: 364, + DialingCode: "+98", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"Iran"}, }, /** @@ -1843,12 +1847,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Korea, Republic of", - Alpha2: "KR", - Alpha3: "KOR", - Numeric: 410, - DialingCode: "+82", - Assignment: OfficiallyAssigned, + Name: "Korea, Republic of", + Alpha2: "KR", + Alpha3: "KOR", + Numeric: 410, + DialingCode: "+82", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"South Korea"}, }, /** @@ -2109,12 +2114,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Saint Martin (French part)", - Alpha2: "MF", - Alpha3: "MAF", - Numeric: 663, - DialingCode: "+590", - Assignment: OfficiallyAssigned, + Name: "Saint Martin (French part)", + Alpha2: "MF", + Alpha3: "MAF", + Numeric: 663, + DialingCode: "+590", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"Saint Martin"}, }, /** @@ -2825,12 +2831,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Russian Federation", - Alpha2: "RU", - Alpha3: "RUS", - Numeric: 643, - DialingCode: "+7", - Assignment: OfficiallyAssigned, + Name: "Russian Federation", + Alpha2: "RU", + Alpha3: "RUS", + Numeric: 643, + DialingCode: "+7", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"Russia"}, }, /** @@ -3363,12 +3370,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Turkey", - Alpha2: "TR", - Alpha3: "TUR", - Numeric: 792, - DialingCode: "+90", - Assignment: OfficiallyAssigned, + Name: "Turkey", + Alpha2: "TR", + Alpha3: "TUR", + Numeric: 792, + DialingCode: "+90", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"Türkiye"}, }, /** @@ -3405,12 +3413,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Taiwan, Province of China", - Alpha2: "TW", - Alpha3: "TWN", - Numeric: 158, - DialingCode: "+886", - Assignment: OfficiallyAssigned, + Name: "Taiwan, Province of China", + Alpha2: "TW", + Alpha3: "TWN", + Numeric: 158, + DialingCode: "+886", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"Taiwan"}, }, /** @@ -3419,12 +3428,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Tanzania, United Republic of", - Alpha2: "TZ", - Alpha3: "TZA", - Numeric: 834, - DialingCode: "+255", - Assignment: OfficiallyAssigned, + Name: "Tanzania, United Republic of", + Alpha2: "TZ", + Alpha3: "TZA", + Numeric: 834, + DialingCode: "+255", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"Tanzania"}, }, /** @@ -3461,12 +3471,13 @@ var ( * Exceptionally reserved] */ CountryCode{ - Name: "United Kingdom", - Alpha2: "UK", - Alpha3: "", - Numeric: -1, - DialingCode: "+44", - Assignment: ExceptionallyReserved, + Name: "United Kingdom", + Alpha2: "UK", + Alpha3: "", + Numeric: -1, + DialingCode: "+44", + Assignment: ExceptionallyReserved, + AlternateNames: []string{"Northern Ireland"}, }, /** * United States Minor Outlying Islands @@ -3600,12 +3611,13 @@ var ( * Officially assigned] */ CountryCode{ - Name: "Viet Nam", - Alpha2: "VN", - Alpha3: "VNM", - Numeric: 704, - DialingCode: "+84", - Assignment: OfficiallyAssigned, + Name: "Viet Nam", + Alpha2: "VN", + Alpha3: "VNM", + Numeric: 704, + DialingCode: "+84", + Assignment: OfficiallyAssigned, + AlternateNames: []string{"Vietnam"}, }, /** @@ -3822,10 +3834,11 @@ const ( ) type CountryCode struct { - Name string - Alpha2 string - Alpha3 string - Numeric int - DialingCode string - Assignment Assignment + Name string + Alpha2 string + Alpha3 string + Numeric int + DialingCode string + Assignment Assignment + AlternateNames []string } diff --git a/go.mod b/go.mod index 69c6c1c..fd70711 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/upfluence/pkg -go 1.23 +go 1.23.0 + +toolchain go1.23.1 require ( github.com/Microsoft/go-winio v0.4.14 // indirect @@ -33,11 +35,10 @@ require ( github.com/upfluence/log v0.0.5 github.com/upfluence/stats v0.1.4 github.com/upfluence/thrift v2.4.4+incompatible - golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 golang.org/x/oauth2 v0.19.0 golang.org/x/sync v0.8.0 - golang.org/x/term v0.13.0 - golang.org/x/text v0.13.0 + golang.org/x/term v0.24.0 + golang.org/x/text v0.18.0 golang.org/x/time v0.3.0 ) @@ -49,6 +50,7 @@ require ( github.com/denisenkom/go-mssqldb v0.11.0 // indirect github.com/getsentry/sentry-go v0.25.0 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect github.com/jinzhu/now v1.1.2 // indirect github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect @@ -57,8 +59,10 @@ require ( github.com/prometheus/common v0.45.0 // indirect go.uber.org/atomic v1.6.0 // indirect go.uber.org/multierr v1.5.0 // indirect - golang.org/x/crypto v0.14.0 // indirect - golang.org/x/sys v0.13.0 // indirect + golang.org/x/crypto v0.27.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/tools v0.25.0 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index cda3347..c176089 100644 --- a/go.sum +++ b/go.sum @@ -510,11 +510,9 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= -golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk= -golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -547,8 +545,8 @@ golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg= @@ -590,14 +588,14 @@ golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= -golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM= +golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=