Skip to content

Commit

Permalink
update : name of functions, add: testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
rzp-Piyush committed Nov 19, 2024
1 parent 9b82081 commit 47e219e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *CountrySubdivisions) GetStateByStateCode(code string) (State, bool) {
return State{}, false
}

func (r *CountrySubdivisions) GetCityByCityNameAndStateCode(cityName string, stateCode string) (City, bool) {
func (r *CountrySubdivisions) GetCityDetailsByCityName(cityName string, stateCode string) (City, bool) {
if state, exists := r.States[stateCode]; exists {
for _, city := range state.Cities {
if city.Name == cityName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,22 @@ func TestGetCityByCityNameAndStateCode(t *testing.T) {
}

// Test: Valid city name and state code
city, exists := data.GetCityByCityNameAndStateCode("Bengaluru", "KA")
city, exists := data.GetCityDetailsByCityName("Bengaluru", "KA")
assert.True(t, exists, "City should exist for valid city name and state code")
assert.Equal(t, "Bengaluru", city.GetName())

// Test: Valid city name but invalid state code
city, exists = data.GetCityByCityNameAndStateCode("Bengaluru", "MH")
city, exists = data.GetCityDetailsByCityName("Bengaluru", "MH")
assert.False(t, exists, "City should not exist for valid city name but invalid state code")
assert.Equal(t, City{}, city, "City should be empty for invalid state code")

// Test: Invalid city name
city, exists = data.GetCityByCityNameAndStateCode("Chennai", "KA")
city, exists = data.GetCityDetailsByCityName("Chennai", "KA")
assert.False(t, exists, "City should not exist for invalid city name")
assert.Equal(t, City{}, city, "City should be empty for invalid city name")

// Test: Invalid state code
city, exists = data.GetCityByCityNameAndStateCode("Mumbai", "TN")
city, exists = data.GetCityDetailsByCityName("Mumbai", "TN")
assert.False(t, exists, "City should not exist for invalid state code")
assert.Equal(t, City{}, city, "City should be empty for invalid state code")
}
70 changes: 35 additions & 35 deletions packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,96 +5,96 @@ import (
"github.com/razorpay/i18nify/packages/i18nify-go/modules/country_subdivisions"
)

type PinCodeDetails struct {
type ZipCodeDetails struct {
Cities []*CityDetails
StateCodes []string
}

type PinCodeData struct {
pinCodeToDetails map[string]*PinCodeDetails
cityToPinCodes map[string][]string
type ZipCodeData struct {
zipCodeToDetails map[string]*ZipCodeDetails
cityToZipCodes map[string][]string
}
type CityDetails struct {
cityName string
stateCode string
}

var zipCodeStore = make(map[string]*PinCodeData)
var zipCodeStore = make(map[string]*ZipCodeData)

func GetCountryZipCodeDetails(code string) *PinCodeData {
if _, exists := zipCodeStore[code]; !exists {
subdivision := country_subdivisions.GetCountrySubdivisions(code)
pinCodeData := initializeZipCodeMap(subdivision)
zipCodeStore[code] = pinCodeData
func GetCountryZipCodeDetails(countryCode string) *ZipCodeData {
if _, exists := zipCodeStore[countryCode]; !exists {
subdivision := country_subdivisions.GetCountrySubdivisions(countryCode)
zipCodeData := initializeZipCodeMap(subdivision)
zipCodeStore[countryCode] = zipCodeData
}
return zipCodeStore[code]
return zipCodeStore[countryCode]
}
func GetStatesFromZipCode(zipCode string, countryCode string) []country_subdivisions.State {
pinCodeData := GetCountryZipCodeDetails(countryCode)
zipCodeData := GetCountryZipCodeDetails(countryCode)
subdivisions := country_subdivisions.GetCountrySubdivisions(countryCode)
var states []country_subdivisions.State
if pinCodeData.pinCodeToDetails[zipCode] == nil {
if zipCodeData.zipCodeToDetails[zipCode] == nil {
return states
}
for _, stateCode := range pinCodeData.pinCodeToDetails[zipCode].StateCodes {
for _, stateCode := range zipCodeData.zipCodeToDetails[zipCode].StateCodes {
if state, exists := subdivisions.GetStateByStateCode(stateCode); exists {
states = append(states, state)
}
}
return states
}
func GetCitiesFromZipCode(zipCode string, countryCode string) []country_subdivisions.City {
pinCodeData := GetCountryZipCodeDetails(countryCode)
zipCodeData := GetCountryZipCodeDetails(countryCode)
subdivision := country_subdivisions.GetCountrySubdivisions(countryCode)
var cities []country_subdivisions.City
if pinCodeData.pinCodeToDetails[zipCode] == nil {
if zipCodeData.zipCodeToDetails[zipCode] == nil {
return cities
}
for _, cityDetails := range pinCodeData.pinCodeToDetails[zipCode].Cities {
if city, exists := subdivision.GetCityByCityNameAndStateCode(cityDetails.cityName, cityDetails.stateCode); exists {
for _, cityDetails := range zipCodeData.zipCodeToDetails[zipCode].Cities {
if city, exists := subdivision.GetCityDetailsByCityName(cityDetails.cityName, cityDetails.stateCode); exists {
cities = append(cities, city)
}
}
return cities
}
func IsValidPinCode(zipCode string, countryCode string) bool {
pinCodeData := GetCountryZipCodeDetails(countryCode)
return pinCodeData.pinCodeToDetails[zipCode] != nil
func IsValidZipCode(zipCode string, countryCode string) bool {
zipCodeData := GetCountryZipCodeDetails(countryCode)
return zipCodeData.zipCodeToDetails[zipCode] != nil
}
func GetPinCodesFromCity(city string, countryCode string) []string {
pinCodeData := GetCountryZipCodeDetails(countryCode)
return pinCodeData.cityToPinCodes[city]
func GetZipCodesFromCity(city string, countryCode string) []string {
zipCodeData := GetCountryZipCodeDetails(countryCode)
return zipCodeData.cityToZipCodes[city]
}

// initializeZipCodeMap builds the zip code maps for the given CountrySubdivisions.
func initializeZipCodeMap(subdivisions country_subdivisions.CountrySubdivisions) *PinCodeData {
func initializeZipCodeMap(subdivisions country_subdivisions.CountrySubdivisions) *ZipCodeData {
var cityToZipCode = make(map[string][]string)
var details = make(map[string]*PinCodeDetails)
var details = make(map[string]*ZipCodeDetails)

// Iterate through all states and cities to populate the zip code maps.
for stateCode, state := range subdivisions.States {
for _, city := range state.Cities {
for _, zipcode := range city.Zipcodes {
// check if an entry with specific PinCode already exists, if not create one
// check if an entry with specific ZipCode already exists, if not create one
if _, exists := details[zipcode]; !exists {
details[zipcode] = &PinCodeDetails{
details[zipcode] = &ZipCodeDetails{
StateCodes: []string{},
Cities: []*CityDetails{},
}
}
pinCodeDetail := details[zipcode]
pinCodeDetail.StateCodes = append(pinCodeDetail.StateCodes, stateCode)
pinCodeDetail.Cities = append(pinCodeDetail.Cities, &CityDetails{
zipCodeDetail := details[zipcode]
zipCodeDetail.StateCodes = append(zipCodeDetail.StateCodes, stateCode)
zipCodeDetail.Cities = append(zipCodeDetail.Cities, &CityDetails{
cityName: city.Name,
stateCode: stateCode,
})
details[zipcode] = pinCodeDetail
details[zipcode] = zipCodeDetail
}
cityToZipCode[city.Name] = city.Zipcodes
}
}
return &PinCodeData{
cityToPinCodes: cityToZipCode,
pinCodeToDetails: details,
return &ZipCodeData{
cityToZipCodes: cityToZipCode,
zipCodeToDetails: details,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func TestGetStatesFromZipCode(t *testing.T) {
states = GetStatesFromZipCode("570001", "IN")
assert.Equal(t, 1, len(states))
assert.Equal(t, "Karnataka", states[0].Name)

// Test case with invalid country code
states = GetStatesFromZipCode("570001", "IND")
assert.Equal(t, 0, len(states), "Invalid country code should not break and return empty array")
}

func TestGetCitiesFromZipCode(t *testing.T) {
Expand Down

0 comments on commit 47e219e

Please sign in to comment.