-
Notifications
You must be signed in to change notification settings - Fork 5
/
country.go
285 lines (220 loc) · 6.72 KB
/
country.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Package country provides lookups methods over ISO-3166 countries.
Constants have been generated with code generation(github.com/mikekonan/go-countries@generator).
The source of truth is https://www.iso.org/iso-3166-country-codes.html.
Lookups methods by alpha2 and alpha3 are case robust.
Author Mikalai Konan(mikalai.konan@icloud.com).
*/
package country
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
)
//Name represents country name
type Name string
//UnmarshalJSON unmarshall implementation for name
func (name *Name) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
enumValue := Name(str)
if _, err := ByNameErr(enumValue); err != nil {
return err
}
*name = enumValue
return nil
}
//Value implementation of driver.Valuer
func (name Name) Value() (value driver.Value, err error) {
if name == "" {
return "", nil
}
if _, err = ByNameErr(name); err != nil {
return nil, err
}
return name.String(), nil
}
//Validate implementation of ozzo-validation Validate interface
func (name Name) Validate(_ interface{}) (err error) {
_, err = ByNameErr(name)
return
}
//IsSet indicates if Name is set
func (name Name) IsSet() bool {
return len(string(name)) > 0
}
//String implementation of Stringer interface
func (name Name) String() string {
return string(name)
}
//Alpha2Code represents alpha-2 code
type Alpha2Code string
//UnmarshalJSON unmarshall implementation for alpha2code
func (code *Alpha2Code) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
enumValue := Alpha2Code(str)
if _, err := ByAlpha2CodeErr(enumValue); err != nil {
return err
}
*code = enumValue
return nil
}
//Value implementation of driver.Valuer
func (code Alpha2Code) Value() (value driver.Value, err error) {
if code == "" {
return "", nil
}
var country Country
if country, err = ByAlpha2CodeErr(code); err != nil {
return nil, err
}
return country.Alpha2Code().String(), nil
}
//Validate implementation of ozzo-validation Validate interface
func (code Alpha2Code) Validate(_ interface{}) (err error) {
_, err = ByAlpha2CodeErr(code)
return
}
//IsSet indicates if Name is set
func (code Alpha2Code) IsSet() bool {
return len(string(code)) > 0
}
//String implementation of Stringer interface
func (code Alpha2Code) String() string {
return string(code)
}
func (code Alpha2Code) toUpper() Alpha2Code {
return Alpha2Code(strings.ToUpper(code.String()))
}
//Alpha3Code represents alpha-3 code
type Alpha3Code string
//UnmarshalJSON unmarshall implementation for alpha3code
func (code *Alpha3Code) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
enumValue := Alpha3Code(str)
if _, err := ByAlpha3CodeErr(enumValue); err != nil {
return err
}
*code = enumValue
return nil
}
//Value implementation of driver.Valuer
func (code Alpha3Code) Value() (value driver.Value, err error) {
if code == "" {
return "", nil
}
var country Country
if country, err = ByAlpha3CodeErr(code); err != nil {
return nil, err
}
return country.Alpha3Code().String(), nil
}
//Validate implementation of ozzo-validation Validate interface
func (code Alpha3Code) Validate(_ interface{}) (err error) {
_, err = ByAlpha3CodeErr(code)
return
}
//IsSet indicates if Name is set
func (code Alpha3Code) IsSet() bool {
return len(string(code)) > 0
}
//String implementation of Stringer interface
func (code Alpha3Code) String() string {
return string(code)
}
func (code Alpha3Code) toUpper() Alpha3Code {
return Alpha3Code(strings.ToUpper(code.String()))
}
//Country represents country entity according to ISO-3166.
type Country struct {
name Name
alpha2 Alpha2Code
alpha3 Alpha3Code
}
//Name returns country name
func (country Country) Name() Name { return country.name }
//Alpha2Code returns alpha-2 code
func (country Country) Alpha2Code() Alpha2Code { return country.alpha2 }
//Alpha3Code returns alpha-2 code
func (country Country) Alpha3Code() Alpha3Code { return country.alpha3 }
//NameStr returns country name string
func (country Country) NameStr() string { return country.name.String() }
//Alpha2CodeStr returns country alpha-2 code string
func (country Country) Alpha2CodeStr() string { return country.alpha2.String() }
//Alpha3CodeStr returns country alpha-2 code string
func (country Country) Alpha3CodeStr() string { return country.alpha3.String() }
//ByAlpha3Code lookup for country by alpha-3 code
func ByAlpha3Code(code Alpha3Code) (result Country, ok bool) {
result, ok = countryByAlpha3[code.toUpper()]
return
}
//ByAlpha3CodeStr lookup for country by alpha-3 code string
func ByAlpha3CodeStr(code string) (result Country, ok bool) {
return ByAlpha3Code(Alpha3Code(code))
}
//ByAlpha3CodeErr lookup for country by alpha-3 code with error return type
func ByAlpha3CodeErr(code Alpha3Code) (result Country, err error) {
var ok bool
result, ok = ByAlpha3Code(code)
if !ok {
err = fmt.Errorf("'%s' is not valid ISO-3166-alpha3 code", code)
}
return
}
//ByAlpha3CodeStrErr lookup for country by alpha-3 code string with error return type
func ByAlpha3CodeStrErr(code string) (result Country, err error) {
return ByAlpha3CodeErr(Alpha3Code(code))
}
//ByAlpha2Code lookup for country by alpha-2 code
func ByAlpha2Code(code Alpha2Code) (result Country, ok bool) {
result, ok = countryByAlpha2[code.toUpper()]
return
}
//ByAlpha2CodeStr lookup for country by alpha-2 code string
func ByAlpha2CodeStr(code string) (result Country, ok bool) {
return ByAlpha2Code(Alpha2Code(code))
}
//ByAlpha2CodeErr lookup for country by alpha-2 code with error return type
func ByAlpha2CodeErr(code Alpha2Code) (result Country, err error) {
var ok bool
result, ok = ByAlpha2Code(code)
if !ok {
err = fmt.Errorf("'%s' is not valid ISO-3166-alpha2 code", code)
}
return
}
//ByAlpha2CodeStrErr lookup for country by alpha-2 code string with error return type
func ByAlpha2CodeStrErr(code string) (result Country, err error) {
return ByAlpha2CodeErr(Alpha2Code(code))
}
//ByName lookup for country by name
func ByName(country Name) (result Country, ok bool) {
result, ok = countryByName[country]
return
}
//ByNameStr lookup for country by name string
func ByNameStr(country string) (result Country, ok bool) {
return ByName(Name(country))
}
//ByNameErr lookup for country by name with error return type
func ByNameErr(country Name) (result Country, err error) {
var ok bool
result, ok = ByName(country)
if !ok {
err = fmt.Errorf("'%s' is not valid ISO-3166 Country name", country)
}
return
}
//ByNameStrErr lookup for country by name string with error return type
func ByNameStrErr(country string) (result Country, err error) {
return ByNameErr(Name(country))
}