Skip to content

Commit

Permalink
[I18ONCALL-265] Feature support embed for static data files (#174)
Browse files Browse the repository at this point in the history
* add embed support for static files

* add embed changes and move data to data dirs

* fix data dir

* fix relative path for data dir

* revert fmt changes

* fix phonenumber

* add comments
  • Loading branch information
varunkumark1997 authored Oct 9, 2024
1 parent f6e02c2 commit e2e5d3f
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 44 deletions.
16 changes: 6 additions & 10 deletions packages/i18nify-go/modules/country_metadata/country_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
package country_metadata

import (
"embed"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
)

//go:embed data
var metaJsonDir embed.FS

// DataFile defines the path to the JSON data file containing country metadata.
const DataFile = "data.json"
const DataFile = "data/data.json"

// UnmarshalCountryMetadata parses JSON data into a CountryMetadata struct.
func UnmarshalCountryMetadata(data []byte) (CountryMetadata, error) {
Expand Down Expand Up @@ -44,12 +45,7 @@ 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.
_, 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))
metaJsonData, err := metaJsonDir.ReadFile(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 @@ -9,7 +9,7 @@ import (
)

func TestUnmarshalCountryMetadata(t *testing.T) {
jsonData, err := os.ReadFile("data.json")
jsonData, err := os.ReadFile("data/data.json")
countryMetadata, err := UnmarshalCountryMetadata(jsonData)

assert.NoError(t, err, "Unexpected error during unmarshal")
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestMarshalCountryMetadata(t *testing.T) {
var readFileFunc = os.ReadFile

func TestGetMetadataInformation(t *testing.T) {
jsonData, err := os.ReadFile("data.json")
jsonData, err := os.ReadFile("data/data.json")

// Mock implementation of os.ReadFile
readFileFunc = func(filename string) ([]byte, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
package country_subdivisions

import (
"embed"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
)

//go:embed data
var subDivJsonDir embed.FS

// DataFile is the directory where JSON files containing country subdivision data are stored. "

// UnmarshalCountrySubdivisions parses JSON data into a CountrySubdivisions struct.
Expand Down Expand Up @@ -48,12 +50,8 @@ 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.
_, 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"))
completePath := filepath.Join("data/", code+".json")
subDivJsonData, err := subDivJsonDir.ReadFile(completePath)
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 @@ -14,7 +14,7 @@ import (
var testJSONData = []byte(`{"country_name": "India", "states": {"KA": {"name": "Karnataka", "cities": [{"name": "Bengaluru", "timezone": "Asia/Kolkata", "zipcodes": ["560018", "560116", "560500"], "region_name/district_name": "nan"}]}}}`)

func TestUnmarshalCountrySubdivisions(t *testing.T) {
jsonData, err := os.ReadFile("IN.json")
jsonData, err := subDivJsonDir.ReadFile("data/IN.json")
subDivData, err := UnmarshalCountrySubdivisions(jsonData)
assert.NoError(t, err, "Unexpected error during unmarshal")

Expand Down
16 changes: 6 additions & 10 deletions packages/i18nify-go/modules/currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
package currency

import (
"embed"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
)

//go:embed data
var currencyJsonDir embed.FS

// DataFile defines the path to the JSON data file containing currency information.
const DataFile = "data.json"
const DataFile = "data/data.json"

// UnmarshalCurrency parses JSON data into a Currency struct.
func UnmarshalCurrency(data []byte) (Currency, error) {
Expand Down Expand Up @@ -44,12 +45,7 @@ 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.
_, 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))
currencyJsonData, err := currencyJsonDir.ReadFile(DataFile)
if err != nil {
// Handle error reading the file.
fmt.Println("Error reading JSON file:", err)
Expand Down
4 changes: 2 additions & 2 deletions packages/i18nify-go/modules/currency/currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestUnmarshalCurrency(t *testing.T) {
jsonData, err := os.ReadFile("data.json")
jsonData, err := os.ReadFile("data/data.json")
result, err := UnmarshalCurrency(jsonData)

assert.NoError(t, err, "Unexpected error during unmarshal")
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestMarshalCurrency(t *testing.T) {
var readFileFunc = os.ReadFile

func TestGetCurrencyInformation(t *testing.T) {
jsonData, err := os.ReadFile("data.json")
jsonData, err := os.ReadFile("data/data.json")
// Mock implementation of os.ReadFile
readFileFunc = func(filename string) ([]byte, error) {
return jsonData, errors.New("error reading JSON file")
Expand Down
16 changes: 6 additions & 10 deletions packages/i18nify-go/modules/phonenumber/phonenumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
package phonenumber

import (
"embed"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
)

//go:embed data
var teleJsonDir embed.FS

// DataFile defines the path to the JSON data file containing country telephone number information.
const DataFile = "data.json"
const DataFile = "data/data.json"

// UnmarshalPhoneNumber parses JSON data into a PhoneNumber struct.
func UnmarshalPhoneNumber(data []byte) (PhoneNumber, error) {
Expand All @@ -43,12 +44,7 @@ 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.
_, 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))
teleJsonData, err := teleJsonDir.ReadFile(DataFile)
if err != nil {
fmt.Println("Error reading JSON file:", err)
return CountryTeleInformation{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestUnmarshalPhoneNumber(t *testing.T) {
jsonData, err := os.ReadFile("data.json")
jsonData, err := os.ReadFile("data/data.json")
result, err := UnmarshalPhoneNumber(jsonData)

assert.NoError(t, err, "Unexpected error during unmarshal")
Expand Down

0 comments on commit e2e5d3f

Please sign in to comment.