-
Notifications
You must be signed in to change notification settings - Fork 2
/
address.go
665 lines (590 loc) · 14.1 KB
/
address.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package br
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/phenpessoa/gutils/cache"
"github.com/phenpessoa/gutils/unsafex"
)
var (
// ErrInvalidUF is returned when an invalid state (UF) code is passed.
ErrInvalidUF = errors.New("br: invalid uf passed")
// ErrInvalidCEP is returned when an invalid CEP code is passed.
ErrInvalidCEP = errors.New("br: invalid cep passed")
// ErrInvalidSerializedAddress is returned when trying to deserialize an
// invalid string into an Address.
ErrInvalidSerializedAddress = errors.New(
"br: invalid serialized address",
)
)
// UF stands for Unidade Federativa and represents a Brazilian state.
type UF uint8
// NewUF creates a UF instance from a given state code.
func NewUF(codigo int) (UF, error) {
if codigo < 11 || codigo > 53 {
return 0, ErrInvalidUF
}
uf := UF(uint8(codigo))
if uf.String() == "" {
return 0, ErrInvalidUF
}
return uf, nil
}
// NewUFFromStr creates a UF instance from a string representation of the
// state's name or abbreviation.
func NewUFFromStr(uf string) (UF, error) {
if len(uf) < 2 || len(uf) > 19 {
return 0, ErrInvalidUF
}
switch strings.ToLower(uf) {
case "ro", "rondônia", "rondonia":
return UF(11), nil
case "ac", "acre":
return UF(12), nil
case "am", "amazonas":
return UF(13), nil
case "rr", "roraima":
return UF(14), nil
case "pa", "pará", "para":
return UF(15), nil
case "ap", "amapá", "amapa":
return UF(16), nil
case "to", "tocantins":
return UF(17), nil
case "ma", "maranhão", "maranhao":
return UF(21), nil
case "pi", "piauí", "piaui":
return UF(22), nil
case "ce", "ceará", "ceara":
return UF(23), nil
case "rn", "rio grande do norte", "riograndedonorte":
return UF(24), nil
case "pb", "paraíba", "paraiba":
return UF(25), nil
case "pe", "pernambuco":
return UF(26), nil
case "al", "alagoas":
return UF(27), nil
case "se", "sergipe":
return UF(28), nil
case "ba", "bahia":
return UF(29), nil
case "mg", "minas gerais", "minasgerais":
return UF(31), nil
case "es", "espírito santo", "espiritosanto":
return UF(32), nil
case "rj", "rio de janeiro", "riodejaneiro":
return UF(33), nil
case "sp", "são paulo", "sao paulo", "sãopaulo", "saopaulo":
return UF(35), nil
case "pr", "paraná", "parana":
return UF(41), nil
case "sc", "santa catarina", "santacatarina":
return UF(42), nil
case "rs", "rio grande do sul", "riograndedosul":
return UF(43), nil
case "ms", "mato grosso do sul", "matogrossodosul":
return UF(50), nil
case "mt", "mato grosso", "matogrosso":
return UF(51), nil
case "go", "goiás", "goias":
return UF(52), nil
case "df", "distrito federal", "distritofederal":
return UF(53), nil
default:
return 0, ErrInvalidUF
}
}
// String returns the abbreviation of the UF, such as RJ and SP.
func (uf UF) String() string {
switch uf {
case 11:
return "RO"
case 12:
return "AC"
case 13:
return "AM"
case 14:
return "RR"
case 15:
return "PA"
case 16:
return "AP"
case 17:
return "TO"
case 21:
return "MA"
case 22:
return "PI"
case 23:
return "CE"
case 24:
return "RN"
case 25:
return "PB"
case 26:
return "PE"
case 27:
return "AL"
case 28:
return "SE"
case 29:
return "BA"
case 31:
return "MG"
case 32:
return "ES"
case 33:
return "RJ"
case 35:
return "SP"
case 41:
return "PR"
case 42:
return "SC"
case 43:
return "RS"
case 50:
return "MS"
case 51:
return "MT"
case 52:
return "GO"
case 53:
return "DF"
default:
return ""
}
}
// UnidadeFederativa returns the full name of the UF, such as Rio de Janeiro.
func (uf UF) UnidadeFederativa() string {
switch uf {
case 11:
return "Rondônia"
case 12:
return "Acre"
case 13:
return "Amazonas"
case 14:
return "Roraima"
case 15:
return "Pará"
case 16:
return "Amapá"
case 17:
return "Tocantins"
case 21:
return "Maranhão"
case 22:
return "Piauí"
case 23:
return "Ceará"
case 24:
return "Rio Grande do Norte"
case 25:
return "Paraíba"
case 26:
return "Pernambuco"
case 27:
return "Alagoas"
case 28:
return "Sergipe"
case 29:
return "Bahia"
case 31:
return "Minas Gerais"
case 32:
return "Espírito Santo"
case 33:
return "Rio de Janeiro"
case 35:
return "São Paulo"
case 41:
return "Paraná"
case 42:
return "Santa Catarina"
case 43:
return "Rio Grande do Sul"
case 50:
return "Mato Grosso do Sul"
case 51:
return "Mato Grosso"
case 52:
return "Goiás"
case 53:
return "Distrito Federal"
default:
return ""
}
}
// Codigo returns the numeric code of the UF.
func (uf UF) Codigo() int {
return int(uf)
}
// UnmarshalJSON implements the json.Unmarshaler interface for UF.
func (uf *UF) UnmarshalJSON(b []byte) error {
str := unsafex.String(b)
if strings.Contains(str, `"`) {
str = strings.ReplaceAll(str, `"`, "")
_uf, err := NewUFFromStr(str)
if err != nil {
return fmt.Errorf("can not unmarshal %s into uf: %w", str, err)
}
*uf = _uf
return nil
}
parsedCode, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return fmt.Errorf("can not unmarshal %s into uf: %w", str, ErrInvalidUF)
}
_uf, err := NewUF(int(parsedCode))
if err != nil {
return fmt.Errorf("can not unmarshal %d into uf: %w", parsedCode, err)
}
*uf = _uf
return nil
}
// MarshalJSON implements the json.Marshaler interface for UF.
func (uf UF) MarshalJSON() ([]byte, error) {
return []byte(`"` + uf.String() + `"`), nil
}
// Scan implements the sql.Scanner interface for UF.
func (uf *UF) Scan(value any) error {
i64, ok := value.(int64)
if !ok {
return fmt.Errorf("br: unknown type passed to UF Scan: %T", value)
}
_uf, err := NewUF(int(i64))
if err != nil {
return fmt.Errorf("br: can not convert %d to UF", i64)
}
*uf = _uf
return nil
}
// Value implements the driver.Valuer interface for UF.
func (uf UF) Value() (driver.Value, error) {
return int64(uf), nil
}
// CEP represents a Brazilian postal code (Código de Endereçamento Postal).
type CEP string
// NewCEP creates a CEP instance from a string representation.
//
// It verifies the length of the CEP, the digits, and checks the cache for known
// invalid CEPs. It does not make requests to APIs to validate the corresponding
// address if the CEP is not in cache.
//
// To check if this CEP actually represents an Address, call the CEP.ToAddress
// method.
func NewCEP(s string) (CEP, error) {
cep := CEP(s)
if !cep.IsValid() {
return "", ErrInvalidCEP
}
return cep, nil
}
// IsValid checks whether the provided CEP is valid based on its length, digits
// and check the caches for known invalid CEPs.
//
// It does not make requests to APIs to validate the corresponding address.
//
// To check if this CEP actually represents an Address, call the CEP.ToAddress
// method.
func (cep CEP) IsValid() bool {
l := len(cep)
if l != 8 && l != 9 {
return false
}
if l == 9 && cep[5] != '-' {
return false
}
var pad int
for i := 0; i < 8; i++ {
cur := cep[i+pad]
if i == 5 && l == 9 {
pad++
cur = cep[i+pad]
}
if cur < '0' || cur > '9' {
return false
}
}
return !invalidCEPs.Contains(string(cep))
}
// String returns the formatted CEP string as XXXXX-XXX.
func (cep CEP) String() string {
if !cep.IsValid() {
return ""
}
if len(cep) == 9 {
return string(cep)
}
out := make([]byte, 9)
for i := range cep {
switch {
case i < 5:
out[i] = cep[i]
case i == 5:
out[i] = '-'
out[i+1] = cep[i]
default:
out[i+1] = cep[i]
}
}
return unsafex.String(out)
}
func (cep CEP) Value() (driver.Value, error) {
return cep.String(), nil
}
// ToAddress converts a CEP into an Address instance, retrieving address
// information associated with the CEP.
//
// This method may perform requests to external APIs to fetch address details
// based on the CEP.
func (cep CEP) ToAddress() (addr Address, err error) {
if !cep.IsValid() {
return Address{}, ErrInvalidCEP
}
if addr, ok := addresses.Get(cep.String()); ok {
return addr, nil
}
defer func() {
if err != nil && errors.Is(err, ErrInvalidCEP) {
invalidCEPs.Set(cep.String(), unit{})
}
}()
addr, err = buscaCEP(cep)
if err != nil {
if errors.Is(err, ErrInvalidCEP) {
return Address{}, ErrInvalidCEP
}
var err2 error
addr, err2 = viaCEP(cep)
if err2 != nil {
if errors.Is(err2, ErrInvalidCEP) {
return Address{}, ErrInvalidCEP
}
return Address{}, fmt.Errorf(
"failed to validate cep: %w",
errors.Join(err, err2),
)
}
}
addresses.Set(cep.String(), addr)
return addr, nil
}
func buscaCEP(cep CEP) (Address, error) {
type buscaCEPResponse struct {
Erro bool `json:"erro"`
Mensagem string `json:"mensagem"`
Total int `json:"total"`
Dados []Address `json:"dados"`
}
const buscaCEPURL = "https://buscacepinter.correios.com.br/app/endereco/carrega-cep-endereco.php"
form := url.Values{}
form.Set("pagina", "/app/endereco/index.php")
form.Set("endereco", cep.String())
form.Set("tipoCEP", "ALL")
res, err := http.Post(
buscaCEPURL,
"application/x-www-form-urlencoded; charset=UTF-8",
strings.NewReader(form.Encode()),
)
if err != nil {
return Address{}, fmt.Errorf(
"failed to make post request to buscaCEP: %w", err,
)
}
defer res.Body.Close()
data, _ := io.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
return Address{}, fmt.Errorf(
"buscaCEP status code not ok: %d\ndata: %s",
res.StatusCode, string(data),
)
}
var dados buscaCEPResponse
if err := json.Unmarshal(data, &dados); err != nil {
return Address{}, fmt.Errorf(
"failed to unmarshal buscaCEP json: %w\ndata: %s",
err, string(data),
)
}
if dados.Erro {
return Address{}, fmt.Errorf(
"busca cep server returned error: %s", dados.Mensagem,
)
}
if len(dados.Dados) == 0 {
return Address{}, ErrInvalidCEP
}
addr := dados.Dados[0]
logradouro := addr.Logradouro
logradouroParts := strings.Split(logradouro, ",")
if len(logradouroParts) == 1 {
logradouroParts = strings.Split(logradouro, "-")
}
var complemento string
if len(logradouroParts) > 1 {
logradouro = strings.TrimSpace(logradouroParts[0])
complemento = strings.TrimSpace(logradouroParts[1])
}
addr.Logradouro = logradouro
addr.Complemento = complemento
addr.CEP = cep
return addr, nil
}
func viaCEP(cep CEP) (Address, error) {
viaCEPURL := fmt.Sprintf("https://viacep.com.br/ws/%s/json/", cep)
res, err := http.Get(viaCEPURL)
if err != nil {
return Address{}, fmt.Errorf(
"failed to make get request to viaCEP: %w", err,
)
}
defer res.Body.Close()
data, _ := io.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
return Address{}, fmt.Errorf(
"viaCEP status code not ok: %d\ndata: %s",
res.StatusCode, string(data),
)
}
type viaCEPResponse struct {
Address
Erro bool `json:"erro"`
}
var dados viaCEPResponse
if err := json.Unmarshal(data, &dados); err != nil {
return Address{}, fmt.Errorf(
"failed to unmarshal viaCEP json: %w\ndata: %s", err, string(data),
)
}
if dados.Erro || dados.Address.CEP == "" {
return Address{}, ErrInvalidCEP
}
return dados.Address, nil
}
type unit struct{}
var (
addresses *cache.Cache[string, Address]
invalidCEPs *cache.Cache[string, unit]
)
const (
cacheAddressesAndCEPsFor = 2 * time.Hour
)
func init() {
addresses = cache.New[string, Address](cacheAddressesAndCEPsFor)
invalidCEPs = cache.New[string, unit](cacheAddressesAndCEPsFor)
}
// Address represents an address associated with a Brazilian CEP.
type Address struct {
UF UF `json:"uf"`
CEP CEP `json:"cep"`
Localidade string `json:"localidade"`
Logradouro string `json:"logradouroDNEC"`
Complemento string `json:"complemento"`
Bairro string `json:"bairro"`
NomeUnidade string `json:"nomeUnidade"`
}
func (addr Address) serializedSize() int {
return 2 + 5 + // uf
len(addr.Localidade) + 5 +
len(addr.Logradouro) + 5 +
len(addr.Complemento) + 5 +
len(addr.Bairro) + 5 +
len(addr.NomeUnidade) + 5 +
len(addr.CEP)
}
// Serialize converts the Address instance into a serialized string
// representation.
//
// This can be used to store the address as a string on a database, for example.
func (addr Address) Serialize() string {
var buf bytes.Buffer
buf.Grow(addr.serializedSize())
buf.WriteString(addr.UF.String())
buf.WriteString("<pbr>")
buf.WriteString(addr.Localidade)
buf.WriteString("<pbr>")
buf.WriteString(addr.Logradouro)
buf.WriteString("<pbr>")
buf.WriteString(addr.Complemento)
buf.WriteString("<pbr>")
buf.WriteString(addr.Bairro)
buf.WriteString("<pbr>")
buf.WriteString(addr.NomeUnidade)
buf.WriteString("<pbr>")
buf.WriteString(addr.CEP.String())
return buf.String()
}
// Deserialize parses a serialized string and populates the Address fields.
func (addr *Address) Deserialize(str string) error {
parts := strings.Split(str, "<pbr>")
if len(parts) != 7 {
return fmt.Errorf(
"%w: invalid length: %d",
ErrInvalidSerializedAddress, len(parts),
)
}
uf, err := NewUFFromStr(parts[0])
if err != nil {
return fmt.Errorf(
"%w: unknown uf: %s",
ErrInvalidSerializedAddress, parts[0],
)
}
cep, err := NewCEP(parts[6])
if err != nil {
return fmt.Errorf(
"%w: invalid CEP: %s",
ErrInvalidSerializedAddress, parts[6],
)
}
addr.UF = uf
addr.Localidade = parts[1]
addr.Logradouro = parts[2]
addr.Complemento = parts[3]
addr.Bairro = parts[4]
addr.NomeUnidade = parts[5]
addr.CEP = cep
return nil
}
// Scan implements the sql.Scanner interface for Address.
func (addr *Address) Scan(value any) error {
str, ok := value.(string)
if !ok {
return fmt.Errorf(
"br: unknown type passed to Address Scan: %T",
value,
)
}
if err := addr.Deserialize(str); err != nil {
return fmt.Errorf("br: invalid serialized Address: %w", err)
}
return nil
}
// Value implements the driver.Valuer interface for Address.
func (addr Address) Value() (driver.Value, error) {
return addr.Serialize(), nil
}
// Validate fetches additional address information based on the associated CEP
// and updates the Address fields.
//
// This method may perform requests to external APIs to retrieve address
// details.
func (addr *Address) Validate() error {
parsedAddr, err := addr.CEP.ToAddress()
if err != nil {
return err
}
*addr = parsedAddr
return nil
}