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
/
alldbs_query.go
109 lines (95 loc) · 2.28 KB
/
alldbs_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
package cloudant
// NOTE: These parameters are ignored on CouchDB 1.6.X!
//
// QueryBuilder implementation for the AllDBs() API call.
//
// Example:
// query := NewAllDBsQuery().
// StartKey("db1").
// Limit(5).
// Build()
//
// dbList, err := db.AllDBs(query)
import (
"fmt"
"net/url"
"strconv"
)
// AllDBsQueryBuilder defines the available parameter-setting functions.
type AllDBsQueryBuilder interface {
EndKey(string) AllDBsQueryBuilder
InclusiveEnd() AllDBsQueryBuilder
Limit(int) AllDBsQueryBuilder
Skip(int) AllDBsQueryBuilder
StartKey(string) AllDBsQueryBuilder
Build() *allDBsQuery
}
type allDBsQueryBuilder struct {
endKey string
inclusiveEnd bool
limit int
skip int
startKey string
}
// allDBsQuery holds the implemented API call parameters.
type allDBsQuery struct {
EndKey string
InclusiveEnd bool
Limit int
Skip int
StartKey string
}
// NewAllDBsQuery is the entry point.
func NewAllDBsQuery() AllDBsQueryBuilder {
return &allDBsQueryBuilder{}
}
func (a *allDBsQueryBuilder) EndKey(endKey string) AllDBsQueryBuilder {
a.endKey = fmt.Sprintf("\"%s\"", endKey)
return a
}
func (a *allDBsQueryBuilder) InclusiveEnd() AllDBsQueryBuilder {
a.inclusiveEnd = true
return a
}
func (a *allDBsQueryBuilder) Limit(lim int) AllDBsQueryBuilder {
a.limit = lim
return a
}
func (a *allDBsQueryBuilder) Skip(skip int) AllDBsQueryBuilder {
a.skip = skip
return a
}
func (a *allDBsQueryBuilder) StartKey(startKey string) AllDBsQueryBuilder {
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 *allDBsQuery) GetQuery() (url.Values, error) {
vals := url.Values{}
if aq.InclusiveEnd {
vals.Set("inclusive_end", "true")
}
if aq.EndKey != "" {
vals.Set("endkey", aq.EndKey)
}
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))
}
return vals, nil
}
func (a *allDBsQueryBuilder) Build() *allDBsQuery {
return &allDBsQuery{
EndKey: a.endKey,
InclusiveEnd: a.inclusiveEnd,
Limit: a.limit,
Skip: a.skip,
StartKey: a.startKey,
}
}