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
/
operator.go
100 lines (82 loc) · 3.33 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
package kpdboxpipe
import (
"regexp"
"strings"
"time"
"github.com/eaciit/toolkit"
"github.com/raditzlawliet/kendoparser"
"github.com/raditzlawliet/kendoparser/helper"
"gopkg.in/mgo.v2/bson"
"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.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.Gte, "gte")
OperatorManager.RegisterOperator(Operator.Lte, "lte")
OperatorManager.RegisterOperator(Operator.GteDate, "gtedate")
OperatorManager.RegisterOperator(Operator.LteDate, "ltedate")
OperatorManager.RegisterOperator(Operator.Exists, "exist", "exists")
OperatorManager.RegisterOperator(Operator.Between, "between")
}
func (o *OperatorBundle) Equal(kf kendoparser.Filter) interface{} {
if kf.IgnoreCase {
value := regexp.QuoteMeta(cast.ToString(kf.Value))
return toolkit.M{kf.Field: bson.RegEx{Pattern: "^" + strings.ToLower(value) + "$", Options: "i"}}
}
return toolkit.M{kf.Field: toolkit.M{"$eq": kf.Value}}
}
func (o *OperatorBundle) NotEqual(kf kendoparser.Filter) interface{} {
return toolkit.M{kf.Field: toolkit.M{"$ne": kf.Value}}
}
func (o *OperatorBundle) Contain(kf kendoparser.Filter) interface{} {
return toolkit.M{kf.Field: helper.RegexContains(cast.ToString(kf.Value), kf.IgnoreCase)}
}
func (o *OperatorBundle) NotContain(kf kendoparser.Filter) interface{} {
return toolkit.M{kf.Field: toolkit.M{"$ne": helper.RegexContains(cast.ToString(kf.Value), kf.IgnoreCase)}}
}
func (o *OperatorBundle) In(kf kendoparser.Filter) interface{} {
return toolkit.M{kf.Field: toolkit.M{"$in": kf.Values}}
}
func (o *OperatorBundle) Gte(kf kendoparser.Filter) interface{} {
return toolkit.M{kf.Field: toolkit.M{"$gte": kf.Value}}
}
func (o *OperatorBundle) Lte(kf kendoparser.Filter) interface{} {
return toolkit.M{kf.Field: toolkit.M{"$lte": kf.Value}}
}
func (o *OperatorBundle) GteDate(kf kendoparser.Filter) interface{} {
dtVariable, _ := time.Parse(time.RFC3339, cast.ToString(kf.Value))
return toolkit.M{kf.Field: toolkit.M{"$gte": dtVariable}}
}
func (o *OperatorBundle) LteDate(kf kendoparser.Filter) interface{} {
dtVariable, _ := time.Parse(time.RFC3339, cast.ToString(kf.Value))
return toolkit.M{kf.Field: toolkit.M{"$lte": dtVariable}}
}
func (o *OperatorBundle) Exists(kf kendoparser.Filter) interface{} {
return toolkit.M{kf.Field: toolkit.M{"$exists": helper.StringToBool(cast.ToString(kf.Value), false)}}
}
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 toolkit.M{kf.Field: toolkit.M{"$gte": v0, "$lte": v1}}
}