-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountries.go
164 lines (133 loc) · 3.55 KB
/
countries.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
package countries
import (
"strings"
)
const (
alpha2Len = 2
alpha3Len = 3
)
// AllMappings returns the list of all country mappings.
func AllMappings() []Mapping {
return mappings
}
// Exists checks if any occurrence of the query matches.
func Exists(query string) bool {
switch {
case len(query) < alpha2Len:
return false
case len(query) == alpha2Len:
return existsByAlpha2(query)
case len(query) == alpha3Len:
return existsByAlpha3(query)
default:
return existsByNameOrNationality(query)
}
}
// FindCountry looks up any matching occurrence of the query.
func FindCountry(query string) (*Mapping, error) {
switch {
case len(query) < alpha2Len:
return nil, ErrCountryNotFound
case len(query) == alpha2Len:
return findCountryByAlpha2(query)
case len(query) == alpha3Len:
return findCountryByAlpha3(query)
default:
return findCountryByNameOrNationality(query)
}
}
func findCountryByAlpha2(query string) (*Mapping, error) {
query = strings.ToUpper(query)
for i := range mappings {
if mappings[i].Alpha2 == query {
return &mappings[i], nil
}
}
return nil, ErrCountryNotFound
}
func findCountryByAlpha3(query string) (*Mapping, error) {
query = strings.ToUpper(query)
for i := range mappings {
if mappings[i].Alpha3 == query {
return &mappings[i], nil
}
}
return nil, ErrCountryNotFound
}
func findCountryByNameOrNationality(query string) (*Mapping, error) {
for i := range mappings {
if isCountryNameOrNationality(mappings[i].Translations[EN], query) ||
isCountryNameOrNationality(mappings[i].Translations[DE], query) {
return &mappings[i], nil
}
}
return nil, ErrCountryNotFound
}
func existsByAlpha2(query string) bool {
query = strings.ToUpper(query)
for i := range mappings {
if mappings[i].Alpha2 == query {
return true
}
}
return false
}
func existsByAlpha3(query string) bool {
query = strings.ToUpper(query)
for i := range mappings {
if mappings[i].Alpha3 == query {
return true
}
}
return false
}
func existsByNameOrNationality(query string) bool {
for i := range mappings {
if isCountryNameOrNationality(mappings[i].Translations[EN], query) ||
isCountryNameOrNationality(mappings[i].Translations[DE], query) {
return true
}
}
return false
}
func isCountryNameOrNationality(translation Translation, query string) bool {
return strings.EqualFold(translation.Common, query) ||
strings.EqualFold(translation.Official, query) ||
strings.EqualFold(translation.Nationality, query)
}
// Alpha2 looks up any matching occurrence for the query and returns the ISO-3166-1 Alpha-2 code.
func Alpha2(query string) string {
country, err := FindCountry(query)
if err != nil {
return ""
}
return country.Alpha2
}
// Alpha3 looks up any matching occurrence for the query and returns the ISO-3166-1 Alpha-3 code.
func Alpha3(query string) string {
country, err := FindCountry(query)
if err != nil {
return ""
}
return country.Alpha3
}
// CountryName looks up any matching occurrence for the query and returns the official country name in english.
func CountryName(query string) string {
country, err := FindCountry(query)
if err != nil {
return ""
}
return country.Translations[EN].Common
}
// CountryTranslation looks up any matching occurrence for the query and returns the country translation.
func CountryTranslation(query string, lang Language) (*Translation, error) {
country, err := FindCountry(query)
if err != nil {
return nil, ErrCountryNotFound
}
translation, ok := country.Translations[lang]
if !ok {
return nil, ErrTranslationNotFound
}
return &translation, nil
}