forked from pariz/gountries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
211 lines (160 loc) · 4.93 KB
/
setup.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package gountries
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
// New creates an Query object and unmarshals the json file.
func New() *Query {
dataPath := filepath.Join("data", "yaml")
return NewFromPath(dataPath)
}
// NewFromPath creates a Query object from data folder in provided path
func NewFromPath(dataPath string) *Query {
queryInitOnce.Do(func() {
queryInstance = &Query{
Countries: populateCountries(dataPath),
}
queryInstance.NameToAlpha2 = populateNameIndex(queryInstance.Countries)
queryInstance.Alpha3ToAlpha2 = populateAlphaIndex(queryInstance.Countries)
queryInstance.NativeNameToAlpha2 = populateNativeNameIndex(queryInstance.Countries)
subdivisions := populateSubdivisions(dataPath)
for k := range queryInstance.Countries {
c := queryInstance.Countries[k]
c.subdivisions = subdivisions[strings.ToLower(c.Alpha2)]
c.nameToSubdivision = map[string]SubDivision{}
c.codeToSubdivision = map[string]SubDivision{}
for _, s := range c.subdivisions {
for _, n := range s.Names {
c.nameToSubdivision[strings.ToLower(n)] = s
}
c.nameToSubdivision[strings.ToLower(s.Name)] = s
c.codeToSubdivision[strings.ToLower(s.Code)] = s
}
queryInstance.Countries[k] = c
}
})
return queryInstance
}
func populateNameIndex(countries map[string]Country) map[string]string {
index := make(map[string]string)
for alpha2, country := range countries {
name := strings.ToLower(country.Name.Common)
officialName := strings.ToLower(country.Name.Official)
index[name] = alpha2
index[officialName] = alpha2
}
return index
}
func populateAlphaIndex(countries map[string]Country) map[string]string {
index := make(map[string]string)
for alpha2, country := range countries {
index[country.Codes.Alpha3] = alpha2
}
return index
}
func populateCountries(dataPath string) map[string]Country {
// Load countries into memory
//
//
var countries = make(map[string]Country)
countriesPath := path.Join(dataPath, "countries")
// Try packed data first before custom data directory
if yamlFileList, err := AssetDir("data/yaml/countries"); err == nil {
return populateCountriesFromPackedData(yamlFileList, "data/yaml/countries")
}
if info, err := ioutil.ReadDir(countriesPath); err == nil {
var file []byte
for _, v := range info {
if !v.IsDir() {
if file, err = ioutil.ReadFile(filepath.Join(countriesPath, v.Name())); err == nil {
country := Country{}
if err = yaml.Unmarshal(file, &country); err == nil {
// Save
countries[country.Codes.Alpha2] = country
}
}
}
}
} else {
panic(fmt.Errorf("Error loading Countries: %s", err))
}
return countries
}
func populateCountriesFromPackedData(fileList []string, path string) map[string]Country {
var data []byte
var err error
var countries = make(map[string]Country)
for _, yamlFile := range fileList {
var country Country
data, err = Asset(filepath.Join(path, yamlFile))
if err != nil {
continue
}
if err = yaml.Unmarshal(data, &country); err != nil {
continue
}
countries[country.Codes.Alpha2] = country
}
return countries
}
func populateSubdivisions(dataPath string) (list map[string][]SubDivision) {
// Load countries into memory
//
list = map[string][]SubDivision{}
subdivisionsPath := path.Join(dataPath, "subdivisions")
// Try packed data first before custom data directory
if yamlFileList, err := AssetDir("data/yaml/subdivisions"); err == nil {
return populateSubdivisionsFromPackedData(yamlFileList, "data/yaml/subdivisions")
}
if info, err := ioutil.ReadDir(subdivisionsPath); err == nil {
for _, v := range info {
if !v.IsDir() {
if file, err := ioutil.ReadFile(filepath.Join(subdivisionsPath, v.Name())); err == nil {
subdivisions := []SubDivision{}
if err := yaml.Unmarshal(file, &subdivisions); err == nil {
// Save
// subdivisions = append(subdivisions, subdivision...)
list[strings.Split(v.Name(), ".")[0]] = subdivisions
}
}
}
}
} else {
panic("Error loading Subdivisions")
}
return
}
func populateSubdivisionsFromPackedData(fileList []string, path string) map[string][]SubDivision {
var data []byte
var err error
sd := make(map[string][]SubDivision)
for _, yamlFile := range fileList {
data, err = Asset(filepath.Join(path, yamlFile))
if err != nil {
continue
}
var subdivisions []SubDivision
if err = yaml.Unmarshal(data, &subdivisions); err != nil {
continue
}
alpha2 := strings.Split(yamlFile, ".")[0]
sd[alpha2] = subdivisions
}
return sd
}
func populateNativeNameIndex(countries map[string]Country) map[string]string {
index := make(map[string]string)
for alpha2, country := range countries {
for _, nativeNames := range country.Name.Native {
nativeName := strings.ToLower(nativeNames.Common)
officialNativeName := strings.ToLower(nativeNames.Official)
index[nativeName] = alpha2
index[officialNativeName] = alpha2
}
}
return index
}