...
1 package profile
2
3 import (
4 "encoding/json"
5 "strings"
6 )
7
8
9 type AddressClaim struct {
10
11 Formatted string `json:"formatted,omitempty"`
12
13 Street string `json:"street_address,omitempty"`
14
15 Locality string `json:"locality,omitempty"`
16
17 Region string `json:"region,omitempty"`
18
19 PostalCode string `json:"postal_code,omitempty"`
20
21 Country string `json:"country,omitempty"`
22 }
23
24 func (ac *AddressClaim) ToMap() map[string]interface{} {
25 var data map[string]interface{}
26
27 addressClaimBytes, err := json.Marshal(ac)
28 if err != nil {
29 return data
30 }
31
32 err = json.Unmarshal(addressClaimBytes, &data)
33 if err != nil {
34 return data
35 }
36
37 return data
38 }
39
40 func (a Address) ToAddressClaim() *AddressClaim {
41 ac := &AddressClaim{
42 Street: a.Street,
43 Locality: a.City,
44 Country: a.Country,
45 PostalCode: a.PostalCode,
46 Region: a.State,
47 }
48
49 ac.Formatted = formatAddress(ac)
50
51 return ac
52 }
53
54 func formatAddress(ac *AddressClaim) string {
55 addressLines := make([]string, 0)
56
57 if ac.Street != "" {
58 addressLines = append(addressLines, ac.Street)
59 }
60
61 if ac.Locality != "" {
62 addressLines = append(addressLines, ac.Locality)
63 }
64
65 if ac.Region != "" {
66 addressLines = append(addressLines, ac.Region)
67 }
68
69 if ac.PostalCode != "" {
70 addressLines = append(addressLines, ac.PostalCode)
71 }
72
73 if ac.Country != "" {
74 addressLines = append(addressLines, ac.Country)
75 }
76
77 return strings.Join(addressLines, "\r\n")
78 }
79
View as plain text