-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen.go
188 lines (166 loc) · 4.07 KB
/
gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
//go:build ignore
// +build ignore
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"sort"
"strings"
"text/template"
"time"
)
const dataTemplate = `// Code generated by go generate; DO NOT EDIT.
//go:generate go run gen.go
package address
// CLDRVersion is the CLDR version from which the country list is derived.
const CLDRVersion = "{{ .CLDRVersion }}"
var countries = map[string]string{
{{ export .Countries }}
}
`
func main() {
log.Println("Fetching data...")
cldrVersion, err := fetchVersion()
if err != nil {
log.Fatal(err)
}
countries, err := fetchCountries()
if err != nil {
log.Fatal(err)
}
log.Println("Processing...")
os.Remove("countries.go")
f, err := os.Create("countries.go")
if err != nil {
log.Fatal(err)
}
defer f.Close()
funcMap := template.FuncMap{
"export": export,
}
t, err := template.New("data").Funcs(funcMap).Parse(dataTemplate)
if err != nil {
log.Fatal(err)
}
t.Execute(f, struct {
CLDRVersion string
Countries map[string]string
}{
CLDRVersion: cldrVersion,
Countries: countries,
})
log.Println("Done.")
}
// fetchVersion fetches the CLDR version from GitHub.
func fetchVersion() (string, error) {
data, err := fetchURL("https://raw.githubusercontent.com/unicode-org/cldr-json/main/cldr-json/cldr-localenames-full/package.json")
if err != nil {
return "", fmt.Errorf("fetchVersion: %w", err)
}
aux := struct {
Version string
}{}
if err := json.Unmarshal(data, &aux); err != nil {
return "", fmt.Errorf("fetchVersion: %w", err)
}
return aux.Version, nil
}
// fetchCountries fetches the CLDR country names from GitHub.
//
// The JSON version of CLDR data is used because it is more convenient
// to parse. See https://github.com/unicode-org/cldr-json for details.
func fetchCountries() (map[string]string, error) {
data, err := fetchURL("https://raw.githubusercontent.com/unicode-org/cldr-json/main/cldr-json/cldr-localenames-full/main/en/territories.json")
if err != nil {
return nil, fmt.Errorf("fetchCountries: %w", err)
}
aux := struct {
Main struct {
En struct {
LocaleDisplayNames struct {
Territories map[string]string
}
}
}
Version string
}{}
if err := json.Unmarshal(data, &aux); err != nil {
return nil, fmt.Errorf("fetchCountries: %w", err)
}
countries := aux.Main.En.LocaleDisplayNames.Territories
for countryCode := range countries {
if len(countryCode) > 2 {
delete(countries, countryCode)
}
if contains([]string{"EU", "EZ", "UN", "QO", "XA", "XB", "ZZ"}, countryCode) {
delete(countries, countryCode)
}
}
return countries, nil
}
func fetchURL(url string) ([]byte, error) {
client := http.Client{Timeout: 15 * time.Second}
resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("fetchURL: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetchURL: Get %q: %v", url, resp.Status)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("fetchURL: Get %q: %w", url, err)
}
return data, nil
}
func contains(a []string, x string) bool {
for _, v := range a {
if v == x {
return true
}
}
return false
}
func export(i interface{}) string {
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Map:
return exportMap(v)
default:
return fmt.Sprintf("%#v", i)
}
}
func exportMap(v reflect.Value) string {
var values []string
flipped := make(map[string]string, v.Len())
iter := v.MapRange()
for iter.Next() {
key := iter.Key().String()
value := iter.Value().String()
values = append(values, value)
flipped[value] = key
}
sort.Slice(values, func(i, j int) bool {
// Compare Å as A to avoid having it come after Z.
v1 := strings.Replace(values[i], "Å", "A", 1)
v2 := strings.Replace(values[j], "Å", "A", 1)
return v1 < v2
})
b := strings.Builder{}
for i, value := range values {
key := flipped[value]
fmt.Fprintf(&b, "%q: %#v,", key, value)
if i+1 < len(values) {
fmt.Fprintf(&b, "\n\t")
}
}
return b.String()
}