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 33
/
search_opts.go
98 lines (81 loc) · 1.77 KB
/
search_opts.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
package t38c
import "strconv"
type whereOpt struct {
Field string
Min float64
Max float64
}
type whereinOpt struct {
Field string
Values []float64
}
type whereEvalOpt struct {
Name string
IsSHA bool
Args []string
}
type searchOpts struct {
Asc bool
Desc bool
NoFields bool
Clip bool
Distance bool
Cursor *int
Limit *int
Sparse *int
Where []whereOpt
Wherein []whereinOpt
Match []string
WhereEval []whereEvalOpt
}
func (opts searchOpts) Args() (args []string) {
for _, opt := range opts.Where {
args = append(args, "WHERE", opt.Field, floatString(opt.Min), floatString(opt.Max))
}
for _, opt := range opts.Wherein {
values := make([]string, len(opt.Values))
for i, val := range opt.Values {
values[i] = floatString(val)
}
args = append(args, "WHEREIN", opt.Field)
args = append(args, strconv.Itoa(len(opt.Values)))
args = append(args, values...)
}
for _, opt := range opts.WhereEval {
if opt.IsSHA {
args = append(args, "WHEREEVALSHA", opt.Name)
} else {
args = append(args, "WHEREEVAL", opt.Name)
}
args = append(args, strconv.Itoa(len(opt.Args)))
args = append(args, opt.Args...)
}
for _, pattern := range opts.Match {
args = append(args, "MATCH", pattern)
}
if opts.Asc {
args = append(args, "ASC")
}
if opts.Desc {
args = append(args, "DESC")
}
if opts.NoFields {
args = append(args, "NOFIELDS")
}
if opts.Clip {
args = append(args, "CLIP")
}
if opts.Distance {
args = append(args, "DISTANCE")
}
if opts.Cursor != nil {
args = append(args, "CURSOR", strconv.Itoa(*opts.Cursor))
}
if opts.Limit != nil {
args = append(args, "LIMIT", strconv.Itoa(*opts.Limit))
}
if opts.Sparse != nil {
args = append(args, "SPARSE", strconv.Itoa(*opts.Sparse))
}
return
}