forked from pirsquare/country-mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountry_mapper.go
176 lines (152 loc) · 3.9 KB
/
country_mapper.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
package country_mapper
import (
"encoding/csv"
"log"
"os"
"strings"
)
type CountryInfoClient struct {
Data []*CountryInfo
}
func (c *CountryInfoClient) MapByName(name string) *CountryInfo {
for _, row := range c.Data {
// check Name field
if strings.ToLower(row.Name) == strings.ToLower(name) {
return row
}
// check AlternateNames field
if stringInSlice(strings.ToLower(name), row.AlternateNamesLower()) {
return row
}
}
return nil
}
func (c *CountryInfoClient) MapByAlpha2(alpha2 string) *CountryInfo {
for _, row := range c.Data {
if strings.ToLower(row.Alpha2) == strings.ToLower(alpha2) {
return row
}
}
return nil
}
func (c *CountryInfoClient) MapByAlpha3(alpha3 string) *CountryInfo {
for _, row := range c.Data {
if strings.ToLower(row.Alpha3) == strings.ToLower(alpha3) {
return row
}
}
return nil
}
func (c *CountryInfoClient) MapByCurrency(currency string) []*CountryInfo {
rowList := []*CountryInfo{}
for _, row := range c.Data {
if stringInSlice(strings.ToLower(currency), row.CurrencyLower()) {
rowList = append(rowList, row)
}
}
return rowList
}
func (c *CountryInfoClient) MapByCallingCode(callingCode string) []*CountryInfo {
rowList := []*CountryInfo{}
for _, row := range c.Data {
if stringInSlice(strings.ToLower(callingCode), row.CallingCodeLower()) {
rowList = append(rowList, row)
}
}
return rowList
}
func (c *CountryInfoClient) MapByRegion(region string) []*CountryInfo {
rowList := []*CountryInfo{}
for _, row := range c.Data {
if strings.ToLower(row.Region) == strings.ToLower(region) {
rowList = append(rowList, row)
}
}
return rowList
}
func (c *CountryInfoClient) MapBySubregion(subregion string) []*CountryInfo {
rowList := []*CountryInfo{}
for _, row := range c.Data {
if strings.ToLower(row.Subregion) == strings.ToLower(subregion) {
rowList = append(rowList, row)
}
}
return rowList
}
type CountryInfo struct {
Name string
AlternateNames []string
Alpha2 string
Alpha3 string
Capital string
Currency []string
CallingCode []string
Region string
Subregion string
}
func (c *CountryInfo) AlternateNamesLower() []string {
updated := []string{}
for _, alternateName := range c.AlternateNames {
updated = append(updated, strings.ToLower(alternateName))
}
return updated
}
func (c *CountryInfo) CurrencyLower() []string {
updated := []string{}
for _, currency := range c.Currency {
updated = append(updated, strings.ToLower(currency))
}
return updated
}
func (c *CountryInfo) CallingCodeLower() []string {
updated := []string{}
for _, callingCode := range c.CallingCode {
updated = append(updated, strings.ToLower(callingCode))
}
return updated
}
func Load(loadFile string) (*CountryInfoClient, error) {
csvfile, err := os.Open(loadFile)
if err != nil {
log.Fatalln("Couldn't open the csv file", err)
}
reader := csv.NewReader(csvfile)
reader.Comma = ';'
data, err := reader.ReadAll()
if err != nil {
log.Fatalln("Couldn't open the csv file : err 2", err)
}
recordList := []*CountryInfo{}
for idx, row := range data {
// skip header
if idx == 0 {
continue
}
// get name
name := strings.Split(row[0], ",")[:1][0]
// use commonly used & altSpellings names as AlternateNames
alternateNames := strings.Split(row[0], ",")[1:]
alternateNames = append(alternateNames, strings.Split(row[8], ",")...)
record := &CountryInfo{
Name: name,
AlternateNames: alternateNames,
Alpha2: row[2],
Alpha3: row[4],
Capital: row[7],
Currency: strings.Split(row[5], ","),
CallingCode: strings.Split(row[6], ","),
Region: row[10],
Subregion: row[11],
}
recordList = append(recordList, record)
}
return &CountryInfoClient{Data: recordList}, nil
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}