From 09569a82b25ffba6540efb1d3496711be7f8790d Mon Sep 17 00:00:00 2001 From: "shankar.parimi" Date: Mon, 29 Apr 2024 07:36:07 +0530 Subject: [PATCH] fix the fileread - instead of relative path, take current go file path and read the file --- .../modules/country_metadata/country_metadata.go | 11 +++++++++-- .../country_subdivisions/country_subdivisions.go | 12 +++++++++--- .../country_subdivisions_test.go | 12 ++++++++++-- packages/i18nify-go/modules/currency/currency.go | 11 +++++++++-- .../i18nify-go/modules/phonenumber/phonenumber.go | 11 +++++++++-- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/packages/i18nify-go/modules/country_metadata/country_metadata.go b/packages/i18nify-go/modules/country_metadata/country_metadata.go index 76d988bc..19f7cc3d 100644 --- a/packages/i18nify-go/modules/country_metadata/country_metadata.go +++ b/packages/i18nify-go/modules/country_metadata/country_metadata.go @@ -11,10 +11,12 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" + "runtime" ) // DataFile defines the path to the JSON data file containing country metadata. -const DataFile = "modules/country_metadata/data.json" +const DataFile = "data.json" // UnmarshalCountryMetadata parses JSON data into a CountryMetadata struct. func UnmarshalCountryMetadata(data []byte) (CountryMetadata, error) { @@ -42,7 +44,12 @@ func (r *CountryMetadata) GetAllMetadataInformation() map[string]MetadataInforma // GetMetadataInformation retrieves metadata information for a specific country code. func GetMetadataInformation(code string) MetadataInformation { // Read JSON data file containing country metadata. - metaJsonData, err := os.ReadFile(DataFile) + _, fileName, _, ok := runtime.Caller(0) + if !ok { + fmt.Println("Error getting current file directory") + return MetadataInformation{} + } + metaJsonData, err := os.ReadFile(filepath.Join(filepath.Dir(fileName), DataFile)) if err != nil { // Handle error reading the file. fmt.Printf("Error reading country metadata file: %v", err) diff --git a/packages/i18nify-go/modules/country_subdivisions/country_subdivisions.go b/packages/i18nify-go/modules/country_subdivisions/country_subdivisions.go index be6d83f5..fea30c51 100644 --- a/packages/i18nify-go/modules/country_subdivisions/country_subdivisions.go +++ b/packages/i18nify-go/modules/country_subdivisions/country_subdivisions.go @@ -11,10 +11,11 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" + "runtime" ) -// DataFile is the directory where JSON files containing country subdivision data are stored. -const DataFile = "modules/country_subdivisions/" +// DataFile is the directory where JSON files containing country subdivision data are stored. " // UnmarshalCountrySubdivisions parses JSON data into a CountrySubdivisions struct. func UnmarshalCountrySubdivisions(data []byte) (CountrySubdivisions, error) { @@ -47,7 +48,12 @@ func (r *CountrySubdivisions) GetStates() map[string]State { // GetCountrySubdivisions retrieves subdivision information for a specific country code. func GetCountrySubdivisions(code string) CountrySubdivisions { // Read JSON data file containing country subdivision information. - subDivJsonData, err := os.ReadFile(DataFile + code + ".json") + _, currentFileName, _, ok := runtime.Caller(0) + if !ok { + fmt.Println("Error getting current file directory") + return CountrySubdivisions{} + } + subDivJsonData, err := os.ReadFile(filepath.Join(filepath.Dir(currentFileName), code+".json")) if err != nil { fmt.Println("Error reading JSON file:", err) return CountrySubdivisions{} 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 3ee48b32..f36f41d0 100644 --- a/packages/i18nify-go/modules/country_subdivisions/country_subdivisions_test.go +++ b/packages/i18nify-go/modules/country_subdivisions/country_subdivisions_test.go @@ -2,9 +2,12 @@ package country_subdivisions import ( "errors" + "fmt" "github.com/stretchr/testify/assert" "os" + "path/filepath" "reflect" + "runtime" "testing" ) @@ -45,8 +48,13 @@ func TestMarshalCountrySubdivisions(t *testing.T) { var readFileFunc = os.ReadFile func TestGetCountrySubdivisions(t *testing.T) { - jsonData, err := os.ReadFile("IN.json") + _, currentFileName, _, ok := runtime.Caller(0) + if !ok { + fmt.Println("Error getting current file directory") + } + jsonData, err := os.ReadFile(filepath.Join(filepath.Dir(currentFileName), "IN.json")) + fileName := filepath.Join(filepath.Dir(currentFileName), "IN.json") // Mock implementation of os.ReadFile readFileFunc = func(filename string) ([]byte, error) { return jsonData, errors.New("error reading JSON file") @@ -56,7 +64,7 @@ func TestGetCountrySubdivisions(t *testing.T) { readFileFunc = os.ReadFile }() - _, err = readFileFunc(DataFile) + _, err = readFileFunc(fileName) if err != nil { return } diff --git a/packages/i18nify-go/modules/currency/currency.go b/packages/i18nify-go/modules/currency/currency.go index 8ff14a91..2c97076d 100644 --- a/packages/i18nify-go/modules/currency/currency.go +++ b/packages/i18nify-go/modules/currency/currency.go @@ -11,10 +11,12 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" + "runtime" ) // DataFile defines the path to the JSON data file containing currency information. -const DataFile = "modules/currency/data.json" +const DataFile = "data.json" // UnmarshalCurrency parses JSON data into a Currency struct. func UnmarshalCurrency(data []byte) (Currency, error) { @@ -42,7 +44,12 @@ func (r *Currency) GetAllCurrencyInformation() map[string]CurrencyInformation { // GetCurrencyInformation retrieves currency information for a specific currency code. func GetCurrencyInformation(code string) CurrencyInformation { // Read JSON data file containing currency information. - currencyJsonData, err := os.ReadFile(DataFile) + _, fileName, _, ok := runtime.Caller(0) + if !ok { + fmt.Println("Error getting current file directory") + return CurrencyInformation{} + } + currencyJsonData, err := os.ReadFile(filepath.Join(filepath.Dir(fileName), DataFile)) if err != nil { // Handle error reading the file. fmt.Println("Error reading JSON file:", err) diff --git a/packages/i18nify-go/modules/phonenumber/phonenumber.go b/packages/i18nify-go/modules/phonenumber/phonenumber.go index cdc8aa8e..a472c430 100644 --- a/packages/i18nify-go/modules/phonenumber/phonenumber.go +++ b/packages/i18nify-go/modules/phonenumber/phonenumber.go @@ -11,10 +11,12 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" + "runtime" ) // DataFile defines the path to the JSON data file containing country telephone number information. -const DataFile = "modules/phonenumber/data.json" +const DataFile = "data.json" // UnmarshalPhoneNumber parses JSON data into a PhoneNumber struct. func UnmarshalPhoneNumber(data []byte) (PhoneNumber, error) { @@ -41,7 +43,12 @@ func (r *PhoneNumber) GetAllCountryTeleInformation() map[string]CountryTeleInfor // GetCountryTeleInformation retrieves telephone information for a specific country code. func GetCountryTeleInformation(code string) CountryTeleInformation { // Read JSON data file containing country telephone information. - teleJsonData, err := os.ReadFile(DataFile) + _, currentFileName, _, ok := runtime.Caller(0) + if !ok { + fmt.Println("Error getting current file directory") + return CountryTeleInformation{} + } + teleJsonData, err := os.ReadFile(filepath.Join(filepath.Dir(currentFileName), DataFile)) if err != nil { fmt.Println("Error reading JSON file:", err) return CountryTeleInformation{}