-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.go
161 lines (151 loc) · 3.75 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
package postal
import (
"fmt"
"strings"
)
// warning: be careful when adding new fields, today the compatibility is only done for Nominatim
// if you want to add more drivers (i.e., gmap), please do not add fields, but create a mapping
// function that transforms a gmap struct to Address
// if unsure, ask @moul
type Address struct {
BusStop string `json:"bus_stop"`
Building string `json:"building"`
HouseNumber string `json:"house_number"`
Road string `json:"road"`
Suburb string `json:"suburb"`
CityDistrict string `json:"city_district"`
City string `json:"city"`
Commercial string `json:"commercial"`
County string `json:"county"`
Hamlet string `json:"hamlet"`
State string `json:"state"`
StateDistrict string `json:"state_district"`
Country Country `json:"country"`
Town string `json:"town"`
Village string `json:"village"`
PostCode string `json:"postcode"`
CountryCode string `json:"country_code"`
}
// MinimalCompare returns a minimal address based on another one
// if a and b doesn't share the same city, it's not a mandatory
// to detail smaller objects than the city
//
// This function should be used when we display from + to addresses on the same screen
func (a *Address) MinimalCompare(b *Address) (string, string) {
if string(a.Country) != string(b.Country) {
aRet := fmt.Sprintf("%s, %s", a.SmartCity(), a.Country.Short())
bRet := fmt.Sprintf("%s, %s", b.SmartCity(), b.Country.Short())
return aRet, bRet
}
// FIXME: add more checks: states, cities, etc
return a.Short(), b.Short()
}
// Full returns a complete (cannot be more precise) address
func (a *Address) Full() string {
parts := []string{
a.BusStop,
a.Building,
a.HouseNumber,
a.Road,
a.Suburb,
a.Village,
a.CityDistrict,
a.Commercial,
a.Hamlet,
a.City,
a.Town,
a.County,
a.StateDistrict,
a.State,
a.PostCode,
string(a.Country),
}
return strings.Join(nonEmpty(parts), ", ")
}
// Short returns a minimalist representation of the address
func (a *Address) Short() string {
var parts []string
switch a.CountryCode {
case "fr":
parts = []string{
a.SmartRoad(),
a.SmartCity(),
}
case "us":
parts = []string{
a.SmartRoad(),
a.SmartCity(),
a.State,
}
default:
parts = []string{
a.SmartRoad(),
a.SmartCity(),
}
}
return strings.Join(nonEmpty(parts), ", ")
}
func (a *Address) SmartRoad() string {
switch a.CountryCode {
case "fr":
return first(
a.Building,
fmt.Sprintf("%s %s", a.HouseNumber, a.Road),
)
}
return first(
a.Building,
strings.Join(nonEmpty([]string{a.HouseNumber, a.Road}), ", "),
)
}
func (a *Address) SmartCity() string {
return first(
a.Village,
a.Town,
a.City,
a.County,
a.CityDistrict,
a.State,
a.StateDistrict,
)
}
// Long returns the a long but not full address
func (a *Address) Long() string {
switch a.CountryCode {
case "fr":
parts := []string{}
parts = append(parts, a.SmartRoad())
parts = append(parts, a.PostCode)
parts = append(parts, a.SmartCity())
return fmt.Sprintf("%s, %s", strings.Join(nonEmpty(parts), " "), a.Country.Short())
}
parts := []string{
// a.BusStop,
a.Building,
a.HouseNumber,
a.Road,
a.Suburb,
a.Village,
a.CityDistrict,
// a.Commercial,
a.Hamlet,
a.City,
a.Town,
a.County,
a.StateDistrict,
a.State,
a.PostCode,
a.Country.Short(),
}
return strings.Join(nonEmpty(parts), ", ")
}
// Letter returns an address in a letter-ready format (multiline)
func (a *Address) Letter() string {
lines := []string{
a.Building,
strings.Join(nonEmpty([]string{a.HouseNumber, a.Road}), ", "),
strings.Join(nonEmpty([]string{a.PostCode, a.SmartCity()}), " - "),
string(a.Country),
}
return strings.Join(nonEmpty(lines), "\n")
}