-
Notifications
You must be signed in to change notification settings - Fork 6
/
lookup.go
125 lines (118 loc) · 3.06 KB
/
lookup.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
package autocomplete_pro
import (
"net/url"
"strconv"
"strings"
)
// Lookup represents all input fields documented here:
// https://smartystreets.com/docs/cloud/us-autocomplete-pro-api#http-request-input-fields
type (
Lookup struct {
Search string
Source string
MaxResults int
CityFilter []string
StateFilter []string
ZIPFilter []string
ExcludeStates []string
PreferCity []string
PreferState []string
PreferZIP []string
PreferRatio int
Geolocation Geolocation
Selected string
Results []*Suggestion
}
Geolocation string
)
const (
GeolocateCity Geolocation = "city"
GeolocateNone Geolocation = "none"
)
func (l Lookup) populate(query url.Values) {
l.populateSearch(query)
l.populateMaxResults(query)
l.populateCityFilter(query)
l.populateStateFilter(query)
l.populateZIPFilter(query)
l.populateExcludeStates(query)
l.populatePreferCity(query)
l.populatePreferState(query)
l.populatePreferZIP(query)
l.populatePreferRatio(query)
l.populateGeolocation(query)
l.populateSource(query)
l.populateSelected(query)
}
func (l Lookup) populateSearch(query url.Values) {
if len(l.Search) > 0 {
query.Set("search", l.Search)
}
}
func (l Lookup) populateMaxResults(query url.Values) {
if l.MaxResults > 0 {
query.Set("max_results", strconv.Itoa(l.MaxResults))
}
}
func (l Lookup) populateCityFilter(query url.Values) {
if len(l.CityFilter) > 0 {
query.Set("include_only_cities", strings.Join(l.CityFilter, ";"))
}
}
func (l Lookup) populateStateFilter(query url.Values) {
if len(l.StateFilter) > 0 {
query.Set("include_only_states", strings.Join(l.StateFilter, ";"))
}
}
func (l Lookup) populateZIPFilter(query url.Values) {
if len(l.ZIPFilter) > 0 {
query.Set("include_only_zip_codes", strings.Join(l.ZIPFilter, ";"))
}
}
func (l Lookup) populateExcludeStates(query url.Values) {
if len(l.ExcludeStates) > 0 {
query.Set("exclude_states", strings.Join(l.ExcludeStates, ";"))
}
}
func (l Lookup) populatePreferCity(query url.Values) {
if len(l.PreferCity) > 0 {
query.Set("prefer_cities", strings.Join(l.PreferCity, ";"))
}
}
func (l Lookup) populatePreferState(query url.Values) {
if len(l.PreferState) > 0 {
query.Set("prefer_states", strings.Join(l.PreferState, ";"))
}
}
func (l Lookup) populatePreferZIP(query url.Values) {
if len(l.PreferZIP) > 0 {
query.Set("prefer_zip_codes", strings.Join(l.PreferZIP, ";"))
}
}
func (l Lookup) populatePreferRatio(query url.Values) {
if l.PreferRatio > 0 {
query.Set("prefer_ratio", strconv.Itoa(l.PreferRatio))
}
}
func (l Lookup) populateGeolocation(query url.Values) {
if len(l.ZIPFilter) > 0 || len(l.PreferZIP) > 0 {
query.Set("prefer_geolocation", "none")
return
}
switch l.Geolocation {
case GeolocateCity:
query.Set("prefer_geolocation", "city")
case GeolocateNone:
query.Set("prefer_geolocation", "none")
}
}
func (l Lookup) populateSource(query url.Values) {
if len(l.Source) > 0 {
query.Set("source", l.Source)
}
}
func (l Lookup) populateSelected(query url.Values) {
if len(l.Selected) > 0 {
query.Set("selected", l.Selected)
}
}