diff --git a/country/code_fetcher.go b/country/code_fetcher.go index 2f8d73f..b473979 100644 --- a/country/code_fetcher.go +++ b/country/code_fetcher.go @@ -134,17 +134,25 @@ func (icf *IndexedCodeFetcher) Search(searchTerm string, operator SearchOperator } func (icf *IndexedCodeFetcher) prepareIndex() { - ccs := icf.CountryCodes + codes := icf.CountryCodes - if ccs == nil { - ccs = DefaultCountryCodes + if codes == nil { + codes = DefaultCountryCodes } - icf.indexedCountryCodes = make(map[string]CountryCode, len(ccs)) + icf.indexedCountryCodes = make(map[string]CountryCode, len(codes)) - for _, cc := range ccs { - for _, k := range icf.ExtractKeys(cc) { - icf.indexedCountryCodes[icf.NormalizeKey(k)] = cc + for _, code := range codes { + for _, k := range icf.ExtractKeys(code) { + normalizedKey := icf.NormalizeKey(k) + + // prevent codes with assignments with smaller priority to overwrite + // those with higher priority + if cc, ok := icf.indexedCountryCodes[normalizedKey]; ok && cc.Assignment < code.Assignment { + continue + } + + icf.indexedCountryCodes[normalizedKey] = code } } } diff --git a/country/code_fetcher_test.go b/country/code_fetcher_test.go index 7f5340e..5d1459d 100644 --- a/country/code_fetcher_test.go +++ b/country/code_fetcher_test.go @@ -84,7 +84,7 @@ func TestCodeFetcher_Search(t *testing.T) { mustFetch("UM"), mustFetch("AE"), mustFetch("TZ"), - mustFetch("UK"), + mustFetch("GB"), }, }, {