Skip to content

Commit

Permalink
county: Add alternateNames
Browse files Browse the repository at this point in the history
  • Loading branch information
tgallice committed Nov 27, 2024
1 parent eea9ca5 commit 732c298
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 103 deletions.
20 changes: 15 additions & 5 deletions country/code_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)

Expand All @@ -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
Expand All @@ -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
}
}
})

Expand Down
33 changes: 25 additions & 8 deletions country/code_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
156 changes: 84 additions & 72 deletions country/country_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},

/**
Expand Down Expand Up @@ -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"},
},

/**
Expand Down Expand Up @@ -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"},
},

/**
Expand Down Expand Up @@ -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"},
},

/**
Expand Down Expand Up @@ -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"},
},

/**
Expand Down Expand Up @@ -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"},
},

/**
Expand Down Expand Up @@ -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"},
},

/**
Expand Down Expand Up @@ -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"},
},

/**
Expand Down Expand Up @@ -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"},
},

/**
Expand All @@ -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"},
},

/**
Expand Down Expand Up @@ -3600,12 +3610,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"},
},

/**
Expand Down Expand Up @@ -3822,10 +3833,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
}
16 changes: 10 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
)

Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 732c298

Please sign in to comment.