This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperator.go
197 lines (169 loc) · 6.8 KB
/
operator.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
package kpmongo
import (
"regexp"
"strings"
"time"
"github.com/raditzlawliet/kendoparser"
"github.com/raditzlawliet/kendoparser/helper"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/spf13/cast"
)
var (
// OperatorManager of Mongo Parser
OperatorManager = new(kendoparser.OperatorManager)
// Operator bundle of Mongo Parser
Operator = OperatorBundle{}
)
type OperatorBundle struct{}
func init() {
RegisterOperator()
}
// RegisterOperator RegisterOperator
func RegisterOperator() {
OperatorManager.SetDefaultOperator(Operator.Equal)
OperatorManager.RegisterOperator(Operator.Equal, "eq", "equal", "=", "==")
OperatorManager.RegisterOperator(Operator.NotEqual, "ne", "neq", "notequal", "<>", "!=")
OperatorManager.RegisterOperator(Operator.EqualDate, "eqdate", "equaldate", "=date", "==date")
OperatorManager.RegisterOperator(Operator.NotEqualDate, "nedate", "neqdate", "notequaldate", "<>date", "!=date")
OperatorManager.RegisterOperator(Operator.Contain, "contain", "contains", "include", "includes")
OperatorManager.RegisterOperator(Operator.NotContain, "notcontains", "notcontains", "doesnotcontain", "doesnotcontains", "notinclude", "notincludes", "doesnotinclude", "doesnotincludes")
OperatorManager.RegisterOperator(Operator.In, "in")
OperatorManager.RegisterOperator(Operator.NotIn, "notin", "nin")
OperatorManager.RegisterOperator(Operator.Gt, "gt", ">")
OperatorManager.RegisterOperator(Operator.Lt, "lt", "<")
OperatorManager.RegisterOperator(Operator.Gte, "gte", ">=")
OperatorManager.RegisterOperator(Operator.Lte, "lte", "<=")
OperatorManager.RegisterOperator(Operator.GtDate, "gtdate", ">date")
OperatorManager.RegisterOperator(Operator.LtDate, "ltdate", "<date")
OperatorManager.RegisterOperator(Operator.GteDate, "gtedate", ">=date")
OperatorManager.RegisterOperator(Operator.LteDate, "ltedate", "<=date")
OperatorManager.RegisterOperator(Operator.Exists, "exist", "exists")
OperatorManager.RegisterOperator(Operator.DateExists, "dateexist", "dateexists")
OperatorManager.RegisterOperator(Operator.Between, "between")
}
func (o *OperatorBundle) Equal(kf kendoparser.Filter) interface{} {
// Ignore-case only for string :D
if value, ok := kf.Value.(string); ok && kf.IgnoreCase {
value := regexp.QuoteMeta(value)
return bson.M{kf.Field: primitive.Regex{Pattern: "^" + strings.ToLower(value) + "$", Options: "i"}}
}
// other string, it will directly :|
return bson.M{kf.Field: bson.M{"$eq": kf.Value}}
}
func (o *OperatorBundle) NotEqual(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$ne": kf.Value}}
}
func (o *OperatorBundle) EqualDate(kf kendoparser.Filter) interface{} {
dtVariable, _ := time.Parse(time.RFC3339, cast.ToString(kf.Value))
return bson.M{kf.Field: bson.M{"$eq": dtVariable}}
}
func (o *OperatorBundle) NotEqualDate(kf kendoparser.Filter) interface{} {
dtVariable, _ := time.Parse(time.RFC3339, cast.ToString(kf.Value))
return bson.M{kf.Field: bson.M{"$ne": dtVariable}}
}
func (o *OperatorBundle) Contain(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: RegexContains(cast.ToString(kf.Value), kf.IgnoreCase)}
}
func (o *OperatorBundle) ContainNumber(kf kendoparser.Filter) interface{} {
field := kf.Field
if !strings.HasPrefix(field, "$") {
field = "$" + field
}
return bson.M{
"$expr": bson.M{
"$regexMatch": bson.M{
"input": bson.M{"$toString": field},
"regex": RegexContains(cast.ToString(kf.Value), kf.IgnoreCase),
},
},
}
}
func (o *OperatorBundle) NotContain(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$ne": RegexContains(cast.ToString(kf.Value), kf.IgnoreCase)}}
}
func (o *OperatorBundle) In(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$in": kf.Values}}
}
func (o *OperatorBundle) NotIn(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$nin": kf.Values}}
}
func (o *OperatorBundle) Gt(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$gt": kf.Value}}
}
func (o *OperatorBundle) Lt(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$lt": kf.Value}}
}
func (o *OperatorBundle) Gte(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$gte": kf.Value}}
}
func (o *OperatorBundle) Lte(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$lte": kf.Value}}
}
func (o *OperatorBundle) GtDate(kf kendoparser.Filter) interface{} {
str := cast.ToString(kf.Value)
if t, ok := kf.Value.(time.Time); ok { // fixing time.Time value not valid converted :|
str = t.Format(time.RFC3339)
}
dtVariable, _ := time.Parse(time.RFC3339, str)
return bson.M{kf.Field: bson.M{"$gt": dtVariable}}
}
func (o *OperatorBundle) LtDate(kf kendoparser.Filter) interface{} {
str := cast.ToString(kf.Value)
if t, ok := kf.Value.(time.Time); ok { // fixing time.Time value not valid converted :|
str = t.Format(time.RFC3339)
}
dtVariable, _ := time.Parse(time.RFC3339, str)
return bson.M{kf.Field: bson.M{"$lt": dtVariable}}
}
func (o *OperatorBundle) GteDate(kf kendoparser.Filter) interface{} {
str := cast.ToString(kf.Value)
if t, ok := kf.Value.(time.Time); ok { // fixing time.Time value not valid converted :|
str = t.Format(time.RFC3339)
}
dtVariable, _ := time.Parse(time.RFC3339, str)
return bson.M{kf.Field: bson.M{"$gte": dtVariable}}
}
func (o *OperatorBundle) LteDate(kf kendoparser.Filter) interface{} {
str := cast.ToString(kf.Value)
if t, ok := kf.Value.(time.Time); ok { // fixing time.Time value not valid converted :|
str = t.Format(time.RFC3339)
}
dtVariable, _ := time.Parse(time.RFC3339, str)
return bson.M{kf.Field: bson.M{"$lte": dtVariable}}
}
func (o *OperatorBundle) Exists(kf kendoparser.Filter) interface{} {
return bson.M{kf.Field: bson.M{"$exists": helper.StringToBool(cast.ToString(kf.Value), false)}}
}
// filter for time.Time / DateTime in Mongo exists or not (usefull for safe-delete flag)
func (o *OperatorBundle) DateExists(kf kendoparser.Filter) interface{} {
if strings.ToLower(cast.ToString(kf.Value)) == "true" {
return bson.M{"$and": bson.A{
bson.M{kf.Field: bson.M{"$exists": true}},
bson.M{kf.Field: bson.M{"$ne": time.Time{}}},
}}
}
return bson.M{"$or": bson.A{
bson.M{kf.Field: bson.M{"$exists": false}},
bson.M{kf.Field: bson.M{"$eq": time.Time{}}},
}}
}
func (o *OperatorBundle) Between(kf kendoparser.Filter) interface{} {
var v0, v1 interface{}
if len(kf.Values) > 0 {
v0 = kf.Values[0]
}
if len(kf.Values) > 1 {
v1 = kf.Values[1]
}
return bson.M{kf.Field: bson.M{"$gte": v0, "$lte": v1}}
}
// RegexContains Generate bson.RegEx for contains
func RegexContains(value string, ignoreCase bool) primitive.Regex {
value = regexp.QuoteMeta(value)
if ignoreCase {
return primitive.Regex{Pattern: "" + strings.ToLower(value) + "", Options: "i"}
} else {
return primitive.Regex{Pattern: "" + value + "", Options: ""}
}
}