Skip to content

Commit

Permalink
added getcurrencysymbol, convert_to_major & minor_unit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
isudhanshukrjha committed Nov 28, 2024
1 parent 03b688c commit 36bfc9a
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 9 deletions.
4 changes: 3 additions & 1 deletion packages/i18nify-go/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func main() {
fmt.Println(state.GetCities()[0]) //{Yellāpur nan Asia/Kolkata [581337 581337 ...}

//USD
currencyUS := currency.GetCurrencyInformation("USD")
currencyUS, _ := currency.GetCurrencyInformation("USD")
fmt.Println(currencyUS.Name) //US Dollar
fmt.Println(currencyUS.Symbol) //$

// add convert to major unit and minor unit examples
}
5 changes: 4 additions & 1 deletion packages/i18nify-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/razorpay/i18nify/packages/i18nify-go

go 1.20

require github.com/stretchr/testify v1.9.0
require (
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions packages/i18nify-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
39 changes: 39 additions & 0 deletions packages/i18nify-go/modules/converter/convert_to_major.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package currency provides functionality to handle information about currencies.
package currency

import (
"fmt"
"math"
"strconv"

"github.com/razorpay/i18nify/packages/i18nify-go/modules/currency"
)

// ConvertToMajorUnit converts an amount from a minor currency unit to a major currency unit.
// For example, 100 cents to 1 dollar.
func ConvertToMajorUnit(code string, amount float64) (float64, error) {
// Get currency information for the specified code.
currencyInfo, err := currency.GetCurrencyInformation(code)
if err != nil {
return 0, err
}

// Convert MinorUnit to float64.
minorUnit, err := strconv.ParseFloat(currencyInfo.MinorUnit, 64)
if err != nil {
return 0, fmt.Errorf("invalid minor unit for currency code '%s': %v", code, err)
}

// Calculate the minor unit multiplier to float64
minorUnitMultiplier := math.Pow(10, minorUnit)
if minorUnitMultiplier <= 0 {
minorUnitMultiplier = 100 // Default fallback value.
return 0, fmt.Errorf("invalid minor unit multiplier for currency code '%s'", code)
}
// 100 = Default fallback value.

// Convert the amount from minor to major unit.
majorUnitAmount := amount / minorUnitMultiplier

return majorUnitAmount, nil
}
39 changes: 39 additions & 0 deletions packages/i18nify-go/modules/converter/convert_to_minor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package currency provides functionality to handle information about currencies.
package currency

import (
"fmt"
"math"
"strconv"

"github.com/razorpay/i18nify/packages/i18nify-go/modules/currency"
)

// ConvertToMinorUnit converts an amount from a major currency unit to a minor currency unit.
// For example, 1 dollar to 100 cents.
func ConvertToMinorUnit(code string, amount float64) (float64, error) {
// Get currency information for the specified code.
currencyInfo, err := currency.GetCurrencyInformation(code)
if err != nil {
return 0, err
}

// Convert MinorUnit to float64.
minorUnit, err := strconv.ParseFloat(currencyInfo.MinorUnit, 64)
if err != nil {
return 0, fmt.Errorf("invalid minor unit for currency code '%s': %v", code, err)
}

// Calculate the minor unit multiplier to float64
minorUnitMultiplier := math.Pow(10, minorUnit)
if minorUnitMultiplier <= 0 {
minorUnitMultiplier = 100 // Default fallback value.
return 0, fmt.Errorf("invalid minor unit multiplier for currency code '%s'", code)
}
// 100 = Default fallback value.

// Convert the amount from minor to major unit.
minorUnitAmount := amount * minorUnitMultiplier

return minorUnitAmount, nil
}
46 changes: 40 additions & 6 deletions packages/i18nify-go/modules/currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,34 @@ type Currency struct {
// GetAllCurrencyInformation returns all currency information.
func (r *Currency) GetAllCurrencyInformation() map[string]CurrencyInformation {
return r.CurrencyInformation
// can be stored in cache on first call
// reset cache if there is any change in data.json file
}

// GetCurrencyInformation retrieves currency information for a specific currency code.
func GetCurrencyInformation(code string) CurrencyInformation {
func GetCurrencyInformation(code string) (CurrencyInformation, error) {
// Read JSON data file containing currency information.
currencyJsonData, err := currencyJsonDir.ReadFile(DataFile)
if err != nil {
// Handle error reading the file.
fmt.Println("Error reading JSON file:", err)
return CurrencyInformation{}
// Handle error reading the file
return CurrencyInformation{}, fmt.Errorf("error reading JSON file: %v", err)
}

// Unmarshal JSON data into SupportedCurrency struct.
allCurrencyData, _ := UnmarshalCurrency(currencyJsonData)
allCurrencyData, err := UnmarshalCurrency(currencyJsonData)
if err != nil {
return CurrencyInformation{}, fmt.Errorf("error unmarshalling JSON data: %v", err)
}

// Retrieve currency information for the specified currency code.
return allCurrencyData.CurrencyInformation[code]
currencyInfo, exists := allCurrencyData.CurrencyInformation[code]

if !exists {
return CurrencyInformation{}, fmt.Errorf("currency code '%s' not found", code)
}

return currencyInfo, nil

}

// NewCurrency creates a new Currency instance.
Expand Down Expand Up @@ -85,3 +98,24 @@ func NewCurrencyInformation(minorUnit string, name string, numericCode string, p
Symbol: symbol,
}
}

// GetCurrencySymbol retrieves the currency symbol for a specific currency code.
func GetCurrencySymbol(code string) (string, error) {
// Validate the input code.
if code == "" {
return "", fmt.Errorf("currency code cannot be empty")
}

// Retrieve currency information for the specified code.
currencyInfo, err := GetCurrencyInformation(code)
if err != nil {
return "", fmt.Errorf("failed to retrieve currency information for code '%s': %v", code, err)
}

// Validate the currency symbol.
if currencyInfo.Symbol == "" {
return "", fmt.Errorf("currency symbol for code '%s' is not available", code)
}

return currencyInfo.Symbol, nil
}
2 changes: 1 addition & 1 deletion packages/i18nify-go/modules/currency/currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestGetCurrencyInformation(t *testing.T) {
return
}

result := GetCurrencyInformation("USD")
result, _ := GetCurrencyInformation("USD")

// Use assert.Equal for assertions with inline expected values
assert.Equal(t, "2", result.MinorUnit, "MinorUnit field mismatch")
Expand Down

0 comments on commit 36bfc9a

Please sign in to comment.