Skip to content

Commit

Permalink
Currency data is now loaded from JSON string defined inside code.
Browse files Browse the repository at this point in the history
This eliminates the need for external configuration file to be maintained for the use of the module.
  • Loading branch information
subwiz committed Dec 9, 2024
1 parent 530e2e2 commit eca87f1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 47 deletions.
37 changes: 0 additions & 37 deletions currency.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package moneylib

import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
)

type currTmp struct {
Expand Down Expand Up @@ -52,36 +48,3 @@ func newCurrency(c *currTmp) Currency {
o := Currency{code: c.Code, number: c.Number, symbol: c.Symbol, nod: c.NOD}
return o
}

var currMap = make(map[string]Currency)

func init() {
confFile := "conf/currency.json"
b, err := os.ReadFile(confFile)
if err != nil {
log.Fatalf("Cannot load config: %v.", confFile)
}
currencies := make([]currTmp, 0)
err = json.Unmarshal(b, &currencies)
if err != nil {
log.Fatalf("Error unmarshaling: %v.\n%v\n", confFile, err)
}
for _, v := range currencies {
currMap[v.Code] = newCurrency(&v)
}
}

// GetCurrency returns the currency object for the code.
func GetCurrency(code string) (Currency, error) {
curr := currMap[code]
if curr.code != code {
return curr, errors.New("Currency code not available: " + code)
}
return currMap[code], nil
}

// ValidCurrency checks if the currency code is correct
func ValidCurrency(code string) bool {
_, err := GetCurrency(code)
return err == nil
}
41 changes: 39 additions & 2 deletions conf/currency.json → init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
package moneylib

import (
"encoding/json"
"errors"
"log"
)

var currencyJSON string = `[
{"code": "AED", "sym": "د.إ", "number": 784, "nod": 2},
{"code": "AFN", "sym": "؋", "number": 971, "nod": 2},
{"code": "ALL", "sym": "L", "number": 8, "nod": 2},
Expand Down Expand Up @@ -137,7 +145,7 @@
{"code": "SYP", "sym": "£S", "number": 760, "nod": 2},
{"code": "SZL", "sym": "L", "number": 748, "nod": 2},
{"code": "THB", "sym": "฿", "number": 764, "nod": 2},
{"code": "TJS", "sym": "ЅМ", "number": 972, "nod": 2},
{"code": "TJS", "sym": "SM", "number": 972, "nod": 2},
{"code": "TMT", "sym": "T", "number": 934, "nod": 2},
{"code": "TND", "sym": "د.ت", "number": 788, "nod": 3},
{"code": "TOP", "sym": "T$", "number": 776, "nod": 2},
Expand Down Expand Up @@ -178,3 +186,32 @@
{"code": "ZMW", "sym": "ZK", "number": 967, "nod": 2},
{"code": "ZWL", "sym": "$", "number": 932, "nod": 2}
]
`

var currMap = make(map[string]Currency)

func init() {
currencies := make([]currTmp, 0)
err := json.Unmarshal([]byte(currencyJSON), &currencies)
if err != nil {
log.Fatalf("Error unmarshaling currency data")
}
for _, v := range currencies {
currMap[v.Code] = newCurrency(&v)
}
}

// GetCurrency returns the currency object for the code.
func GetCurrency(code string) (Currency, error) {
curr := currMap[code]
if curr.code != code {
return curr, errors.New("Currency code not available: " + code)
}
return currMap[code], nil
}

// ValidCurrency checks if the currency code is correct
func ValidCurrency(code string) bool {
_, err := GetCurrency(code)
return err == nil
}
8 changes: 0 additions & 8 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ func (m *Money) Human(lan language.Tag) string {
return p.Sprintf("%s"+format, m.curr.symbol, value)
}

// StringHuman returns money representation for humans.
// func (m *Money) StringHuman() string {
// curr := m.Currency()
// f, _ := m.Float().Float64()
// o := money.Format(f, money.Options{"currency": curr.Code()})
// return o
// }

func pow10(x uint) uint {
if x == 0 {
return 0
Expand Down

0 comments on commit eca87f1

Please sign in to comment.