2021-08-31 18:49:08 +02:00
|
|
|
//go:build ignore
|
2019-12-20 11:30:58 +01:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// This generates countries.go by running "go generate"
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Country struct {
|
|
|
|
Code string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
var countries []Country
|
|
|
|
|
|
|
|
func main() {
|
2021-10-06 07:10:50 +02:00
|
|
|
rawData, err := os.ReadFile("./countries.json")
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(rawData, &countries)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create("countries.go")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
for i, v := range countries {
|
|
|
|
countries[i].Code = strings.ToLower(v.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
packageTemplate.Execute(f, struct {
|
|
|
|
Countries []Country
|
|
|
|
}{
|
|
|
|
Countries: countries,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
|
|
|
|
package maps
|
|
|
|
|
2019-12-20 20:23:16 +01:00
|
|
|
var CountryNames = map[string]string{
|
2019-12-20 11:30:58 +01:00
|
|
|
{{- range .Countries }}
|
|
|
|
{{ printf "%q" .Code }}: {{ printf "%q" .Name }},
|
|
|
|
{{- end }}
|
|
|
|
}`))
|