forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacet_token.go
228 lines (183 loc) · 6.29 KB
/
facet_token.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package ravendb
import "strings"
var _ queryToken = &facetToken{}
type facetToken struct {
facetSetupDocumentID string
aggregateByFieldName string
alias string
ranges []string
optionsParameterName string
aggregations []*facetAggregationToken
}
func (t *facetToken) GetName() string {
return firstNonEmptyString(t.alias, t.aggregateByFieldName)
}
func newFacetTokenWithID(facetSetupDocumentID string) *facetToken {
return &facetToken{
facetSetupDocumentID: facetSetupDocumentID,
}
}
func newFacetTokenAll(aggregateByFieldName string, alias string, ranges []string, optionsParameterName string) *facetToken {
return &facetToken{
aggregateByFieldName: aggregateByFieldName,
alias: alias,
ranges: ranges,
optionsParameterName: optionsParameterName,
}
}
func (t *facetToken) writeTo(writer *strings.Builder) error {
writer.WriteString("facet(")
if t.facetSetupDocumentID != "" {
writer.WriteString("id('")
writer.WriteString(t.facetSetupDocumentID)
writer.WriteString("'))")
return nil
}
firstArgument := false
if t.aggregateByFieldName != "" {
writer.WriteString(t.aggregateByFieldName)
} else if len(t.ranges) != 0 {
firstInRange := true
for _, rang := range t.ranges {
if !firstInRange {
writer.WriteString(", ")
}
firstInRange = false
writer.WriteString(rang)
}
} else {
firstArgument = true
}
for _, aggregation := range t.aggregations {
if !firstArgument {
writer.WriteString(", ")
}
firstArgument = false
if err := aggregation.writeTo(writer); err != nil {
return err
}
}
if stringIsNotBlank(t.optionsParameterName) {
writer.WriteString(", $")
writer.WriteString(t.optionsParameterName)
}
writer.WriteString(")")
if stringIsBlank(t.alias) || t.alias == t.aggregateByFieldName {
return nil
}
writer.WriteString(" as ")
writer.WriteString(t.alias)
return nil
}
func createFacetToken(facetSetupDocumentID string) (*facetToken, error) {
if stringIsBlank(facetSetupDocumentID) {
return nil, newIllegalArgumentError("facetSetupDocumentID cannot be null")
}
return newFacetTokenWithID(facetSetupDocumentID), nil
}
func createFacetTokenWithFacet(facet *Facet, addQueryParameter func(interface{}) string) *facetToken {
optionsParameterName := getOptionsParameterName(facet, addQueryParameter)
token := newFacetTokenAll(facet.FieldName, facet.DisplayFieldName, nil, optionsParameterName)
applyAggregations(facet, token)
return token
}
func createFacetTokenWithRangeFacet(facet *RangeFacet, addQueryParameter func(interface{}) string) (*facetToken, error) {
optionsParameterName := getOptionsParameterName(facet, addQueryParameter)
token := newFacetTokenAll("", facet.DisplayFieldName, facet.Ranges, optionsParameterName)
applyAggregations(facet, token)
return token, nil
}
func createFacetTokenWithGenericRangeFacet(facet *GenericRangeFacet, addQueryParameter func(interface{}) string) (*facetToken, error) {
optionsParameterName := getOptionsParameterName(facet, addQueryParameter)
var ranges []string
for _, rangeBuilder := range facet.Ranges {
r, err := genericRangeFacetParse(rangeBuilder, addQueryParameter)
if err != nil {
return nil, err
}
ranges = append(ranges, r)
}
token := newFacetTokenAll("", facet.DisplayFieldName, ranges, optionsParameterName)
applyAggregations(facet, token)
return token, nil
}
func createFacetTokenWithFacetBase(facet FacetBase, addQueryParameter func(interface{}) string) (*facetToken, error) {
// this is just a dispatcher
return facet.ToFacetToken(addQueryParameter)
}
func applyAggregations(facet FacetBase, token *facetToken) {
m := facet.GetAggregations()
for key, value := range m {
var aggregationToken *facetAggregationToken
switch key {
case FacetAggregationMax:
aggregationToken = facetAggregationTokenMax(value)
case FacetAggregationMin:
aggregationToken = facetAggregationTokenMin(value)
case FacetAggregationAverage:
aggregationToken = facetAggregationTokenAverage(value)
case FacetAggregationSum:
aggregationToken = facetAggregationTokenSum(value)
default:
panic("Unsupported aggregation method: " + key)
//throw new NotImplementedError("Unsupported aggregation method: " + aggregation.getKey());
}
token.aggregations = append(token.aggregations, aggregationToken)
}
}
func getOptionsParameterName(facet FacetBase, addQueryParameter func(interface{}) string) string {
if facet.GetOptions() == nil || facet.GetOptions() == DefaultFacetOptions {
return ""
}
return addQueryParameter(facet.GetOptions())
}
var _ queryToken = &facetAggregationToken{}
type facetAggregationToken struct {
fieldName string
aggregation FacetAggregation
}
func newFacetAggregationToken(fieldName string, aggregation FacetAggregation) *facetAggregationToken {
return &facetAggregationToken{
fieldName: fieldName,
aggregation: aggregation,
}
}
func (t *facetAggregationToken) writeTo(writer *strings.Builder) error {
switch t.aggregation {
case FacetAggregationMax:
writer.WriteString("max(")
writer.WriteString(t.fieldName)
writer.WriteString(")")
case FacetAggregationMin:
writer.WriteString("min(")
writer.WriteString(t.fieldName)
writer.WriteString(")")
case FacetAggregationAverage:
writer.WriteString("avg(")
writer.WriteString(t.fieldName)
writer.WriteString(")")
case FacetAggregationSum:
writer.WriteString("sum(")
writer.WriteString(t.fieldName)
writer.WriteString(")")
default:
return newIllegalArgumentError("Invalid aggregation mode: " + t.aggregation)
}
return nil
}
func facetAggregationTokenMax(fieldName string) *facetAggregationToken {
panicIf(stringIsBlank(fieldName), "FieldName can not be null")
return newFacetAggregationToken(fieldName, FacetAggregationMax)
}
func facetAggregationTokenMin(fieldName string) *facetAggregationToken {
panicIf(stringIsBlank(fieldName), "FieldName can not be null")
return newFacetAggregationToken(fieldName, FacetAggregationMin)
}
func facetAggregationTokenAverage(fieldName string) *facetAggregationToken {
panicIf(stringIsBlank(fieldName), "FieldName can not be null")
return newFacetAggregationToken(fieldName, FacetAggregationAverage)
}
func facetAggregationTokenSum(fieldName string) *facetAggregationToken {
panicIf(stringIsBlank(fieldName), "FieldName can not be null")
return newFacetAggregationToken(fieldName, FacetAggregationSum)
}