This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathalldocs_query.go
220 lines (196 loc) · 4.73 KB
/
alldocs_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cloudant
// QueryBuilder implementation for the AllDocs() API call.
//
// Example:
// query := NewAllDocsQuery().
// IncludeDocs().
// Build()
//
// changes, err := db.All(query)
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
// AllDocsQueryBuilder defines the available parameter-setting functions.
type AllDocsQueryBuilder interface {
Conflicts() AllDocsQueryBuilder
DeletedConflicts() AllDocsQueryBuilder
Descending() AllDocsQueryBuilder
EndKey(string) AllDocsQueryBuilder
IncludeDocs() AllDocsQueryBuilder
InclusiveEnd() AllDocsQueryBuilder
Key(string) AllDocsQueryBuilder
Keys([]string) AllDocsQueryBuilder
Limit(int) AllDocsQueryBuilder
Meta() AllDocsQueryBuilder
R(int) AllDocsQueryBuilder
RevsInfo() AllDocsQueryBuilder
Skip(int) AllDocsQueryBuilder
StartKey(string) AllDocsQueryBuilder
Build() *allDocsQuery
}
type allDocsQueryBuilder struct {
conflicts bool
deletedConflicts bool
descending bool
endKey string
includeDocs bool
inclusiveEnd bool
key string
keys []string
limit int
meta bool
r int
revsInfo bool
skip int
startKey string
}
// allDocsQuery holds the implemented API call parameters.
type allDocsQuery struct {
Conflicts bool
DeletedConflicts bool
Descending bool
EndKey string
IncludeDocs bool
InclusiveEnd bool
Key string
Keys []string
Limit int
Meta bool
R int
RevsInfo bool
Skip int
StartKey string
}
// NewAllDocsQuery is the entry point.
func NewAllDocsQuery() AllDocsQueryBuilder {
return &allDocsQueryBuilder{}
}
func (a *allDocsQueryBuilder) Conflicts() AllDocsQueryBuilder {
a.conflicts = true
return a
}
func (a *allDocsQueryBuilder) DeletedConflicts() AllDocsQueryBuilder {
a.deletedConflicts = true
return a
}
func (a *allDocsQueryBuilder) Descending() AllDocsQueryBuilder {
a.descending = true
return a
}
func (a *allDocsQueryBuilder) EndKey(endKey string) AllDocsQueryBuilder {
a.endKey = fmt.Sprintf("\"%s\"", endKey)
return a
}
func (a *allDocsQueryBuilder) IncludeDocs() AllDocsQueryBuilder {
a.includeDocs = true
return a
}
func (a *allDocsQueryBuilder) InclusiveEnd() AllDocsQueryBuilder {
a.inclusiveEnd = true
return a
}
func (a *allDocsQueryBuilder) Key(key string) AllDocsQueryBuilder {
a.key = fmt.Sprintf("\"%s\"", key)
return a
}
func (a *allDocsQueryBuilder) Keys(keys []string) AllDocsQueryBuilder {
a.keys = keys
return a
}
func (a *allDocsQueryBuilder) Limit(lim int) AllDocsQueryBuilder {
a.limit = lim
return a
}
func (a *allDocsQueryBuilder) Meta() AllDocsQueryBuilder {
a.meta = true
return a
}
func (a *allDocsQueryBuilder) R(r int) AllDocsQueryBuilder {
a.r = r
return a
}
func (a *allDocsQueryBuilder) RevsInfo() AllDocsQueryBuilder {
a.revsInfo = true
return a
}
func (a *allDocsQueryBuilder) Skip(skip int) AllDocsQueryBuilder {
a.skip = skip
return a
}
func (a *allDocsQueryBuilder) StartKey(startKey string) AllDocsQueryBuilder {
a.startKey = fmt.Sprintf("\"%s\"", startKey)
return a
}
// GetQuery implements the QueryBuilder interface. It returns an
// url.Values map with the non-default values set.
func (aq *allDocsQuery) GetQuery() (url.Values, error) {
vals := url.Values{}
if aq.Conflicts {
vals.Set("conflicts", "true")
}
if aq.Descending {
vals.Set("descending", "true")
}
if aq.IncludeDocs {
vals.Set("include_docs", "true")
}
if aq.DeletedConflicts {
vals.Set("deleted_conflicts", "true")
}
if aq.InclusiveEnd {
vals.Set("inclusive_end", "true")
}
if aq.RevsInfo {
vals.Set("revs_info", "true")
}
if aq.Meta {
vals.Set("meta", "true")
}
if aq.EndKey != "" {
vals.Set("endkey", aq.EndKey)
}
if aq.Key != "" {
vals.Set("key", aq.Key)
}
if len(aq.Keys) > 0 {
data, err := json.Marshal(aq.Keys)
if err != nil {
return nil, err
}
vals.Set("keys", string(data[:]))
}
if aq.StartKey != "" {
vals.Set("startkey", aq.StartKey)
}
if aq.Limit > 0 {
vals.Set("limit", strconv.Itoa(aq.Limit))
}
if aq.Skip > 0 {
vals.Set("skip", strconv.Itoa(aq.Skip))
}
if aq.R > 0 {
vals.Set("r", strconv.Itoa(aq.R))
}
return vals, nil
}
func (a *allDocsQueryBuilder) Build() *allDocsQuery {
return &allDocsQuery{
Conflicts: a.conflicts,
DeletedConflicts: a.deletedConflicts,
Descending: a.descending,
EndKey: a.endKey,
IncludeDocs: a.includeDocs,
InclusiveEnd: a.inclusiveEnd,
Key: a.key,
Keys: a.keys,
Limit: a.limit,
Meta: a.meta,
R: a.r,
RevsInfo: a.revsInfo,
Skip: a.skip,
StartKey: a.startKey,
}
}