This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathscan_query_builder.go
106 lines (90 loc) · 2.92 KB
/
scan_query_builder.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
package t38c
import "context"
// ScanQueryBuilder struct
type ScanQueryBuilder struct {
client tile38Client
key string
outputFormat *OutputFormat
opts searchOpts
}
func newScanQueryBuilder(client tile38Client, key string) ScanQueryBuilder {
return ScanQueryBuilder{
client: client,
key: key,
}
}
func (query ScanQueryBuilder) toCmd() cmd {
args := []string{query.key}
args = append(args, query.opts.Args()...)
if query.outputFormat != nil {
args = append(args, query.outputFormat.Name)
args = append(args, query.outputFormat.Args...)
}
return newCmd("SCAN", args...)
}
// Do cmd
func (query ScanQueryBuilder) Do(ctx context.Context) (*SearchResponse, error) {
cmd := query.toCmd()
resp := &SearchResponse{}
err := query.client.jExecute(ctx, &resp, cmd.Name, cmd.Args...)
if err != nil {
return nil, err
}
return resp, nil
}
// Cursor is used to iterate though many objects from the search results.
// An iteration begins when the CURSOR is set to Zero or not included with the request,
// and completes when the cursor returned by the server is Zero.
func (query ScanQueryBuilder) Cursor(cursor int) ScanQueryBuilder {
query.opts.Cursor = &cursor
return query
}
// Limit can be used to limit the number of objects returned for a single search request.
func (query ScanQueryBuilder) Limit(limit int) ScanQueryBuilder {
query.opts.Limit = &limit
return query
}
// Match is similar to WHERE except that it works on the object id instead of fields.
// There can be multiple MATCH options in a single search.
// The MATCH value is a simple glob pattern.
func (query ScanQueryBuilder) Match(pattern string) ScanQueryBuilder {
query.opts.Match = append(query.opts.Match, pattern)
return query
}
// Asc order. Only for SEARCH and SCAN commands.
func (query ScanQueryBuilder) Asc() ScanQueryBuilder {
query.opts.Asc = true
return query
}
// Desc order. Only for SEARCH and SCAN commands.
func (query ScanQueryBuilder) Desc() ScanQueryBuilder {
query.opts.Desc = true
return query
}
// Where allows for filtering out results based on field values.
func (query ScanQueryBuilder) Where(field string, min, max float64) ScanQueryBuilder {
query.opts.Where = append(query.opts.Where, whereOpt{
Field: field,
Min: min,
Max: max,
})
return query
}
// Wherein is similar to Where except that it checks whether the object’s field value is in a given list.
func (query ScanQueryBuilder) Wherein(field string, values ...float64) ScanQueryBuilder {
query.opts.Wherein = append(query.opts.Wherein, whereinOpt{
Field: field,
Values: values,
})
return query
}
// NoFields tells the server that you do not want field values returned with the search results.
func (query ScanQueryBuilder) NoFields() ScanQueryBuilder {
query.opts.NoFields = true
return query
}
// Format set response format.
func (query ScanQueryBuilder) Format(fmt OutputFormat) ScanQueryBuilder {
query.outputFormat = &fmt
return query
}