forked from cloudant-labs/go-cloudant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changes_query.go
193 lines (173 loc) · 4.11 KB
/
changes_query.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cloudant
// QueryBuilder implementation for the Changes() API call.
//
// Example:
// query := cloudant.NewChangesQuery().IncludeDocs().Build()
//
// changes, err := db.Changes(query)
import (
"encoding/json"
"net/url"
"strconv"
)
// ChangesQueryBuilder defines the available parameter-setting functions.
type ChangesQueryBuilder interface {
Conflicts() ChangesQueryBuilder
Descending() ChangesQueryBuilder
DocIDs([]string) ChangesQueryBuilder
Feed(string) ChangesQueryBuilder
Filter(string) ChangesQueryBuilder
Heartbeat(int) ChangesQueryBuilder
IncludeDocs() ChangesQueryBuilder
Limit(int) ChangesQueryBuilder
SeqInterval(int) ChangesQueryBuilder
Since(string) ChangesQueryBuilder
Style(string) ChangesQueryBuilder
Timeout(int) ChangesQueryBuilder
Build() *changesQuery
}
type changesQueryBuilder struct {
conflicts bool
descending bool
docIDs []string
feed string
filter string
heartbeat int
includeDocs bool
limit int
seqInterval int
since string
style string
timeout int
}
// changesQuery holds the implemented API call parameters. The doc_ids parameter
// is not yet implemented.
type changesQuery struct {
Conflicts bool
Descending bool
DocIDs []string
Feed string
Filter string
Heartbeat int
IncludeDocs bool
Limit int
SeqInterval int
Since string
Style string
Timeout int
}
// NewChangesQuery is the entry point.
func NewChangesQuery() ChangesQueryBuilder {
return &changesQueryBuilder{}
}
func (c *changesQueryBuilder) Conflicts() ChangesQueryBuilder {
c.conflicts = true
return c
}
func (c *changesQueryBuilder) Descending() ChangesQueryBuilder {
c.descending = true
return c
}
func (c *changesQueryBuilder) DocIDs(docIDs []string) ChangesQueryBuilder {
c.docIDs = docIDs
return c
}
func (c *changesQueryBuilder) Feed(feed string) ChangesQueryBuilder {
c.feed = feed
return c
}
func (c *changesQueryBuilder) Filter(filter string) ChangesQueryBuilder {
c.filter = filter
return c
}
func (c *changesQueryBuilder) Heartbeat(hb int) ChangesQueryBuilder {
c.heartbeat = hb
return c
}
func (c *changesQueryBuilder) IncludeDocs() ChangesQueryBuilder {
c.includeDocs = true
return c
}
func (c *changesQueryBuilder) Limit(lim int) ChangesQueryBuilder {
c.limit = lim
return c
}
func (c *changesQueryBuilder) SeqInterval(interval int) ChangesQueryBuilder {
c.seqInterval = interval
return c
}
func (c *changesQueryBuilder) Since(seq string) ChangesQueryBuilder {
if seq != "" {
c.since = seq
}
return c
}
func (c *changesQueryBuilder) Style(style string) ChangesQueryBuilder {
c.style = style
return c
}
func (c *changesQueryBuilder) Timeout(secs int) ChangesQueryBuilder {
c.timeout = secs
return c
}
// GetQuery implements the QueryBuilder interface. It returns an
// url.Values map with the non-default values set.
func (cq *changesQuery) GetQuery() (url.Values, error) {
vals := url.Values{}
if cq.Conflicts {
vals.Set("conflicts", "true")
}
if cq.Descending {
vals.Set("descending", "true")
}
if len(cq.DocIDs) > 0 {
data, err := json.Marshal(cq.DocIDs)
if err != nil {
return nil, err
}
vals.Set("doc_ids", string(data[:]))
}
if cq.IncludeDocs {
vals.Set("include_docs", "true")
}
if cq.Feed != "" {
vals.Set("feed", cq.Feed)
}
if cq.Filter != "" {
vals.Set("filter", cq.Filter)
}
if cq.Heartbeat > 0 {
vals.Set("heartbeat", strconv.Itoa(cq.Heartbeat))
}
if cq.Limit > 0 {
vals.Set("limit", strconv.Itoa(cq.Limit))
}
if cq.SeqInterval > 0 {
vals.Set("seq_interval", strconv.Itoa(cq.SeqInterval))
}
if cq.Style != "" {
vals.Set("style", cq.Style)
}
if cq.Since != "" {
vals.Set("since", cq.Since)
}
if cq.Timeout > 0 {
vals.Set("timeout", strconv.Itoa(cq.Timeout))
}
return vals, nil
}
func (c *changesQueryBuilder) Build() *changesQuery {
return &changesQuery{
Conflicts: c.conflicts,
Descending: c.descending,
Feed: c.feed,
Filter: c.filter,
Heartbeat: c.heartbeat,
IncludeDocs: c.includeDocs,
Limit: c.limit,
SeqInterval: c.seqInterval,
Since: c.since,
Style: c.style,
Timeout: c.timeout,
}
}