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 b892576
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 109 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)
}
}
}
169 changes: 91 additions & 78 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 @@ -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"},
},
/**
* <a href="http://en.wikipedia.org/wiki/United_States_Minor_Outlying_Islands">United States Minor Outlying Islands</a>
Expand Down Expand Up @@ -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"},
},

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

0 comments on commit b892576

Please sign in to comment.