Skip to content

Commit a108e89

Browse files
authored
Merge pull request #48 from arktos-venture/develop
Add FindCountriesByCurrency
2 parents 2fa96f5 + 2668ac8 commit a108e89

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

query.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Query struct {
1616
Alpha3ToAlpha2 map[string]string
1717
NativeNameToAlpha2 map[string]string
1818
CallingCodeToAlpha2 map[string]string
19+
CurrencyToAlpha2 map[string][]Country
1920
}
2021

2122
// FindCountryByName finds a country by given name
@@ -67,6 +68,20 @@ func (q *Query) FindCountryByCallingCode(callingCode string) (result Country, er
6768
return q.Countries[alpha2], nil
6869
}
6970

71+
// FindCountriesByCurrency finds a Country based on the given struct data
72+
func (q *Query) FindCountriesByCurrency(currency string) (results []Country, err error) {
73+
if len(currency) != 3 {
74+
return nil, makeError("Invalid currency format", currency)
75+
}
76+
77+
alpha2, exists := q.CurrencyToAlpha2[currency]
78+
if !exists {
79+
return []Country{}, makeError("Could not find countries with currency name", currency)
80+
}
81+
82+
return alpha2, nil
83+
}
84+
7085
// FindAllCountries returns a list of all countries
7186
func (q *Query) FindAllCountries() (countries map[string]Country) {
7287
return q.Countries

query_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,40 @@ func TestFindCountryByCallingCode(t *testing.T) {
397397
})
398398
}
399399
}
400+
401+
func TestFindCountriesByCurrency(t *testing.T) {
402+
tests := []struct {
403+
name string
404+
currency string
405+
expectedError error
406+
expectedCountry int
407+
}{
408+
{
409+
name: "Find countries by valid currency should return countries when currency is present for countries",
410+
currency: "EUR",
411+
expectedError: nil,
412+
expectedCountry: 34,
413+
},
414+
{
415+
name: "Find countries by unknown country code should return error when searching countries",
416+
currency: "ERT",
417+
expectedError: fmt.Errorf("gountries error. Could not find countries with currency name: ERT"),
418+
expectedCountry: 0,
419+
},
420+
{
421+
name: "Find countries by invalid currency length code should return error when searching countries",
422+
currency: "CNYE",
423+
expectedError: fmt.Errorf("gountries error. Invalid currency format: CNYE"),
424+
expectedCountry: 0,
425+
},
426+
}
427+
for _, test := range tests {
428+
t.Run(test.name, func(t *testing.T) {
429+
actualCountry, actualError := query.FindCountriesByCurrency(test.currency)
430+
if !(reflect.DeepEqual(test.expectedCountry, len(actualCountry)) &&
431+
reflect.DeepEqual(test.expectedError, actualError)) {
432+
t.Fail()
433+
}
434+
})
435+
}
436+
}

setup.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func NewFromPath(dataPath string) *Query {
3030
queryInstance.Alpha3ToAlpha2 = populateAlphaIndex(queryInstance.Countries)
3131
queryInstance.NativeNameToAlpha2 = populateNativeNameIndex(queryInstance.Countries)
3232
queryInstance.CallingCodeToAlpha2 = populateCallingCodeIndex(queryInstance.Countries)
33+
queryInstance.CurrencyToAlpha2 = populateCallingCurrencyIndex(queryInstance.Countries)
3334

3435
subdivisions := populateSubdivisions(dataPath)
3536
for k := range queryInstance.Countries {
@@ -220,3 +221,14 @@ func populateCallingCodeIndex(countries map[string]Country) map[string]string {
220221
}
221222
return index
222223
}
224+
225+
func populateCallingCurrencyIndex(countries map[string]Country) map[string][]Country {
226+
index := make(map[string][]Country)
227+
228+
for _, country := range countries {
229+
for _, currency := range country.Currencies {
230+
index[currency] = append(index[currency], country)
231+
}
232+
}
233+
return index
234+
}

0 commit comments

Comments
 (0)