From eca87f195aee036d4dbc1ffbf2d9f0d570d7fe76 Mon Sep 17 00:00:00 2001 From: Subhash Chandran Date: Tue, 10 Dec 2024 00:18:24 +0530 Subject: [PATCH] Currency data is now loaded from JSON string defined inside code. This eliminates the need for external configuration file to be maintained for the use of the module. --- currency.go | 37 ------------------------------- conf/currency.json => init.go | 41 +++++++++++++++++++++++++++++++++-- money.go | 8 ------- 3 files changed, 39 insertions(+), 47 deletions(-) rename conf/currency.json => init.go (92%) diff --git a/currency.go b/currency.go index f590856..107f16f 100644 --- a/currency.go +++ b/currency.go @@ -1,11 +1,7 @@ package moneylib import ( - "encoding/json" - "errors" "fmt" - "log" - "os" ) type currTmp struct { @@ -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, ¤cies) - 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 -} diff --git a/conf/currency.json b/init.go similarity index 92% rename from conf/currency.json rename to init.go index 548c568..fa9f5f1 100644 --- a/conf/currency.json +++ b/init.go @@ -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}, @@ -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}, @@ -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), ¤cies) + 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 +} diff --git a/money.go b/money.go index 629c03f..83959b9 100644 --- a/money.go +++ b/money.go @@ -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