-
Notifications
You must be signed in to change notification settings - Fork 2
/
predicates.go
135 lines (111 loc) · 2.76 KB
/
predicates.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
package plusminus
import (
"fmt"
"strings"
)
// Predicate defines a predicate to return from a node.
func Predicate(name string) *predicate {
return &predicate{
name: name,
}
}
type predicateList []*predicate
func (p predicateList) toString() string {
var predicatesGraphQLpm string
for i := 0; i < len(p); i++ {
predicatesGraphQLpm += p[i].toString() + "\n"
}
return "{\n" + predicatesGraphQLpm + "}"
}
type predicate struct {
name string
normalize bool
filter expr
first interface{}
offset interface{}
facets bool
facetNames []string
facetFilter expr
predicates predicateList
}
// Normalize will toggle on normalization of predicate and facet results.
// Only predicates and facets with an alias will be returned.
func (p *predicate) Normalize() *predicate {
p.normalize = true
return p
}
// Predicates allows you to add a number of predicates to the current returned node.
// Predicates can be nested.
func (p *predicate) Predicates(preds ...*predicate) *predicate {
p.predicates = append(p.predicates, preds...)
return p
}
func (p *predicate) Filter(filter expr) *predicate {
p.filter = filter
return p
}
func (p *predicate) First(val interface{}) *predicate {
p.first = val
return p
}
func (p *predicate) Offset(val interface{}) *predicate {
p.offset = val
return p
}
func (p *predicate) Facets(names ...string) *predicate {
p.facets = true
p.facetNames = names
return p
}
func (p *predicate) FacetFilter(filter expr) *predicate {
p.facets = true
p.facetFilter = filter
return p
}
func (p *predicate) toString() string {
s := p.name
if p.first != nil || p.offset != nil {
s += "("
var pagination []string
if p.first != nil {
switch p.first.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
pagination = append(pagination, fmt.Sprintf("first: %d", p.first))
default:
pagination = append(pagination, fmt.Sprintf("first: %s", p.first))
}
}
if p.offset != nil {
switch p.offset.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
pagination = append(pagination, fmt.Sprintf("offset: %d", p.offset))
default:
pagination = append(pagination, fmt.Sprintf("offset: %s", p.offset))
}
}
s += strings.Join(pagination, ", ")
s += ")"
}
if p.filter != nil {
s += " @filter(" + p.filter.toString() + ")"
}
if p.facets {
if len(p.facetNames) == 0 && p.facetFilter == nil {
s += " @facets"
} else {
if p.facetFilter != nil {
s += " @facets(" + p.facetFilter.toString() + ")"
}
if len(p.facetNames) > 0 {
s += " @facets(" + strings.Join(p.facetNames, ", ") + ")"
}
}
}
if p.normalize {
s += " @normalize"
}
if len(p.predicates) > 0 {
s += " " + p.predicates.toString()
}
return s
}