-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
125 lines (105 loc) · 2.52 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
package bsonquery
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type builder struct {
conditionGroups map[int]conditionGroup
}
type conditionGroup struct {
logicalOp logicalOperator
conditions []condition
}
type logicalOperator int
const (
lopAnd logicalOperator = iota
lopOr
lopNor
lopNot
copEQ = "$eq"
copGT = "$gt"
copGTE = "$gte"
copIN = "$in"
copLT = "$lt"
copLTE = "$lte"
copNE = "$ne"
copNIN = "$nin"
copRegex = "$regex"
copExists = "$exists"
)
func Builder() *builder {
b := &builder{}
b.conditionGroups = make(map[int]conditionGroup)
return b
}
func (b *builder) And(c ...condition) *builder {
l := len(b.conditionGroups)
b.conditionGroups[l] = makeConditionGroup(lopAnd, c...)
return b
}
func (b *builder) Or(c ...condition) *builder {
if len(c) < 2 {
panic("OR logical operator require minimum two conditions")
}
l := len(b.conditionGroups)
b.conditionGroups[l] = makeConditionGroup(lopOr, c...)
return b
}
func (b *builder) Nor(c ...condition) *builder {
if len(c) < 2 {
panic("NOR logical operator require minimum two conditions")
}
l := len(b.conditionGroups)
b.conditionGroups[l] = makeConditionGroup(lopNor, c...)
return b
}
func (b *builder) Not(c condition) *builder {
l := len(b.conditionGroups)
b.conditionGroups[l] = makeConditionGroup(lopNor, c)
return b
}
func (b *builder) Build() bson.M {
m := bson.M{}
for _, cg := range b.conditionGroups {
switch cg.logicalOp {
case lopAnd:
for _, c := range cg.conditions {
m[c.fieldName] = bson.M{c.operator: c.value}
}
case lopOr:
m["$or"] = getArrayOfM(cg.conditions)
case lopNor:
m["$nor"] = getArrayOfM(cg.conditions)
case lopNot:
c := cg.conditions[0]
m[c.fieldName] = bson.M{"$not": getM(c)}
}
}
return m
}
func makeConditionGroup(op logicalOperator, c ...condition) conditionGroup {
cg := conditionGroup{}
cg.logicalOp = op
cg.conditions = make([]condition, 0, len(c))
for _, cd := range c {
cg.conditions = append(cg.conditions, cd)
}
return cg
}
func getArrayOfM(cond []condition) []bson.M {
ar := make([]bson.M, 0, len(cond))
for _, c := range cond {
//ar = append(ar, bson.M{c.fieldName: c.value})
ar = append(ar, bson.M{c.fieldName: getM(c)})
}
return ar
}
func getM(c condition) bson.M {
if c.operator == copRegex {
//primitive.Regex{Pattern: "he", Options: ""}
m := bson.M{c.operator: primitive.Regex{Pattern: c.value.(string), Options: c.options}}
return m
}
m := bson.M{c.operator: c.value}
return m
}