From bb130eb55152e74430ccf6524505d92fca123e3d Mon Sep 17 00:00:00 2001 From: Benjamin Ross Date: Tue, 10 Sep 2024 13:53:19 -0600 Subject: [PATCH 1/4] add Yuan Fen --- iso4217.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/iso4217.go b/iso4217.go index 3228cb6..8aa0806 100644 --- a/iso4217.go +++ b/iso4217.go @@ -15,7 +15,6 @@ // specific language governing permissions and limitations // under the License. -// Generated on 2023-07-25T04:47:20Z by daniel, any modifications will be overwritten package iso4217 type CurrencyCode struct { @@ -75,6 +74,7 @@ var ( CLP = CurrencyCode{Code: "CLP", NumericCode: "152", Name: "Chilean Peso", DecimalPlaces: 0} CLF = CurrencyCode{Code: "CLF", NumericCode: "990", Name: "Unidad de Fomento", DecimalPlaces: 4} CNY = CurrencyCode{Code: "CNY", NumericCode: "156", Name: "Yuan Renminbi", DecimalPlaces: 2} + CNH = CurrencyCode{Code: "CNH", NumericCode: "157", Name: "Yuan Fen", DecimalPlaces: 2} COP = CurrencyCode{Code: "COP", NumericCode: "170", Name: "Colombian Peso", DecimalPlaces: 2} COU = CurrencyCode{Code: "COU", NumericCode: "970", Name: "Unidad de Valor Real", DecimalPlaces: 2} KMF = CurrencyCode{Code: "KMF", NumericCode: "174", Name: "Comorian Franc", DecimalPlaces: 0} @@ -205,7 +205,6 @@ var ( YER = CurrencyCode{Code: "YER", NumericCode: "886", Name: "Yemeni Rial", DecimalPlaces: 2} ZMW = CurrencyCode{Code: "ZMW", NumericCode: "967", Name: "Zambian Kwacha", DecimalPlaces: 2} ZWL = CurrencyCode{Code: "ZWL", NumericCode: "932", Name: "Zimbabwe Dollar", DecimalPlaces: 2} - ZWG = CurrencyCode{Code: "ZWG", NumericCode: "924", Name: "Zimbabwe Gold", DecimalPlaces: 2} XBA = CurrencyCode{Code: "XBA", NumericCode: "955", Name: "Bond Markets Unit European Composite Unit (EURCO)", DecimalPlaces: 0} XBB = CurrencyCode{Code: "XBB", NumericCode: "956", Name: "Bond Markets Unit European Monetary Unit (E.M.U.-6)", DecimalPlaces: 0} XBC = CurrencyCode{Code: "XBC", NumericCode: "957", Name: "Bond Markets Unit European Unit of Account 9 (E.U.A.-9)", DecimalPlaces: 0} @@ -297,6 +296,8 @@ var lookupTable = map[string]CurrencyCode{ "990": CLF, // Unidad de Fomento "CNY": CNY, // Yuan Renminbi "156": CNY, // Yuan Renminbi + "CNH": CNH, // Yuan Fen + "157": CNH, // Yuan Fen "COP": COP, // Colombian Peso "170": COP, // Colombian Peso "COU": COU, // Unidad de Valor Real @@ -557,8 +558,6 @@ var lookupTable = map[string]CurrencyCode{ "967": ZMW, // Zambian Kwacha "ZWL": ZWL, // Zimbabwe Dollar "932": ZWL, // Zimbabwe Dollar - "ZWG": ZWG, // Zimbabwe Gold - "924": ZWG, // Zimbabwe Gold "XBA": XBA, // Bond Markets Unit European Composite Unit (EURCO) "955": XBA, // Bond Markets Unit European Composite Unit (EURCO) "XBB": XBB, // Bond Markets Unit European Monetary Unit (E.M.U.-6) From 24e1c22580d96cf8e7720150a9ea3f2a92d08b0b Mon Sep 17 00:00:00 2001 From: Benjamin Ross Date: Tue, 10 Sep 2024 13:55:59 -0600 Subject: [PATCH 2/4] need this change --- iso4217.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/iso4217.go b/iso4217.go index 8aa0806..34770c8 100644 --- a/iso4217.go +++ b/iso4217.go @@ -205,6 +205,7 @@ var ( YER = CurrencyCode{Code: "YER", NumericCode: "886", Name: "Yemeni Rial", DecimalPlaces: 2} ZMW = CurrencyCode{Code: "ZMW", NumericCode: "967", Name: "Zambian Kwacha", DecimalPlaces: 2} ZWL = CurrencyCode{Code: "ZWL", NumericCode: "932", Name: "Zimbabwe Dollar", DecimalPlaces: 2} + ZWG = CurrencyCode{Code: "ZWG", NumericCode: "924", Name: "Zimbabwe Gold", DecimalPlaces: 2} XBA = CurrencyCode{Code: "XBA", NumericCode: "955", Name: "Bond Markets Unit European Composite Unit (EURCO)", DecimalPlaces: 0} XBB = CurrencyCode{Code: "XBB", NumericCode: "956", Name: "Bond Markets Unit European Monetary Unit (E.M.U.-6)", DecimalPlaces: 0} XBC = CurrencyCode{Code: "XBC", NumericCode: "957", Name: "Bond Markets Unit European Unit of Account 9 (E.U.A.-9)", DecimalPlaces: 0} @@ -558,6 +559,8 @@ var lookupTable = map[string]CurrencyCode{ "967": ZMW, // Zambian Kwacha "ZWL": ZWL, // Zimbabwe Dollar "932": ZWL, // Zimbabwe Dollar + "ZWG": ZWG, // Zimbabwe Gold + "924": ZWG, // Zimbabwe Gold "XBA": XBA, // Bond Markets Unit European Composite Unit (EURCO) "955": XBA, // Bond Markets Unit European Composite Unit (EURCO) "XBB": XBB, // Bond Markets Unit European Monetary Unit (E.M.U.-6) From e02012f8784e5efba768cd7ee3ab86d6eb19b0f3 Mon Sep 17 00:00:00 2001 From: Benjamin Ross Date: Tue, 10 Sep 2024 14:01:44 -0600 Subject: [PATCH 3/4] remove code generation and update dependencies --- go.mod | 3 +- go.sum | 1 + iso4217_gen.go | 182 ------------------------------------------------- makefile | 4 -- 4 files changed, 3 insertions(+), 187 deletions(-) delete mode 100644 iso4217_gen.go diff --git a/go.mod b/go.mod index 991db11..ed51454 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,10 @@ module github.com/moov-io/iso4217 go 1.20 +require github.com/stretchr/testify v1.8.4 + require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.8.4 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 8cf6655..fa4b6e6 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/iso4217_gen.go b/iso4217_gen.go deleted file mode 100644 index bcd3488..0000000 --- a/iso4217_gen.go +++ /dev/null @@ -1,182 +0,0 @@ -// Licensed to The Moov Authors under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. The Moov Authors licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -//go:build ignore -// +build ignore - -// Generates iso4217.go. -// -// This file grabs the ISO 4217 currency codes and writes them -// into source code so we don't rely on any external files (zip, -// json, etc). -// -// The data is pulled from datahub.io as the ISO.org site only offers -// XML. -// -// https://datahub.io/core/currency-codes -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "go/format" - "log" - "net/http" - "os" - "os/user" - "runtime" - "strconv" - "strings" - "time" -) - -var ( - downloadUrl = "https://datahub.io/core/currency-codes/r/codes-all.json" - outputFilename = "iso4217.go" - - charCleaner = strings.NewReplacer(`"`, ``, `’`, `'`, `’`, `'`, `“`, `"`, `”`, `"`, `\`, ``) -) - -// {"AlphabeticCode": "AFN", "NumericCode": 971.0, "Currency": "Afghani", ... } -type currency struct { - Code string `json:"AlphabeticCode"` - NumericCode float64 `json:"NumericCode"` - Name string `json:"Currency"` - MinorUnit string `json:"MinorUnit"` -} - -func main() { - when := time.Now().Format("2006-01-02T03:04:05Z") - who, err := user.Current() - if err != nil { - log.Fatalf("Unable to get user on %s", runtime.GOOS) - } - - // Write copyright header - var buf bytes.Buffer - fmt.Fprintf(&buf, `// Licensed to The Moov Authors under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. The Moov Authors licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -// Generated on %s by %s, any modifications will be overwritten -package iso4217 - -type CurrencyCode struct { - Code, NumericCode, Name string - - // DecimalPlaces represents the unsigned integer value of a currency's minor unit. - // DecimalPlaces is 0 if the currency doesn't have a minor unit. - DecimalPlaces uint8 -} - -func (cc CurrencyCode) String() string { - return cc.Code -} - -func (cc CurrencyCode) Valid() bool { - _, exists := Lookup(cc.Code) - return exists -} -`, when, who.Username) - - // Download certs - resp, err := http.Get(downloadUrl) - if err != nil { - log.Fatalf("error while downloading %s: %v", downloadUrl, err) - } - defer resp.Body.Close() - - var currencies []currency - if err := json.NewDecoder(resp.Body).Decode(¤cies); err != nil { - log.Fatalf("error while parsing currency response: %v", err) - } - - // The JSON file contains duplicates so we need to dedup them.. - cs := make(map[string]bool, 150) - - // Write countries to source code - var varBuffer bytes.Buffer - fmt.Fprintln(&varBuffer, "var (") - - var lookupBuffer bytes.Buffer - fmt.Fprintln(&lookupBuffer, "var lookupTable = map[string]CurrencyCode{") - - var skipCount int - for i := range currencies { - code, name, minorunit := currencies[i].Code, currencies[i].Name, currencies[i].MinorUnit - if code == "" || name == "" || minorunit == "" { - fmt.Printf("SKIPPING: code=%q currency=%q minorunit=%q\n", code, name, minorunit) - skipCount++ - continue - } - - name = charCleaner.Replace(name) - minorunit = charCleaner.Replace(minorunit) - if _, exists := cs[code]; !exists { - cs[code] = true // mark as seen - - var decimalPlaces uint8 = 0 - if minorunit != "-" { - d, err := strconv.Atoi(minorunit) - if err != nil { - log.Fatalf("error while parsing currency minor unit: %v", err) - } - decimalPlaces = uint8(d) - } - - numericCode := fmt.Sprintf("%03d", int(currencies[i].NumericCode)) - fmt.Fprintf(&varBuffer, fmt.Sprintf(` %s = CurrencyCode{Code: "%s", NumericCode: "%s", Name: "%s", DecimalPlaces: %d}`+"\n", code, code, numericCode, name, decimalPlaces)) - fmt.Fprintf(&lookupBuffer, fmt.Sprintf(`"%s": %s, // %s`+"\n", code, code, name)) - fmt.Fprintf(&lookupBuffer, fmt.Sprintf(`"%s": %s, // %s`+"\n", numericCode, code, name)) - } - } - fmt.Printf("Skipped %d currencies\n", skipCount) - - fmt.Fprintln(&varBuffer, ")") - fmt.Fprintln(&lookupBuffer, "}") - - // Add code to file - buf.Write(varBuffer.Bytes()) - buf.WriteString("\n") - buf.Write(lookupBuffer.Bytes()) - - // format source code and write file - out, err := format.Source(buf.Bytes()) - if err != nil { - fmt.Println(buf.String()) - log.Fatalf("error formatting output code, err=%v", err) - } - - err = os.WriteFile(outputFilename, out, 0644) - if err != nil { - log.Fatalf("error writing file, err=%v", err) - } -} diff --git a/makefile b/makefile index 0e52b3e..f7124bf 100644 --- a/makefile +++ b/makefile @@ -12,10 +12,6 @@ endif clean: @rm -rf ./bin/ ./tmp/ coverage.txt misspell* staticcheck lint-project.sh -.PHONY: generate -generate: clean - @go run iso4217_gen.go - .PHONY: cover-test cover-web cover-test: go test -coverprofile=cover.out ./... From 9633000604abb90eb02825b415523e7423395bf7 Mon Sep 17 00:00:00 2001 From: Benjamin Ross Date: Tue, 10 Sep 2024 14:06:46 -0600 Subject: [PATCH 4/4] update changelog --- CHANGELOG.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16f1302..8f7c187 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ -## v0.3.1 (Released 2023-09-10) +## v0.3.2 (Released 2024-09-10) + +Improvements + +- Add new Yuan Fen currency code (CNH) +- Update dependencies + +## v0.3.1 (Released 2024-09-10) IMPROVEMENTS