//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) } }