Skip to content

Commit 2fa96f5

Browse files
authored
Merge pull request #44 from rohithv-tw/QueryCountryByCallingCode
2 parents 2d99937 + e6ca13d commit 2fa96f5

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

query.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ var queryInstance *Query
1111

1212
// Query contains queries for countries, cities, etc.
1313
type Query struct {
14-
Countries map[string]Country
15-
NameToAlpha2 map[string]string
16-
Alpha3ToAlpha2 map[string]string
17-
NativeNameToAlpha2 map[string]string
14+
Countries map[string]Country
15+
NameToAlpha2 map[string]string
16+
Alpha3ToAlpha2 map[string]string
17+
NativeNameToAlpha2 map[string]string
18+
CallingCodeToAlpha2 map[string]string
1819
}
1920

2021
// FindCountryByName finds a country by given name
@@ -58,6 +59,14 @@ func (q *Query) FindCountryByAlpha(code string) (result Country, err error) {
5859
}
5960
}
6061

62+
func (q *Query) FindCountryByCallingCode(callingCode string) (result Country, err error) {
63+
alpha2, exists := q.CallingCodeToAlpha2[callingCode]
64+
if !exists {
65+
return Country{}, makeError("Could not find country with callingCode", callingCode)
66+
}
67+
return q.Countries[alpha2], nil
68+
}
69+
6170
// FindAllCountries returns a list of all countries
6271
func (q *Query) FindAllCountries() (countries map[string]Country) {
6372
return q.Countries

query_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gountries
33
import (
44
"fmt"
55
"math/rand"
6+
"reflect"
67
"sort"
78
"testing"
89

@@ -359,8 +360,40 @@ func TestFindSubdivisionCountryByNameError(t *testing.T) {
359360

360361
_, err := query.FindSubdivisionCountryByName(subdivisionName)
361362
if err != nil {
362-
assert.Equal(t, "gountries error. Invalid subdivision name: " + subdivisionName, err.Error())
363+
assert.Equal(t, "gountries error. Invalid subdivision name: "+subdivisionName, err.Error())
363364
} else {
364365
t.Fail()
365366
}
366367
}
368+
369+
func TestFindCountryByCallingCode(t *testing.T) {
370+
country, _ := query.FindCountryByName("India")
371+
tests := []struct {
372+
name string
373+
callingCode string
374+
expectedError error
375+
expectedCountry Country
376+
}{
377+
{
378+
name: "Find country by valid calling code should return country when calling code is present for countries",
379+
callingCode: "91",
380+
expectedError: nil,
381+
expectedCountry: country,
382+
},
383+
{
384+
name: "Find country by invalid country code should return error when searching for country",
385+
callingCode: "abc",
386+
expectedError: fmt.Errorf("gountries error. Could not find country with callingCode: abc"),
387+
expectedCountry: Country{},
388+
},
389+
}
390+
for _, test := range tests {
391+
t.Run(test.name, func(t *testing.T) {
392+
actualCountry, actualError := query.FindCountryByCallingCode(test.callingCode)
393+
if !(reflect.DeepEqual(test.expectedCountry, actualCountry) &&
394+
reflect.DeepEqual(test.expectedError, actualError)) {
395+
t.Fail()
396+
}
397+
})
398+
}
399+
}

setup.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func NewFromPath(dataPath string) *Query {
2929
queryInstance.NameToAlpha2 = populateNameIndex(queryInstance.Countries)
3030
queryInstance.Alpha3ToAlpha2 = populateAlphaIndex(queryInstance.Countries)
3131
queryInstance.NativeNameToAlpha2 = populateNativeNameIndex(queryInstance.Countries)
32+
queryInstance.CallingCodeToAlpha2 = populateCallingCodeIndex(queryInstance.Countries)
3233

3334
subdivisions := populateSubdivisions(dataPath)
3435
for k := range queryInstance.Countries {
@@ -209,3 +210,13 @@ func populateNativeNameIndex(countries map[string]Country) map[string]string {
209210
}
210211
return index
211212
}
213+
214+
func populateCallingCodeIndex(countries map[string]Country) map[string]string {
215+
index := make(map[string]string)
216+
for alpha2, country := range countries {
217+
for _, callingCode := range country.CallingCodes {
218+
index[callingCode] = alpha2
219+
}
220+
}
221+
return index
222+
}

0 commit comments

Comments
 (0)