-
Notifications
You must be signed in to change notification settings - Fork 5
/
boolean.go
89 lines (80 loc) · 1.86 KB
/
boolean.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
package veldt
import (
"fmt"
"github.com/unchartedsoftware/veldt/util/json"
)
const (
// And represents an AND binary operator
And = "AND"
// Or represents an OR binary operator
Or = "OR"
// Not represents a NOT unary operator.
Not = "NOT"
)
// BinaryExpression represents a binary boolean expression.
type BinaryExpression struct {
Left Query
Op string
Right Query
}
// Parse should parse through the provided JSON and populate the struct fields.
func (b *BinaryExpression) Parse(params map[string]interface{}) error {
// left
l, ok := params["left"]
if !ok {
return fmt.Errorf("`left` parameter missing from query")
}
left, ok := l.(Query)
if !ok {
return fmt.Errorf("`left` is not a query type")
}
b.Left = left
// op
op, ok := json.GetString(params, "op")
if !ok {
return fmt.Errorf("`op` parameter missing from query")
}
if op != And && op != Or {
return fmt.Errorf("`op` parameter value is not recognized")
}
b.Op = op
// right
r, ok := params["right"]
if !ok {
return fmt.Errorf("`right` parameter missing from query")
}
right, ok := r.(Query)
if !ok {
return fmt.Errorf("`right` is not a query type")
}
b.Right = right
return nil
}
// UnaryExpression represents a unary boolean expression.
type UnaryExpression struct {
Query Query
Op string
}
// Parse should parse through the provided JSON and populate the struct fields.
func (u *UnaryExpression) Parse(params map[string]interface{}) error {
// left
q, ok := params["query"]
if !ok {
return fmt.Errorf("`query` parameter missing from query")
}
query, ok := q.(Query)
if !ok {
return fmt.Errorf("`query` is not a query type")
}
u.Query = query
// op
op, ok := json.GetString(params, "op")
if !ok {
return fmt.Errorf("`op` parameter missing from query")
}
if op != Not {
return fmt.Errorf("`op` parameter value is not recognized")
}
u.Op = op
return nil
}