-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiso3166_gen.go
132 lines (117 loc) · 3.9 KB
/
iso3166_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
// 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 iso3166.go.
//
// This file grabs the ISO 3166-1-alpha2 codes and writes them
// into source code so we don't rely on any external files (zip,
// json, etc).
package main
import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"log"
"net/http"
"os"
"os/user"
"runtime"
"slices"
"strings"
"time"
)
var (
// From https://datahub.io/core/country-list#data
downloadUrl = "https://raw.githubusercontent.com/fannarsh/country-list/master/data.json"
)
// [{"Code": "AF", "Name": "Afghanistan"}, ...]
type country struct {
Code string `json:"Code"`
Name string `json:"Name"`
}
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 iso3166
`, 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 countries []country
if err := json.NewDecoder(resp.Body).Decode(&countries); err != nil {
log.Fatalf("error while parsing country response: %v", err)
}
slices.SortFunc(countries, func(a, b country) int {
if a.Name < b.Name {
return -1
}
return 1
})
// Write countries to source code
fmt.Fprintln(&buf, "var countryCodes = map[string][]string{")
for i := range countries {
countryCode := countries[i].Code
// Add some extra names to certain countries
countryNames := []string{countries[i].Name}
switch countryNames[0] {
case "United Kingdom of Great Britain and Northern Ireland":
countryNames = append(countryNames, "United Kingdom", "England", "Great Britain")
case "United States of America":
countryNames = append(countryNames, "USA", "United States")
}
countryNamesValue := strings.Join(countryNames, `","`)
fmt.Fprintf(&buf, fmt.Sprintf(`"%s": {"%s"},`+"\n", countryCode, countryNamesValue))
}
fmt.Fprintln(&buf, "}")
// 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("data.go", out, 0644)
if err != nil {
log.Fatalf("error writing file, err=%v", err)
}
}