-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastate.go
97 lines (79 loc) · 1.84 KB
/
datastate.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
package kendo
import (
"fmt"
"net/url"
"strings"
"github.com/globalsign/mgo/bson"
)
type SortDescriptor struct {
Dir string // asc desc
Field string
}
type AggregateDescriptor struct {
Aggregate string //"count" | "sum" | "average" | "min" | "max"
Field string
}
func (ad AggregateDescriptor) getExpression(isRoot bool) string {
expression := fmt.Sprintf("$items.aggregates.%s.%s", ad.getKey(), ad.Aggregate)
if isRoot {
expression = fmt.Sprintf("$items.%s", ad.Field)
}
return expression
}
func (ad AggregateDescriptor) getAggregate() string {
key := ad.Aggregate
if key == "average" {
key = "avg"
}
return fmt.Sprintf("$%s", key)
}
func (ad AggregateDescriptor) getKey() string {
return sanitizeKey(ad.Field)
}
type GroupDescriptor struct {
Aggregates []AggregateDescriptor
Dir string // asc desc
Field string
}
func (gd GroupDescriptor) getKey() string {
return sanitizeKey(gd.Field)
}
func (gd GroupDescriptor) getSort() int {
sort := 1
if gd.Dir == "desc" {
sort = -1
}
return sort
}
type FilterDescriptor struct {
Field string
IgnoreCase bool
Operator string
Value interface{}
}
type CompositeFilterDescriptor struct {
Logic string // or and
Filters []FilterDescriptor //could also be a CompositeFilterDescriptor
}
type LookupDescriptor struct { // TODO add toMongo method
From string
LocalField string
ForeignField string
As string
Single bool
}
type DataState struct {
Page int
PageSize int
Filter CompositeFilterDescriptor
Group []GroupDescriptor
Sort []SortDescriptor
Lookup []LookupDescriptor
Aggregates []AggregateDescriptor
values url.Values
replacements map[string]string
preprocessing []bson.M
}
func sanitizeKey(s string) string {
return strings.Replace(s, ".", "", -1)
}