Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to query countries based on calling code #44

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}