From 81df226ea0984895e27a4fbcefe368190589eb0c Mon Sep 17 00:00:00 2001 From: rzp-Piyush Date: Tue, 19 Nov 2024 16:34:08 +0530 Subject: [PATCH] update: cities as map instead of array --- packages/i18nify-go/example/example.go | 1 + .../country_subdivisions.go | 44 +++++++++++++++---- .../country_subdivisions_test.go | 16 +++---- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/packages/i18nify-go/example/example.go b/packages/i18nify-go/example/example.go index ce81882b..d836da18 100644 --- a/packages/i18nify-go/example/example.go +++ b/packages/i18nify-go/example/example.go @@ -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()) diff --git a/packages/i18nify-go/modules/country_subdivisions/country_subdivisions.go b/packages/i18nify-go/modules/country_subdivisions/country_subdivisions.go index da0ede23..4a067533 100644 --- a/packages/i18nify-go/modules/country_subdivisions/country_subdivisions.go +++ b/packages/i18nify-go/modules/country_subdivisions/country_subdivisions.go @@ -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 } @@ -96,13 +93,17 @@ 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. @@ -110,8 +111,33 @@ 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, diff --git a/packages/i18nify-go/modules/country_subdivisions/country_subdivisions_test.go b/packages/i18nify-go/modules/country_subdivisions/country_subdivisions_test.go index e717db0f..02c7f70c 100644 --- a/packages/i18nify-go/modules/country_subdivisions/country_subdivisions_test.go +++ b/packages/i18nify-go/modules/country_subdivisions/country_subdivisions_test.go @@ -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", }, @@ -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"}}, }, }, },