initial commit
This commit is contained in:
parent
7d34e0d6f9
commit
704d2e0330
7 changed files with 1410 additions and 1 deletions
1092
iso/currencies.go
Normal file
1092
iso/currencies.go
Normal file
File diff suppressed because it is too large
Load diff
10
iso/currency.go
Normal file
10
iso/currency.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package iso
|
||||
|
||||
type ISOCurrency struct {
|
||||
Name string
|
||||
Code string
|
||||
Number string
|
||||
MinorUnit string
|
||||
}
|
||||
|
||||
//go:generate go run generate.go
|
||||
98
iso/generate.go
Normal file
98
iso/generate.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
xmlFileURL = "https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml"
|
||||
)
|
||||
|
||||
type iso4217 struct {
|
||||
XMLName xml.Name `xml:"ISO_4217"`
|
||||
Published string `xml:"Pblshd,attr"`
|
||||
CurrencyTable struct {
|
||||
XMLName xml.Name `xml:"CcyTbl"`
|
||||
CurrencyEntries []iso4217CurrencyEntry `xml:"CcyNtry"`
|
||||
} `xml:"CcyTbl"`
|
||||
}
|
||||
|
||||
type iso4217CurrencyEntry struct {
|
||||
XMLName xml.Name `xml:"CcyNtry"`
|
||||
CountryName string `xml:"CtryNm"`
|
||||
CurrencyName string `xml:"CcyNm"`
|
||||
Currency string `xml:"Ccy"`
|
||||
CurrencyNumber string `xml:"CcyNbr"`
|
||||
CurrencyMinorUnits string `xml:"CcyMnrUnts"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
resp, err := resty.New().
|
||||
SetTimeout(time.Second * 10).
|
||||
R().Get(xmlFileURL)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
panic(fmt.Sprintf("response from %q: %d %q", xmlFileURL, resp.StatusCode(), string(resp.Body())))
|
||||
}
|
||||
|
||||
var parsed iso4217
|
||||
|
||||
err = xml.Unmarshal(resp.Body(), &parsed)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
||||
_, _ = fmt.Fprint(buf, "// Code generated with `go generate`. DO NOT EDIT.\n\n")
|
||||
_, _ = fmt.Fprint(buf, "package iso\n\n")
|
||||
_, _ = fmt.Fprint(buf, "var Currencies = map[string]ISOCurrency {\n")
|
||||
|
||||
sean := map[string]struct{}{}
|
||||
for _, entry := range parsed.CurrencyTable.CurrencyEntries {
|
||||
if entry.Currency == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := sean[entry.Currency]; ok {
|
||||
continue
|
||||
}
|
||||
sean[entry.Currency] = struct{}{}
|
||||
|
||||
_, _ = fmt.Fprintf(buf, "\t%q: {\n", entry.Currency)
|
||||
_, _ = fmt.Fprintf(buf, "\t\t%s: %q,\n", "Name", entry.CurrencyName)
|
||||
_, _ = fmt.Fprintf(buf, "\t\t%s: %q,\n", "Code", entry.Currency)
|
||||
_, _ = fmt.Fprintf(buf, "\t\t%s: %q,\n", "Number", entry.CurrencyNumber)
|
||||
_, _ = fmt.Fprintf(buf, "\t\t%s: %q,\n", "MinorUnit", entry.CurrencyMinorUnits)
|
||||
_, _ = fmt.Fprint(buf, "\t},\n")
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprint(buf, "}\n")
|
||||
|
||||
oF, err := os.OpenFile("currencies.go", os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmtCmd := exec.Command("gofmt", "-s")
|
||||
fmtCmd.Stdin = bytes.NewReader(buf.Bytes())
|
||||
fmtCmd.Stdout = oF
|
||||
if err = fmtCmd.Start(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = fmtCmd.Wait(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue