-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
42 lines (39 loc) · 949 Bytes
/
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
package cep
import (
"encoding/json"
"strings"
)
// Address has address informations.
type Address struct {
CEP string `json:"cep"`
City string `json:"city"`
Neighborhood string `json:"neighborhood"`
State string `json:"state"`
Street string `json:"street"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (a *Address) UnmarshalJSON(data []byte) error {
fields := make(map[string]string)
err := json.Unmarshal(data, &fields)
if err != nil {
return &Error{Kind: UnmarshalErr, Err: err}
}
for name, val := range fields {
switch strings.ToLower(name) {
case "cep":
a.CEP, err = Canonicalize(val)
if err != nil {
return &Error{Kind: UnmarshalErr, Err: err}
}
case "city", "localidade":
a.City = val
case "neighborhood", "bairro":
a.Neighborhood = val
case "state", "uf":
a.State = val
case "street", "logradouro":
a.Street = val
}
}
return nil
}