Skip to content

Commit

Permalink
Merge pull request #44 from rohithv-tw/QueryCountryByCallingCode
Browse files Browse the repository at this point in the history
  • Loading branch information
pariz authored May 23, 2022
2 parents 2d99937 + e6ca13d commit 2fa96f5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
17 changes: 13 additions & 4 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ var queryInstance *Query

// Query contains queries for countries, cities, etc.
type Query struct {
Countries map[string]Country
NameToAlpha2 map[string]string
Alpha3ToAlpha2 map[string]string
NativeNameToAlpha2 map[string]string
Countries map[string]Country
NameToAlpha2 map[string]string
Alpha3ToAlpha2 map[string]string
NativeNameToAlpha2 map[string]string
CallingCodeToAlpha2 map[string]string
}

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

func (q *Query) FindCountryByCallingCode(callingCode string) (result Country, err error) {
alpha2, exists := q.CallingCodeToAlpha2[callingCode]
if !exists {
return Country{}, makeError("Could not find country with callingCode", callingCode)
}
return q.Countries[alpha2], nil
}

// FindAllCountries returns a list of all countries
func (q *Query) FindAllCountries() (countries map[string]Country) {
return q.Countries
Expand Down
35 changes: 34 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gountries
import (
"fmt"
"math/rand"
"reflect"
"sort"
"testing"

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

_, err := query.FindSubdivisionCountryByName(subdivisionName)
if err != nil {
assert.Equal(t, "gountries error. Invalid subdivision name: " + subdivisionName, err.Error())
assert.Equal(t, "gountries error. Invalid subdivision name: "+subdivisionName, err.Error())
} else {
t.Fail()
}
}

func TestFindCountryByCallingCode(t *testing.T) {
country, _ := query.FindCountryByName("India")
tests := []struct {
name string
callingCode string
expectedError error
expectedCountry Country
}{
{
name: "Find country by valid calling code should return country when calling code is present for countries",
callingCode: "91",
expectedError: nil,
expectedCountry: country,
},
{
name: "Find country by invalid country code should return error when searching for country",
callingCode: "abc",
expectedError: fmt.Errorf("gountries error. Could not find country with callingCode: abc"),
expectedCountry: Country{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actualCountry, actualError := query.FindCountryByCallingCode(test.callingCode)
if !(reflect.DeepEqual(test.expectedCountry, actualCountry) &&
reflect.DeepEqual(test.expectedError, actualError)) {
t.Fail()
}
})
}
}
11 changes: 11 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func NewFromPath(dataPath string) *Query {
queryInstance.NameToAlpha2 = populateNameIndex(queryInstance.Countries)
queryInstance.Alpha3ToAlpha2 = populateAlphaIndex(queryInstance.Countries)
queryInstance.NativeNameToAlpha2 = populateNativeNameIndex(queryInstance.Countries)
queryInstance.CallingCodeToAlpha2 = populateCallingCodeIndex(queryInstance.Countries)

subdivisions := populateSubdivisions(dataPath)
for k := range queryInstance.Countries {
Expand Down Expand Up @@ -209,3 +210,13 @@ func populateNativeNameIndex(countries map[string]Country) map[string]string {
}
return index
}

func populateCallingCodeIndex(countries map[string]Country) map[string]string {
index := make(map[string]string)
for alpha2, country := range countries {
for _, callingCode := range country.CallingCodes {
index[callingCode] = alpha2
}
}
return index
}

0 comments on commit 2fa96f5

Please sign in to comment.