forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
78 lines (61 loc) · 1.84 KB
/
options.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
package osmapi
import (
"errors"
"fmt"
"strings"
"time"
)
// FeatureOption can be used when fetching a feature or a set of different features.
type FeatureOption interface {
applyFeature([]string) ([]string, error)
}
// At adds an `at=2006-01-02T15:04:05Z` parameter to the request.
// The osm.fyi supports requesting features and maps as they were at the given time.
func At(t time.Time) FeatureOption {
return &at{t}
}
type at struct{ t time.Time }
func (o *at) applyFeature(p []string) ([]string, error) {
return append(p, "at="+o.t.UTC().Format("2006-01-02T15:04:05Z")), nil
}
func (o *at) feature() {}
// NotesOption defines a valid option for the osmapi.Notes by bounding box api.
type NotesOption interface {
applyNotes([]string) ([]string, error)
}
// Limit indicates the number of results to return valid values [1,10000].
// Default is 100.
func Limit(num int) NotesOption {
return &limit{num}
}
// MaxDaysClosed specifies the number of days a note needs to be closed to
// no longer be returned. 0 will return only open notes, -1 will return all notes.
// Default is 7.
func MaxDaysClosed(num int) NotesOption {
return &maxDaysClosed{num}
}
type limit struct{ n int }
func (o *limit) applyNotes(p []string) ([]string, error) {
if o.n < 1 || 10000 < o.n {
return nil, errors.New("osmapi: limit must be between 1 and 10000")
}
return append(p, fmt.Sprintf("limit=%d", o.n)), nil
}
type maxDaysClosed struct{ n int }
func (o *maxDaysClosed) applyNotes(p []string) ([]string, error) {
return append(p, fmt.Sprintf("closed=%d", o.n)), nil
}
func featureOptions(opts []FeatureOption) (string, error) {
if len(opts) == 0 {
return "", nil
}
params := make([]string, 0, len(opts))
var err error
for _, o := range opts {
params, err = o.applyFeature(params)
if err != nil {
return "", err
}
}
return strings.Join(params, "&"), nil
}