forked from cloudant-labs/go-cloudant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_query.go
200 lines (178 loc) · 4.06 KB
/
get_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
package cloudant
// QueryBuilder implementation for the Get() API call.
//
// Example:
// var doc interface{}
// query := NewGetQuery().
// RevsInfo().
// Conflicts().
// Build()
//
// err := db.Get(docId, query, &doc)
import (
"encoding/json"
"net/url"
)
// GetQueryBuilder defines the available parameter-setting functions.
type GetQueryBuilder interface {
Attachments() GetQueryBuilder
AttEncodingInfo() GetQueryBuilder
AttsSince([]string) GetQueryBuilder
Conflicts() GetQueryBuilder
DeletedConflicts() GetQueryBuilder
Latest() GetQueryBuilder
LocalSeq() GetQueryBuilder
Meta() GetQueryBuilder
OpenRevs([]string) GetQueryBuilder
Rev(string) GetQueryBuilder
Revs() GetQueryBuilder
RevsInfo() GetQueryBuilder
Build() *getQuery
}
type getQueryBuilder struct {
attachments bool
attEncodingInfo bool
attsSince []string
conflicts bool
deletedConflicts bool
latest bool
localSeq bool
meta bool
openRevs []string
rev string
revs bool
revsInfo bool
}
// getQuery holds the implemented API call parameters.
type getQuery struct {
Attachments bool
AttEncodingInfo bool
AttsSince []string
Conflicts bool
DeletedConflicts bool
Latest bool
LocalSeq bool
Meta bool
OpenRevs []string
Rev string
Revs bool
RevsInfo bool
}
// NewGetQuery is the entry point.
func NewGetQuery() GetQueryBuilder {
return &getQueryBuilder{}
}
func (g *getQueryBuilder) Attachments() GetQueryBuilder {
g.attachments = true
return g
}
func (g *getQueryBuilder) AttEncodingInfo() GetQueryBuilder {
g.attEncodingInfo = true
return g
}
func (g *getQueryBuilder) AttsSince(since []string) GetQueryBuilder {
g.attsSince = since
return g
}
func (g *getQueryBuilder) Conflicts() GetQueryBuilder {
g.conflicts = true
return g
}
func (g *getQueryBuilder) DeletedConflicts() GetQueryBuilder {
g.deletedConflicts = true
return g
}
func (g *getQueryBuilder) Latest() GetQueryBuilder {
g.latest = true
return g
}
func (g *getQueryBuilder) LocalSeq() GetQueryBuilder {
g.localSeq = true
return g
}
func (g *getQueryBuilder) Meta() GetQueryBuilder {
g.meta = true
return g
}
func (g *getQueryBuilder) OpenRevs(revs []string) GetQueryBuilder {
g.openRevs = revs
return g
}
func (g *getQueryBuilder) Rev(rev string) GetQueryBuilder {
g.rev = rev
return g
}
func (g *getQueryBuilder) Revs() GetQueryBuilder {
g.revs = true
return g
}
func (g *getQueryBuilder) RevsInfo() GetQueryBuilder {
g.revsInfo = true
return g
}
// GetQuery implements the QueryBuilder interface. It returns an
// url.Values map with the non-default values set.
func (gq *getQuery) GetQuery() (url.Values, error) {
vals := url.Values{}
if gq.Attachments {
vals.Set("attachments", "true")
}
if gq.AttEncodingInfo {
vals.Set("att_encoding_info", "true")
}
if len(gq.AttsSince) > 0 {
data, err := json.Marshal(gq.AttsSince)
if err != nil {
return nil, err
}
vals.Set("attsSince", string(data[:]))
}
if gq.Conflicts {
vals.Set("conflicts", "true")
}
if gq.DeletedConflicts {
vals.Set("deleted_conflicts", "true")
}
if gq.Latest {
vals.Set("latest", "true")
}
if gq.LocalSeq {
vals.Set("local_seq", "true")
}
if gq.Meta {
vals.Set("meta", "true")
}
if len(gq.OpenRevs) > 0 {
data, err := json.Marshal(gq.OpenRevs)
if err != nil {
return nil, err
}
vals.Set("open_revs", string(data[:]))
}
if gq.Rev != "" {
vals.Set("rev", gq.Rev)
}
if gq.Revs {
vals.Set("revs", "true")
}
if gq.RevsInfo {
vals.Set("revs_info", "true")
}
return vals, nil
}
func (g *getQueryBuilder) Build() *getQuery {
return &getQuery{
Attachments: g.attachments,
AttEncodingInfo: g.attEncodingInfo,
AttsSince: g.attsSince,
Conflicts: g.conflicts,
DeletedConflicts: g.deletedConflicts,
Latest: g.latest,
LocalSeq: g.localSeq,
Meta: g.meta,
OpenRevs: g.openRevs,
Rev: g.rev,
Revs: g.revs,
RevsInfo: g.revsInfo,
}
}