-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewquery_options.go
189 lines (163 loc) · 4.23 KB
/
viewquery_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
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
package gocb
import (
"bytes"
"context"
"encoding/json"
"net/url"
"strconv"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
)
// StaleMode specifies the consistency required for a view query.
type StaleMode int
const (
// Before indicates to update the index before querying it.
Before = StaleMode(1)
// None indicates that no special behaviour should be used.
None = StaleMode(2)
// After indicates to update the index asynchronously after querying.
After = StaleMode(3)
)
// SortOrder specifies the ordering for the view queries results.
type SortOrder int
const (
// Ascending indicates the query results should be sorted from lowest to highest.
Ascending = SortOrder(1)
// Descending indicates the query results should be sorted from highest to lowest.
Descending = SortOrder(2)
)
// Range specifies a value range to get results between.
type Range struct {
Start interface{}
End interface{}
InclusiveEnd bool
}
// ViewOptions represents the options available when executing view query.
type ViewOptions struct {
Stale StaleMode
Skip uint
Limit uint
Order SortOrder
Reduce bool
Group bool
GroupLevel uint
Key interface{}
Keys []interface{}
Range *Range
IDRangeStart string
IDRangeEnd string
Development bool
Custom map[string]string
Context context.Context
ParentSpanContext opentracing.SpanContext
}
func (opts *ViewOptions) toURLValues() (*url.Values, error) {
options := &url.Values{}
if opts.Stale != 0 {
if opts.Stale == Before {
options.Set("stale", "false")
} else if opts.Stale == None {
options.Set("stale", "ok")
} else if opts.Stale == After {
options.Set("stale", "update_after")
} else {
return nil, errors.New("Unexpected stale option")
}
}
if opts.Skip != 0 {
options.Set("skip", strconv.FormatUint(uint64(opts.Skip), 10))
}
if opts.Limit != 0 {
options.Set("limit", strconv.FormatUint(uint64(opts.Limit), 10))
}
if opts.Order != 0 {
if opts.Order == Ascending {
options.Set("descending", "false")
} else if opts.Order == Descending {
options.Set("descending", "true")
} else {
return nil, errors.New("Unexpected order option")
}
}
options.Set("reduce", "false") // is this line necessary?
if opts.Reduce {
options.Set("reduce", "true")
// Only set group if a reduce view
options.Set("group", "false") // is this line necessary?
if opts.Group {
options.Set("group", "true")
}
if opts.GroupLevel != 0 {
options.Set("group_level", strconv.FormatUint(uint64(opts.GroupLevel), 10))
}
}
if opts.Key != nil {
jsonKey, err := opts.marshalJson(opts.Key)
if err != nil {
return nil, err
}
options.Set("key", string(jsonKey))
}
if len(opts.Keys) > 0 {
jsonKeys, err := opts.marshalJson(opts.Keys)
if err != nil {
return nil, err
}
options.Set("keys", string(jsonKeys))
}
if opts.Range != nil {
if opts.Range.Start != nil {
jsonStartKey, err := opts.marshalJson(opts.Range.Start)
if err != nil {
return nil, err
}
options.Set("startkey", string(jsonStartKey))
} else {
options.Del("startkey")
}
if opts.Range.End != nil {
jsonEndKey, err := opts.marshalJson(opts.Range.End)
if err != nil {
return nil, err
}
options.Set("endkey", string(jsonEndKey))
} else {
options.Del("endkey")
}
if opts.Range.Start != nil || opts.Range.End != nil {
if opts.Range.InclusiveEnd {
options.Set("inclusive_end", "true")
} else {
options.Set("inclusive_end", "false")
}
} else {
options.Del("inclusive_end")
}
}
if opts.IDRangeStart == "" {
options.Del("startkey_docid")
} else {
options.Set("startkey_docid", opts.IDRangeStart)
}
if opts.IDRangeEnd == "" {
options.Del("endkey_docid")
} else {
options.Set("endkey_docid", opts.IDRangeEnd)
}
if opts.Custom != nil {
for k, v := range opts.Custom {
options.Set(k, v)
}
}
return options, nil
}
func (opts *ViewOptions) marshalJson(value interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(value)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}