Skip to content

Commit

Permalink
fix the fileread - instead of relative path, take current go file pat…
Browse files Browse the repository at this point in the history
…h and read the file
  • Loading branch information
ShankarParimi committed Apr 29, 2024
1 parent 632803c commit 09569a8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
11 changes: 9 additions & 2 deletions packages/i18nify-go/modules/country_metadata/country_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package country_subdivisions

import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
)

Expand Down Expand Up @@ -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")
Expand All @@ -56,7 +64,7 @@ func TestGetCountrySubdivisions(t *testing.T) {
readFileFunc = os.ReadFile
}()

_, err = readFileFunc(DataFile)
_, err = readFileFunc(fileName)
if err != nil {
return
}
Expand Down
11 changes: 9 additions & 2 deletions packages/i18nify-go/modules/currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions packages/i18nify-go/modules/phonenumber/phonenumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{}
Expand Down

0 comments on commit 09569a8

Please sign in to comment.