/
lookup.go
62 lines (55 loc) · 1.99 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
package street
import (
"net/url"
"strconv"
)
// Lookup contains all input fields defined here:
// https://smartystreets.com/docs/us-street-api#input-fields
type Lookup struct {
Street string `json:"street,omitempty"`
Street2 string `json:"street2,omitempty"`
Secondary string `json:"secondary,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
ZIPCode string `json:"zipcode,omitempty"`
LastLine string `json:"lastline,omitempty"`
Addressee string `json:"addressee,omitempty"`
Urbanization string `json:"urbanization,omitempty"`
InputID string `json:"input_id,omitempty"`
MaxCandidates int `json:"candidates,omitempty"` // Default value: 1
MatchStrategy MatchStrategy `json:"match,omitempty"`
Results []*Candidate `json:"results,omitempty"`
}
func (l *Lookup) encodeQueryString(query url.Values) {
encode(query, l.Street, "street")
encode(query, l.Street2, "street2")
encode(query, l.Secondary, "secondary")
encode(query, l.City, "city")
encode(query, l.State, "state")
encode(query, l.ZIPCode, "zipcode")
encode(query, l.LastLine, "lastline")
encode(query, l.Addressee, "addressee")
encode(query, l.Urbanization, "urbanization")
encode(query, l.InputID, "input_id")
if l.MaxCandidates > 0 {
encode(query, strconv.Itoa(l.MaxCandidates), "candidates")
} else if l.MatchStrategy == MatchEnhanced {
encode(query, "5", "candidates")
}
if l.MatchStrategy != MatchStrict {
encode(query, string(l.MatchStrategy), "match")
}
}
func encode(query url.Values, source string, target string) {
if source != "" {
query.Set(target, source)
}
}
/**************************************************************************/
type MatchStrategy string
const (
MatchStrict = MatchStrategy("strict")
MatchRange = MatchStrategy("range") // Deprecated
MatchInvalid = MatchStrategy("invalid")
MatchEnhanced = MatchStrategy("enhanced")
)