-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
290 lines (260 loc) · 7.07 KB
/
utils.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
286
287
288
289
290
package leblad
import (
"encoding/json"
"os"
)
const (
ZIP_COUNT = 48073
WILAYA_COUNT = 48
)
// openJsonFile opens a local json file with the given name
func openJsonFile(n string) ([]byte, error) {
bytes, err := os.ReadFile(n)
if err != nil {
return nil, err
}
return bytes, nil
}
// unmarshalWilayaListJson unmarshals the given json bytes into a slice of wilayas
func unmarshalWilayaListJson(b []byte) (*[]Wilaya, error) {
var wilayas *[]Wilaya
if err := json.Unmarshal(b, &wilayas); err != nil {
return nil, err
}
return wilayas, nil
}
// filterWilayaList filters the fields of the given slice of wilayas
func filterWilayaList(wilayas *[]Wilaya, fields ...string) *[]Wilaya {
var filtered []Wilaya
for _, w := range *wilayas {
filtered = append(filtered, filterWilaya(w, fields...))
}
return &filtered
}
// filterWilaya filters the fields of the given wilaya
func filterWilaya(w Wilaya, fields ...string) Wilaya {
var f Wilaya
for _, field := range fields {
switch field {
case "matricule":
f.Matricule = w.Matricule
case "name_ar":
f.NameAr = w.NameAr
case "name_ber":
f.NameBer = w.NameBer
case "name_en":
f.NameEn = w.NameEn
case "name":
f.Name = w.Name
case "phoneCodes":
f.PhoneCodes = w.PhoneCodes
case "postalCodes":
f.PostalCodes = w.PostalCodes
case "dairats":
f.Dairats = w.Dairats
case "adjacentWilayas":
f.AdjacentWilayas = w.AdjacentWilayas
}
}
return f
}
// filterDairats filters the fields of the given slice of dairats
func filterDairats(dairats []Daira, fields ...string) []Daira {
var filtered []Daira
for _, d := range dairats {
filtered = append(filtered, filterDaira(d, fields...))
}
return filtered
}
// filterDaira filters the fields of the given daira
func filterDaira(d Daira, fields ...string) Daira {
var f Daira
for _, field := range fields {
switch field {
case "code":
f.Code = d.Code
case "name":
f.Name = d.Name
case "name_ar":
f.NameAr = d.NameAr
case "name_en":
f.NameEn = d.NameEn
case "baladyiats":
f.Baladyiats = d.Baladyiats
}
}
return f
}
// filterBaladyiats filters the fields of the given slice of baladyiats
func filterBaladyiats(baladyiats []Baladyia, fields ...string) []Baladyia {
var filtered []Baladyia
for _, b := range baladyiats {
filtered = append(filtered, filterBaladyia(b, fields...))
}
return filtered
}
// filterBaladyia filters the fields of the given baladyia
func filterBaladyia(b Baladyia, fields ...string) Baladyia {
var f Baladyia
for _, field := range fields {
switch field {
case "code":
f.Code = b.Code
case "name":
f.Name = b.Name
case "name_ar":
f.NameAr = b.NameAr
case "name_en":
f.NameEn = b.NameEn
}
}
return f
}
// getWilayaIndexByPhoneCode returns the index of the wilaya that contains the given phone code
func getWilayaIndexByPhoneCode(wilayas *[]Wilaya, phoneCode int) int {
for i, w := range *wilayas {
for _, c := range w.PhoneCodes {
if c == phoneCode {
return i
}
}
}
return -1
}
// getWilayaIndexByDairaName returns the index of the wilaya that contains the given daira name
func getWilayaIndexByDairaName(wilayas *[]Wilaya, dairaName string) int {
for i, w := range *wilayas {
for _, d := range w.Dairats {
if d.Name == dairaName {
return i
}
}
}
return -1
}
// getWilayaIndexByZipCode returns the index of the wilaya that contains the given zip code
func getWilayaIndexByZipCode(wilayas *[]Wilaya, zipCode int) int {
for i, w := range *wilayas {
for _, pc := range w.PostalCodes {
if pc == zipCode {
return i
}
}
}
return -1
}
// getWilayaIndexByCode returns the index of the wilaya that has the given code
func getWilayaIndexByCode(wilayas *[]Wilaya, wilayaCode int) int {
for i, w := range *wilayas {
if w.Matricule == wilayaCode {
return i
}
}
return -1
}
// getAdjacentWilayas returns the adjacent wilayas of the given wilaya
func getAdjacentWilayas(wilayas *[]Wilaya, wilayaIndex int) []int {
return (*wilayas)[wilayaIndex].AdjacentWilayas
}
// getZipCodes returns the zip codes of the given wilaya
func getZipCodes(wilayas *[]Wilaya, wilayaIndex int) []int {
return (*wilayas)[wilayaIndex].PostalCodes
}
// getDairats returns the dairats of the given wilaya
func getDairats(wilayas *[]Wilaya, wilayaIndex int) []Daira {
return (*wilayas)[wilayaIndex].Dairats
}
// getBaladyiats returns the baladyiats of the given daira
func getBaladyiats(dairats []Daira, dairaIndex int) []Baladyia {
return dairats[dairaIndex].Baladyiats
}
// getDairaIndexByName returns the index of the daira that has the given name
func getDairaIndexByName(dairats []Daira, dairaName string) int {
for i, d := range dairats {
if d.Name == dairaName {
return i
}
}
return -1
}
// getWilayaIndexByDairaCode returns the index of the wilaya that contains the given daira code
func getWilayaIndexByDairaCode(wilayas *[]Wilaya, dairaCode int) int {
for i, w := range *wilayas {
for _, d := range w.Dairats {
if d.Code == dairaCode {
return i
}
}
}
return -1
}
// getDairaIndexByCode returns the index of the daira that has the given code
func getDairaIndexByCode(dairats []Daira, dairaCode int) int {
for i, d := range dairats {
if d.Code == dairaCode {
return i
}
}
return -1
}
// getWilayaIndexByName returns the index of the wilaya that has the given name
func getWilayaIndexByName(wilayas *[]Wilaya, wilayaName string) int {
for i, w := range *wilayas {
if w.Name == wilayaName {
return i
}
}
return -1
}
// getPhoneCodes returns the phone codes of the given wilaya
func getPhoneCodes(wilaya Wilaya) []int {
return wilaya.PhoneCodes
}
// getFirstPhoneCode returns the first phone code of the given wilaya
func getFirstPhoneCode(wilaya Wilaya) int {
return wilaya.PhoneCodes[0]
}
// getBaladyiatsForWilaya returns the baladyiats of the given wilaya
func getBaladyiatsForWilaya(wilaya Wilaya) []Baladyia {
var baladyiats []Baladyia
for _, d := range wilaya.Dairats {
baladyiats = append(baladyiats, d.Baladyiats...)
}
return baladyiats
}
// getWilayaIndexByBaladyiaName returns the index of the wilaya that contains the given baladyia name
func getWilayaIndexByBaladyiaName(wilayas *[]Wilaya, baladyiaName string) int {
for i, w := range *wilayas {
for _, d := range w.Dairats {
for _, b := range d.Baladyiats {
if b.Name == baladyiaName {
return i
}
}
}
}
return -1
}
// getDairaIndexByBaladyiaName returns the index of the daira that contains the given baladyia name
func getDairaIndexByBaladyiaName(dairats []Daira, baladyiaName string) int {
for i, d := range dairats {
for _, b := range d.Baladyiats {
if b.Name == baladyiaName {
return i
}
}
}
return -1
}
// isValidZipCode returns true if the given zip code is valid
func isValidZipCode(zipCode int) bool {
return zipCode >= 1000 && zipCode <= ZIP_COUNT
}
// isValidWilayaCode returns true if the given wilaya code is valid
func isValidWilayaCode(wilayaCode int) bool {
return wilayaCode >= 1 && wilayaCode <= WILAYA_COUNT
}
// isValidPhoneCode returns true if the given phone code is valid
func isValidPhoneCode(phoneCode int) bool {
return phoneCode >= 21 && phoneCode <= 59
}