-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
65 lines (52 loc) · 1.14 KB
/
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
package client
import (
"fmt"
"strings"
)
// Query provides a way to filter which objects are matched
type Query interface {
FieldEqualsValue(name, val string) Query
String() string
}
// QueryOperator provides boolean and arithmetic operators for field-value pairs
type QueryOperator int
const (
// QueryOperatorEquals ...
QueryOperatorEquals QueryOperator = iota + 1
)
// NewQuery creates a new Query
func NewQuery() Query {
return &query{queryParts: make([]queryPart, 0)}
}
type queryPart struct {
fieldName string
op QueryOperator
value string
}
type query struct {
queryParts []queryPart
}
func (q *query) FieldEqualsValue(name, val string) Query {
q.queryParts = append(q.queryParts, queryPart{name, QueryOperatorEquals, val})
return q
}
func (q *query) String() string {
count := len(q.queryParts)
if count == 0 {
return ""
}
var sb strings.Builder
for i, qp := range q.queryParts {
if i == 0 {
sb.WriteString("{")
}
s := fmt.Sprintf("\"%s\" : \"%s\"", qp.fieldName, qp.value)
sb.WriteString(s)
if i == (count - 1) {
sb.WriteString("}")
} else {
sb.WriteString(", ")
}
}
return sb.String()
}