-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathboolean_operator.go
More file actions
114 lines (107 loc) · 2.28 KB
/
Copy pathboolean_operator.go
File metadata and controls
114 lines (107 loc) · 2.28 KB
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
package repository
import (
"github.com/globalsign/mgo/bson"
)
type operatorType int
const (
OperatorAnd operatorType = iota
OperatorNot
OperatorNor
OperatorOr
// Evaluation Operators
OperatorText
// Array Operators
OperatorTypeElemMatch
)
type BooleanOperator struct {
Field *string
Type operatorType
Conditions []interface{}
}
func (o *BooleanOperator) GetCondition() (bson.DocElem, error) {
var t string
switch o.Type {
case OperatorAnd:
t = "$and"
case OperatorNor:
t = "$nor"
case OperatorOr:
t = "$or"
case OperatorNot: // Consider behaviors using the $not and $regex
t = "$not"
cast := *o.Conditions[0].(*BinaryOperatorImpl)
if cast.Type == BinaryOperatorTypeRegex {
return bson.DocElem{
Name: *o.Field,
Value: bson.M{
t: cast.Value,
},
}, nil
} else {
return bson.DocElem{
Name: *o.Field,
Value: bson.M{
t: bson.M{
*cast.OpField: cast.Value,
},
},
}, nil
}
case OperatorText:
t = "$text"
return bson.DocElem{
Name: t,
Value: o.Conditions[0].(FindText),
}, nil
case OperatorTypeElemMatch:
t = "$elemMatch"
elem := make(bson.D, 0, len(o.Conditions))
for i, cond := range o.Conditions {
switch cond.(type) {
case *BooleanOperator:
p, err := o.Conditions[i].(*BooleanOperator).GetCondition()
if err != nil {
return bson.DocElem{}, NewErrTypeNotSupported(cond)
}
elem = append(elem, p)
case BinaryOperator:
p, err := o.Conditions[i].(*BinaryOperatorImpl).GetCondition()
if err != nil {
return bson.DocElem{}, NewErrTypeNotSupported(cond)
}
elem = append(elem, p)
default:
return bson.DocElem{}, NewErrTypeNotSupported(cond)
}
}
return bson.DocElem{
Name: *o.Field,
Value: bson.M{
t: elem,
},
}, nil
}
conds := make([]interface{}, 0, len(o.Conditions))
for _, cond := range o.Conditions {
switch condition := cond.(type) {
case QueryBuilder:
ccc, err := condition.GetQuery()
if err != nil {
return bson.DocElem{}, err
}
if ccc != nil {
conds = append(conds, ccc)
}
case BinaryOperator:
ccc, err := condition.GetCondition()
if err != nil {
return bson.DocElem{}, err
}
conds = append(conds, bson.D{ccc})
}
}
return bson.DocElem{
Name: t,
Value: conds,
}, nil
}