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

Added pincode, city and state util functions #144

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
5 changes: 4 additions & 1 deletion packages/i18nify-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/razorpay/i18nify/packages/i18nify-go

go 1.20

require github.com/stretchr/testify v1.9.0
require (
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions packages/i18nify-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 h1:LoYXNGAShUG3m/ehNk4iFctuhGX/+R1ZpfJ4/ia80JM=
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package country_subdivisions

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"io/ioutil"
"net/http"
)

// DataFile is the directory where JSON files containing country subdivision data are stored. "
Expand All @@ -24,15 +24,21 @@ func UnmarshalCountrySubdivisions(data []byte) (CountrySubdivisions, error) {
return r, err
}

type PincodeValue struct {
City string `json:"city"`
State string `json:"state"`
}

// Marshal converts a CountrySubdivisions struct into JSON data.
func (r *CountrySubdivisions) Marshal() ([]byte, error) {
return json.Marshal(r)
}

// CountrySubdivisions contains information about country subdivisions.
type CountrySubdivisions struct {
CountryName string `json:"country_name"` // CountryName represents the name of the country.
States map[string]State `json:"states"` // States contains information about states or provinces within the country.
CountryName string `json:"country_name"` // CountryName represents the name of the country.
States map[string]State `json:"states"` // States contains information about states or provinces within the country.
PincodeDetailsMap map[string]PincodeValue `json:"-"`
}

// GetCountryName returns the name of the country.
Expand All @@ -45,21 +51,89 @@ func (r *CountrySubdivisions) GetStates() map[string]State {
return r.States
}

func (r *CountrySubdivisions) GetCityAndStateForPincode(pincode string) (string, string, error) {
pincodeDetailsMap := r.PincodeDetailsMap
pincodeDetails, ok := pincodeDetailsMap[pincode]

if !ok {
return "", "", errors.New("Pincode not found")
}

return pincodeDetails.City, pincodeDetails.State, nil
}

func (r *CountrySubdivisions) GetAllCities() []string {
var cities []string
states := r.States
for _, state := range states {
for _, city := range state.Cities {
if city.Name != "nan" {
cities = append(cities, city.Name) // TODO : Get rid of this check after cleaning up the data
}
}
}

return cities
}

func (r *CountrySubdivisions) GetAllPostalcodes() []string {
var postalcodes []string
for postalcode, _ := range r.PincodeDetailsMap {
postalcodes = append(postalcodes, postalcode)
}

return postalcodes
}

func (r *CountrySubdivisions) GetAllStates() []string {
var states []string
for _, state := range r.States {
if state.Name != "nan" {
states = append(states, state.Name) // TODO : Get rid of this check after cleaning up the data
}
}
return states
}

// GetCountrySubdivisions retrieves subdivision information for a specific country code.
func GetCountrySubdivisions(code string) CountrySubdivisions {
// Read JSON data file containing country subdivision information.
_, currentFileName, _, ok := runtime.Caller(0)
if !ok {
fmt.Println("Error getting current file directory")
pincodeDetailsMap := make(map[string]PincodeValue)

url := fmt.Sprintf("https://raw.githubusercontent.com/razorpay/i18nify/master/i18nify-data/country/subdivisions/%s.json", code)
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error fetching JSON file:", err)
return CountrySubdivisions{}
}
subDivJsonData, err := os.ReadFile(filepath.Join(filepath.Dir(currentFileName), code+".json"))
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading JSON file:", err)
fmt.Println("Error reading response body:", err)
return CountrySubdivisions{}
}
// Unmarshal JSON data into CountrySubdivisions struct.
allSubDivData, _ := UnmarshalCountrySubdivisions(subDivJsonData)

var allSubDivData CountrySubdivisions
err = json.Unmarshal(body, &allSubDivData)
if err != nil {
fmt.Println("Error unmarshalling JSON data:", err)
return CountrySubdivisions{}
}

if allSubDivData.States != nil {
for _, state := range allSubDivData.States {
for _, city := range state.Cities {
pincodes := city.Zipcodes
for _, pincode := range pincodes {
pincodeDetails := PincodeValue{City: city.Name, State: state.Name}
pincodeDetailsMap[pincode] = pincodeDetails
}
}
}
}

allSubDivData.PincodeDetailsMap = pincodeDetailsMap
return allSubDivData
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,78 @@ func assertIsArray(t *testing.T, value interface{}) {
t.Errorf("Expected an array or slice, but got %T", value)
}
}

func TestCountrySubdivisions_GetCityAndStateForPincode(t *testing.T) {
subDivData := GetCountrySubdivisions("MY")

tests := []struct {
name string
country string
expected []string
err error
}{
{"Valid country", "MY", []string{"Kulim", "Kedah"}, nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
city, state, err := subDivData.GetCityAndStateForPincode("09000")
if err != nil {
assert.Error(t, tt.err)
} else {
assert.NoError(t, nil)
assert.Equal(t, city, tt.expected[0])
assert.Equal(t, state, tt.expected[1])
}
})
}
}

func TestGetAllStates(t *testing.T) {
subDivData := GetCountrySubdivisions("MY")

tests := []struct {
name string
country string
expected []string
err error
}{
{"Valid country", "MY", []string{"Kedah"}, nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := subDivData.GetAllStates()
if tt.err != nil {
assert.Error(t, tt.err)
} else {
assert.NoError(t, nil)
assert.Subset(t, result, tt.expected)
}
})
}
}

func TestGetAllCities(t *testing.T) {
subDivData := GetCountrySubdivisions("MY")
tests := []struct {
name string
country string
expected []string
err error
}{
{"Valid country", "MY", []string{"Penang"}, nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := subDivData.GetAllCities()
if tt.err != nil {
assert.Error(t, tt.err)
} else {
assert.NoError(t, nil)
assert.Subset(t, result, tt.expected)
}
})
}
}
Loading