From 97def5e4e4cf4ee0f050d48269d1013f5d680f24 Mon Sep 17 00:00:00 2001 From: Xavier Goffin Date: Thu, 30 Apr 2026 11:28:42 +0200 Subject: [PATCH 1/3] country/code_fetcher.go: prevent records with smaller assignment priority from overwriting those with higher priority --- country/code_fetcher.go | 10 +++++++++- country/code_fetcher_test.go | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/country/code_fetcher.go b/country/code_fetcher.go index 2f8d73f..889798a 100644 --- a/country/code_fetcher.go +++ b/country/code_fetcher.go @@ -144,7 +144,15 @@ func (icf *IndexedCodeFetcher) prepareIndex() { for _, cc := range ccs { for _, k := range icf.ExtractKeys(cc) { - icf.indexedCountryCodes[icf.NormalizeKey(k)] = cc + normalizedKey := icf.NormalizeKey(k) + + if assigned, ok := icf.indexedCountryCodes[normalizedKey]; ok { + if assigned.Assignment < cc.Assignment { + continue + } + } + + icf.indexedCountryCodes[normalizedKey] = cc } } } 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"), }, }, { From be005978fa586d5f7f20249129dd83f8169a2cf0 Mon Sep 17 00:00:00 2001 From: Xavier Goffin Date: Thu, 30 Apr 2026 11:44:36 +0200 Subject: [PATCH 2/3] country/code_fetcher.go: one-line if --- country/code_fetcher.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/country/code_fetcher.go b/country/code_fetcher.go index 889798a..b26e59d 100644 --- a/country/code_fetcher.go +++ b/country/code_fetcher.go @@ -146,10 +146,8 @@ func (icf *IndexedCodeFetcher) prepareIndex() { for _, k := range icf.ExtractKeys(cc) { normalizedKey := icf.NormalizeKey(k) - if assigned, ok := icf.indexedCountryCodes[normalizedKey]; ok { - if assigned.Assignment < cc.Assignment { - continue - } + if assigned, ok := icf.indexedCountryCodes[normalizedKey]; ok && assigned.Assignment < cc.Assignment { + continue } icf.indexedCountryCodes[normalizedKey] = cc From c77c9bd1906a6e83b6402316f747e8fa5b121180 Mon Sep 17 00:00:00 2001 From: Xavier Goffin Date: Thu, 30 Apr 2026 11:51:42 +0200 Subject: [PATCH 3/3] country/code_fetcher.go: rename vars, add a small comment --- country/code_fetcher.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/country/code_fetcher.go b/country/code_fetcher.go index b26e59d..b473979 100644 --- a/country/code_fetcher.go +++ b/country/code_fetcher.go @@ -134,23 +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) { + for _, code := range codes { + for _, k := range icf.ExtractKeys(code) { normalizedKey := icf.NormalizeKey(k) - if assigned, ok := icf.indexedCountryCodes[normalizedKey]; ok && assigned.Assignment < cc.Assignment { + // 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] = cc + icf.indexedCountryCodes[normalizedKey] = code } } }