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 pathkendo.go
93 lines (81 loc) · 1.93 KB
/
kendo.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
// helper parser for dboxpipe
package kpdboxpipe
import (
"strings"
"github.com/eaciit/toolkit"
"github.com/raditzlawliet/kendoparser"
"gopkg.in/mgo.v2/bson"
)
// Parser Parser
// type Parser struct{}
// FilterParser convert Filter into M for pipe combination automaticly
// return can @Nullable if filter and filters empty
func FilterParser(kf *kendoparser.Filter) interface{} {
// defaultFilter := toolkit.M{"_id": toolkit.M{"$exists": true}}
if len(kf.Filters) == 0 {
// processing will use copy instead to avoid change original value
ckFilter := *kf
// very customable handler
if kf.AdditionalParsers() != nil {
for _, handler := range kf.AdditionalParsers() {
if r := handler(&ckFilter); r != nil {
return r
}
}
}
// (scoped)
if om := kf.GetOperatorManager(); om != nil {
if opHandler, ok := om.OperatorFilters[kf.Operator]; ok {
f := opHandler(ckFilter)
if f != nil {
return f.(toolkit.M)
}
return nil
}
}
// (global)
if opHandler, ok := OperatorManager.OperatorFilters[kf.Operator]; ok {
f := opHandler(ckFilter)
if f != nil {
return f.(toolkit.M)
}
return nil
}
// default (global)
f := OperatorManager.DefaultOperatorFilter(ckFilter)
if f != nil {
return f.(toolkit.M)
}
return nil
}
// so filters has some values
filters := []toolkit.M{}
for _, kFilterChild := range kf.Filters {
filter := FilterParser(&kFilterChild)
if filter != nil {
filters = append(filters, filter.(toolkit.M))
}
}
if len(filters) > 0 {
if strings.ToLower(kf.Logic) == "and" {
return toolkit.M{"$and": filters}
}
return toolkit.M{"$or": filters}
}
return nil
}
// SortParser SortParser
func SortParser(ksa *kendoparser.Sort) interface{} {
sorter := bson.D{}
for _, ks := range *ksa {
sort := 1
if strings.ToLower(ks.Dir) == "desc" {
sort = -1
}
sorter = append(sorter, bson.DocElem{
Name: ks.Field,
Value: sort,
})
}
return sorter
}