Skip to content

Commit

Permalink
update: cities as map instead of array
Browse files Browse the repository at this point in the history
  • Loading branch information
rzp-Piyush committed Nov 19, 2024
1 parent 27a1605 commit 81df226
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/i18nify-go/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
fmt.Println(state.GetCities()[0]) //{Yellāpur nan Asia/Kolkata [581337 581337 ...}

// Get States by zipcode
// As suggested online there are some cases in which one zipcode can be
madhyaPradesh := countryIN.GetStatesByZipCode("452010")[0]
fmt.Printf("For pincode 452010 state : %s\n", madhyaPradesh) // {[{Wārāseonī nan Asia/Kolkata [481331 ...}
fmt.Printf("State name %s\n", madhyaPradesh.GetName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ func (r *CountrySubdivisions) GetStateByStateCode(code string) (State, 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 {
return city, true
}
}
city, exists := state.Cities[cityName]
return city, exists
}
return City{}, false
}
Expand Down Expand Up @@ -96,22 +93,51 @@ func NewCountrySubdivisions(countryName string, states map[string]State) *Countr

// State contains information about a state or province.
type State struct {
Cities []City `json:"cities"` // Cities contains information about cities within the state.
Name string `json:"name"` // Name represents the name of the state.
Cities map[string]City `json:"cities"` // Cities contains information about cities within the state.
Name string `json:"name"` // Name represents the name of the state.
}

// GetCities returns information about cities within the state.
func (r *State) GetCities() []City {
return r.Cities
cities := make([]City, 0, len(r.Cities))
for _, city := range r.Cities {
cities = append(cities, city)
}
return cities
}

// GetName returns the name of the state.
func (r *State) GetName() string {
return r.Name
}

// UnmarshalJSON Custom unmarshal for States
func (r *State) UnmarshalJSON(data []byte) error {
// Temporary structure to unmarshal cities as a slice
type Temp State
temp := &struct {
Cities []City `json:"cities"`
*Temp
}{
Temp: (*Temp)(r),
}

// Unmarshal into temporary struct
if err := json.Unmarshal(data, &temp); err != nil {
return err
}

// Convert the slice of cities into a map
r.Cities = make(map[string]City)
for _, city := range temp.Cities {
r.Cities[city.Name] = city
}

return nil
}

// NewState creates a new State instance.
func NewState(cities []City, name string) *State {
func NewState(cities map[string]City, name string) *State {
return &State{
Cities: cities,
Name: name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestMarshalCountrySubdivisions(t *testing.T) {
CountryName: "India",
States: map[string]State{
"KA": {
Cities: []City{
{Name: "Bengaluru", RegionName: "nan", Timezone: "Asia/Kolkata", Zipcodes: []string{"560018", "560116", "560500"}},
Cities: map[string]City{
"Bengaluru": {Name: "Bengaluru", RegionName: "nan", Timezone: "Asia/Kolkata", Zipcodes: []string{"560018", "560116", "560500"}},
},
Name: "Karnataka",
},
Expand Down Expand Up @@ -109,16 +109,16 @@ func TestGetCityByCityNameAndStateCode(t *testing.T) {
States: map[string]State{
"KA": {
Name: "Karnataka",
Cities: []City{
{Name: "Bengaluru", Timezone: "Asia/Kolkata", Zipcodes: []string{"560018", "560116"}},
{Name: "Mysore", Timezone: "Asia/Kolkata", Zipcodes: []string{"570001"}},
Cities: map[string]City{
"Bengaluru": {Name: "Bengaluru", Timezone: "Asia/Kolkata", Zipcodes: []string{"560018", "560116"}},
"Mysore": {Name: "Mysore", Timezone: "Asia/Kolkata", Zipcodes: []string{"570001"}},
},
},
"MH": {
Name: "Maharashtra",
Cities: []City{
{Name: "Mumbai", Timezone: "Asia/Kolkata", Zipcodes: []string{"400001"}},
{Name: "Pune", Timezone: "Asia/Kolkata", Zipcodes: []string{"411001"}},
Cities: map[string]City{
"Mumbai": {Name: "Mumbai", Timezone: "Asia/Kolkata", Zipcodes: []string{"400001"}},
"Pune": {Name: "Pune", Timezone: "Asia/Kolkata", Zipcodes: []string{"411001"}},
},
},
},
Expand Down

0 comments on commit 81df226

Please sign in to comment.