forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
274 lines (240 loc) · 6.19 KB
/
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package query
import (
"bytes"
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
"text/template"
"time"
)
const (
MultipleValueTagSeparator = ";"
// Operators
equalString = "="
greaterThanString = ">"
lessThanString = "<"
greaterOrEqualString = ">="
lessOrEqualString = "<="
containsString = "CONTAINS"
andString = "AND"
// Values
trueString = "true"
falseString = "false"
emptyString = "empty"
timeString = "TIME"
dateString = "DATE"
)
type Query interface {
Matches(tags Tagged) bool
String() string
}
type Queryable interface {
Query() (Query, error)
}
type parsedQuery struct {
query Query
}
func AsQueryable(query Query) parsedQuery {
return parsedQuery{query: query}
}
func (pq parsedQuery) Query() (Query, error) {
return pq.query, nil
}
// A yet-to-parsed query
type String string
func Must(qry Query, err error) Query {
if err != nil {
panic(fmt.Errorf("could not compile: %v", qry))
}
return qry
}
func (qs String) Query() (Query, error) {
if isEmpty(string(qs)) {
return Empty{}, nil
}
return New(string(qs))
}
func MatchAllQueryable() Queryable {
return Empty{}
}
// A fluent query builder
type Builder struct {
queryString string
condition
// reusable buffer for building queryString
bytes.Buffer
error
}
// Templates
type condition struct {
Tag string
Op string
Operand string
}
var conditionTemplate = template.Must(template.New("condition").Parse("{{.Tag}} {{.Op}} {{.Operand}}"))
// Creates a new query builder with a base query that is the conjunction of all queries passed
func NewBuilder(queries ...string) *Builder {
qb := new(Builder)
qb.queryString = qb.and(stringIterator(queries...))
return qb
}
func (qb *Builder) String() string {
return qb.queryString
}
func (qb *Builder) Query() (Query, error) {
if qb.error != nil {
return nil, qb.error
}
return NewOrEmpty(qb.queryString)
}
func NewOrEmpty(queryString string) (Query, error) {
if isEmpty(queryString) {
return Empty{}, nil
}
return New(queryString)
}
// Creates the conjunction of Builder and rightQuery
func (qb *Builder) And(queryBuilders ...*Builder) *Builder {
return NewBuilder(qb.and(queryBuilderIterator(queryBuilders...)))
}
// Creates the conjunction of Builder and tag = operand
func (qb *Builder) AndEquals(tag string, operand interface{}) *Builder {
qb.condition.Tag = tag
qb.condition.Op = equalString
qb.condition.Operand = operandString(operand)
return NewBuilder(qb.and(stringIterator(qb.conditionString())))
}
func (qb *Builder) AndGreaterThanOrEqual(tag string, operand interface{}) *Builder {
qb.condition.Tag = tag
qb.condition.Op = greaterOrEqualString
qb.condition.Operand = operandString(operand)
return NewBuilder(qb.and(stringIterator(qb.conditionString())))
}
func (qb *Builder) AndLessThanOrEqual(tag string, operand interface{}) *Builder {
qb.condition.Tag = tag
qb.condition.Op = lessOrEqualString
qb.condition.Operand = operandString(operand)
return NewBuilder(qb.and(stringIterator(qb.conditionString())))
}
func (qb *Builder) AndStrictlyGreaterThan(tag string, operand interface{}) *Builder {
qb.condition.Tag = tag
qb.condition.Op = greaterThanString
qb.condition.Operand = operandString(operand)
return NewBuilder(qb.and(stringIterator(qb.conditionString())))
}
func (qb *Builder) AndStrictlyLessThan(tag string, operand interface{}) *Builder {
qb.condition.Tag = tag
qb.condition.Op = lessThanString
qb.condition.Operand = operandString(operand)
return NewBuilder(qb.and(stringIterator(qb.conditionString())))
}
func (qb *Builder) AndContains(tag string, operand interface{}) *Builder {
qb.condition.Tag = tag
qb.condition.Op = containsString
qb.condition.Operand = operandString(operand)
return NewBuilder(qb.and(stringIterator(qb.conditionString())))
}
func (qb *Builder) and(queryIterator func(func(string))) string {
defer qb.Buffer.Reset()
qb.Buffer.WriteString(qb.queryString)
queryIterator(func(q string) {
if !isEmpty(q) {
if qb.Buffer.Len() > 0 {
qb.Buffer.WriteByte(' ')
qb.Buffer.WriteString(andString)
qb.Buffer.WriteByte(' ')
}
qb.Buffer.WriteString(q)
}
})
return qb.Buffer.String()
}
func operandString(value interface{}) string {
buf := new(bytes.Buffer)
switch v := value.(type) {
case string:
buf.WriteByte('\'')
buf.WriteString(v)
buf.WriteByte('\'')
return buf.String()
case fmt.Stringer:
return operandString(v.String())
default:
return StringFromValue(v)
}
}
func StringFromValue(value interface{}) string {
rv := reflect.ValueOf(value)
if rv.Kind() == reflect.Ptr && rv.IsNil() {
return "nil"
}
switch v := value.(type) {
case string:
return v
case encoding.TextMarshaler:
bs, _ := v.MarshalText()
return string(bs)
case fmt.Stringer:
return v.String()
case bool:
if v {
return trueString
}
return falseString
case int:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case uint:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
return strconv.FormatFloat(float64(v), 'f', -1, 64)
case time.Time:
return timeString + " " + v.Format(time.RFC3339)
default:
if rv.Kind() == reflect.Slice {
values := make([]string, rv.Len())
for i := 0; i < rv.Len(); i++ {
values[i] = StringFromValue(rv.Index(i).Interface())
}
return strings.Join(values, MultipleValueTagSeparator)
}
return fmt.Sprintf("%v", v)
}
}
func (qb *Builder) conditionString() string {
defer qb.Buffer.Reset()
err := conditionTemplate.Execute(&qb.Buffer, qb.condition)
if err != nil && qb.error == nil {
qb.error = err
}
return qb.Buffer.String()
}
func isEmpty(queryString string) bool {
return queryString == "" || queryString == emptyString
}
// Iterators over some strings
func stringIterator(strs ...string) func(func(string)) {
return func(callback func(string)) {
for _, s := range strs {
callback(s)
}
}
}
func queryBuilderIterator(qbs ...*Builder) func(func(string)) {
return func(callback func(string)) {
for _, qb := range qbs {
callback(qb.String())
}
}
}