Skip to content

Commit

Permalink
fixed naming conventions, updated data set
Browse files Browse the repository at this point in the history
  • Loading branch information
pariz committed Jul 30, 2017
1 parent dbe7aab commit b4ebec2
Show file tree
Hide file tree
Showing 7 changed files with 1,156 additions and 1,142 deletions.
2,256 changes: 1,128 additions & 1,128 deletions bindata.go

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions country.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gountries

import "strings"
import (
"strings"
)

// Country contains all countries and their country codes
type Country struct {
Expand Down Expand Up @@ -36,7 +38,7 @@ func (c Country) MeasurableCoordinates() (lat, long float64) {

}

// BorderingCountries gets the bordering countries for this country
// BorderingCountries returns the bordering countries the given Country
func (c *Country) BorderingCountries() (countries []Country) {

query := New()
Expand All @@ -53,6 +55,7 @@ func (c *Country) BorderingCountries() (countries []Country) {

}

// SubDivisions returns the subdivisions for the given Country
func (c *Country) SubDivisions() (subdivisions []SubDivision) {

query := New()
Expand Down
10 changes: 10 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gountries

import (
"fmt"
)

// Error returns a formatted error
func makeError(errMsg, errType string) error {
return fmt.Errorf("gountries error. %s: %s", errMsg, errType)
}
10 changes: 5 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gountries

import (
"fmt"
"strings"
)

Expand All @@ -22,7 +21,7 @@ func (q *Query) FindCountryByName(name string) (result Country, err error) {
lowerName := strings.ToLower(name)
alpha2, exists := q.NameToAlpha2[lowerName]
if !exists {
return Country{}, fmt.Errorf("Could not find country with name %s", name)
return Country{}, makeError("Could not find country with name", name)
}
return q.Countries[alpha2], nil
}
Expand All @@ -34,20 +33,21 @@ func (q *Query) FindCountryByAlpha(code string) (result Country, err error) {
case len(code) == 2:
country, exists := q.Countries[codeU]
if !exists {
return Country{}, fmt.Errorf("Could not find country with code %s", code)
return Country{}, makeError("Could not find country with code %s", code)
}
return country, nil
case len(code) == 3:
alpha2, exists := q.Alpha3ToAlpha2[codeU]
if !exists {
return Country{}, fmt.Errorf("Could not find country with code %s", code)
return Country{}, makeError("Could not find country with code", code)
}
return q.Countries[alpha2], nil
default:
return Country{}, fmt.Errorf("%s is an invalid code format", code)
return Country{}, makeError("Invalid code format", code)
}
}

// FindCountries finds a Country based on the given struct data
func (q Query) FindCountries(c Country) (countries []Country) {

for _, country := range q.Countries {
Expand Down
6 changes: 3 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestFindCountryByAlpha(t *testing.T) {
result, err = query.FindCountryByAlpha("SEE")

if err != nil {
assert.EqualError(t, err, "Could not find country with code SEE")
assert.EqualError(t, err, "gountries error. Could not find country with code: SEE")

} else {
t.Fail()
Expand All @@ -129,7 +129,7 @@ func TestFindCountryByAlpha(t *testing.T) {
result, err = query.FindCountryByAlpha("SEEE")

if err != nil {
assert.EqualError(t, err, "SEEE is an invalid code format")
assert.EqualError(t, err, "gountries error. Invalid code format: SEEE")

} else {
t.Fail()
Expand All @@ -141,7 +141,7 @@ func TestFindCountryByAlpha(t *testing.T) {
result, err = query.FindCountryByAlpha("S")

if err != nil {
assert.EqualError(t, err, "S is an invalid code format")
assert.EqualError(t, err, "gountries error. Invalid code format: S")

} else {
t.Fail()
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fmt.Println(distance)
The data in the `data/yaml` subdirectory is embedded using go-bindata. Once you include this library in your project, you won't need to access the data directory. To add or update the data, make changes to the YAML files then run:

```
go-bindata data/yaml/*
go-bindata -pkg gountries data/yaml/*
```

# Testing
Expand Down
7 changes: 4 additions & 3 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ func populateCountries(dataPath string) map[string]Country {

if info, err := ioutil.ReadDir(countriesPath); err == nil {

var file []byte

for _, v := range info {

if !v.IsDir() {

if file, err := ioutil.ReadFile(filepath.Join(countriesPath, v.Name())); err == nil {
if file, err = ioutil.ReadFile(filepath.Join(countriesPath, v.Name())); err == nil {

country := Country{}
if err := yaml.Unmarshal(file, &country); err == nil {
if err = yaml.Unmarshal(file, &country); err == nil {

// Save
countries[country.Codes.Alpha2] = country
Expand Down Expand Up @@ -131,7 +133,6 @@ func populateSubdivisions(dataPath string) (list map[string][]SubDivision) {
if info, err := ioutil.ReadDir(subdivisionsPath); err == nil {

for _, v := range info {
//fmt.Println(v.IsDir())

if !v.IsDir() {

Expand Down

0 comments on commit b4ebec2

Please sign in to comment.