-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MethodReferenceNode.java
404 lines (361 loc) · 17.3 KB
/
MethodReferenceNode.java
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.spel;
import static org.springframework.data.mongodb.core.spel.MethodReferenceNode.AggregationMethodReference.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.ast.MethodReference;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* An {@link ExpressionNode} representing a method reference.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Sebastien Gerard
* @author Christoph Strobl
* @author Mark Paluch
*/
public class MethodReferenceNode extends ExpressionNode {
private static final Map<String, AggregationMethodReference> FUNCTIONS;
static {
Map<String, AggregationMethodReference> map = new HashMap<String, AggregationMethodReference>();
// BOOLEAN OPERATORS
map.put("and", arrayArgRef().forOperator("$and"));
map.put("or", arrayArgRef().forOperator("$or"));
map.put("not", arrayArgRef().forOperator("$not"));
// SET OPERATORS
map.put("setEquals", arrayArgRef().forOperator("$setEquals"));
map.put("setIntersection", arrayArgRef().forOperator("$setIntersection"));
map.put("setUnion", arrayArgRef().forOperator("$setUnion"));
map.put("setDifference", arrayArgRef().forOperator("$setDifference"));
// 2nd.
map.put("setIsSubset", arrayArgRef().forOperator("$setIsSubset"));
map.put("anyElementTrue", arrayArgRef().forOperator("$anyElementTrue"));
map.put("allElementsTrue", arrayArgRef().forOperator("$allElementsTrue"));
// COMPARISON OPERATORS
map.put("cmp", arrayArgRef().forOperator("$cmp"));
map.put("eq", arrayArgRef().forOperator("$eq"));
map.put("gt", arrayArgRef().forOperator("$gt"));
map.put("gte", arrayArgRef().forOperator("$gte"));
map.put("lt", arrayArgRef().forOperator("$lt"));
map.put("lte", arrayArgRef().forOperator("$lte"));
map.put("ne", arrayArgRef().forOperator("$ne"));
// DOCUMENT OPERATORS
map.put("rank", emptyRef().forOperator("$rank"));
map.put("denseRank", emptyRef().forOperator("$denseRank"));
map.put("documentNumber", emptyRef().forOperator("$documentNumber"));
map.put("shift", mapArgRef().forOperator("$shift").mappingParametersTo("output", "by", "default"));
// ARITHMETIC OPERATORS
map.put("abs", singleArgRef().forOperator("$abs"));
map.put("add", arrayArgRef().forOperator("$add"));
map.put("ceil", singleArgRef().forOperator("$ceil"));
map.put("divide", arrayArgRef().forOperator("$divide"));
map.put("exp", singleArgRef().forOperator("$exp"));
map.put("floor", singleArgRef().forOperator("$floor"));
map.put("ln", singleArgRef().forOperator("$ln"));
map.put("log", arrayArgRef().forOperator("$log"));
map.put("log10", singleArgRef().forOperator("$log10"));
map.put("mod", arrayArgRef().forOperator("$mod"));
map.put("multiply", arrayArgRef().forOperator("$multiply"));
map.put("pow", arrayArgRef().forOperator("$pow"));
map.put("sqrt", singleArgRef().forOperator("$sqrt"));
map.put("subtract", arrayArgRef().forOperator("$subtract"));
map.put("trunc", singleArgRef().forOperator("$trunc"));
map.put("round", arrayArgRef().forOperator("$round"));
map.put("derivative", mapArgRef().forOperator("$derivative").mappingParametersTo("input", "unit"));
map.put("integral", mapArgRef().forOperator("$integral").mappingParametersTo("input", "unit"));
map.put("sin", singleArgRef().forOperator("$sin"));
map.put("sinh", singleArgRef().forOperator("$sinh"));
map.put("asin", singleArgRef().forOperator("$asin"));
map.put("asinh", singleArgRef().forOperator("$asinh"));
map.put("cos", singleArgRef().forOperator("$cos"));
map.put("cosh", singleArgRef().forOperator("$cosh"));
map.put("acos", singleArgRef().forOperator("$acos"));
map.put("acosh", singleArgRef().forOperator("$acosh"));
map.put("tan", singleArgRef().forOperator("$tan"));
map.put("tanh", singleArgRef().forOperator("$tanh"));
map.put("rand", emptyRef().forOperator("$rand"));
map.put("atan", singleArgRef().forOperator("$atan"));
map.put("atan2", arrayArgRef().forOperator("$atan2"));
map.put("atanh", singleArgRef().forOperator("$atanh"));
// STRING OPERATORS
map.put("concat", arrayArgRef().forOperator("$concat"));
map.put("strcasecmp", arrayArgRef().forOperator("$strcasecmp"));
map.put("substr", arrayArgRef().forOperator("$substr"));
map.put("toLower", singleArgRef().forOperator("$toLower"));
map.put("toUpper", singleArgRef().forOperator("$toUpper"));
map.put("indexOfBytes", arrayArgRef().forOperator("$indexOfBytes"));
map.put("indexOfCP", arrayArgRef().forOperator("$indexOfCP"));
map.put("split", arrayArgRef().forOperator("$split"));
map.put("strLenBytes", singleArgRef().forOperator("$strLenBytes"));
map.put("strLenCP", singleArgRef().forOperator("$strLenCP"));
map.put("substrCP", arrayArgRef().forOperator("$substrCP"));
map.put("trim", mapArgRef().forOperator("$trim").mappingParametersTo("input", "chars"));
map.put("ltrim", mapArgRef().forOperator("$ltrim").mappingParametersTo("input", "chars"));
map.put("rtrim", mapArgRef().forOperator("$rtrim").mappingParametersTo("input", "chars"));
map.put("regexFind", mapArgRef().forOperator("$regexFind").mappingParametersTo("input", "regex", "options"));
map.put("regexFindAll", mapArgRef().forOperator("$regexFindAll").mappingParametersTo("input", "regex", "options"));
map.put("regexMatch", mapArgRef().forOperator("$regexMatch").mappingParametersTo("input", "regex", "options"));
map.put("replaceOne", mapArgRef().forOperator("$replaceOne").mappingParametersTo("input", "find", "replacement"));
map.put("replaceAll", mapArgRef().forOperator("$replaceAll").mappingParametersTo("input", "find", "replacement"));
// TEXT SEARCH OPERATORS
map.put("meta", singleArgRef().forOperator("$meta"));
// ARRAY OPERATORS
map.put("arrayElemAt", arrayArgRef().forOperator("$arrayElemAt"));
map.put("concatArrays", arrayArgRef().forOperator("$concatArrays"));
map.put("filter", mapArgRef().forOperator("$filter") //
.mappingParametersTo("input", "as", "cond"));
map.put("first", singleArgRef().forOperator("$first"));
map.put("isArray", singleArgRef().forOperator("$isArray"));
map.put("last", singleArgRef().forOperator("$last"));
map.put("size", singleArgRef().forOperator("$size"));
map.put("slice", arrayArgRef().forOperator("$slice"));
map.put("sortArray", mapArgRef().forOperator("$sortArray").mappingParametersTo("input", "sortBy"));
map.put("reverseArray", singleArgRef().forOperator("$reverseArray"));
map.put("reduce", mapArgRef().forOperator("$reduce").mappingParametersTo("input", "initialValue", "in"));
map.put("zip", mapArgRef().forOperator("$zip").mappingParametersTo("inputs", "useLongestLength", "defaults"));
map.put("in", arrayArgRef().forOperator("$in"));
map.put("arrayToObject", singleArgRef().forOperator("$arrayToObject"));
map.put("indexOfArray", arrayArgRef().forOperator("$indexOfArray"));
map.put("range", arrayArgRef().forOperator("$range"));
// VARIABLE OPERATORS
map.put("map", mapArgRef().forOperator("$map") //
.mappingParametersTo("input", "as", "in"));
map.put("let", mapArgRef().forOperator("$let").mappingParametersTo("vars", "in"));
// LITERAL OPERATORS
map.put("literal", singleArgRef().forOperator("$literal"));
// DATE OPERATORS
map.put("dateAdd",
mapArgRef().forOperator("$dateAdd").mappingParametersTo("startDate", "unit", "amount", "timezone"));
map.put("dateSubtract",
mapArgRef().forOperator("$dateSubtract").mappingParametersTo("startDate", "unit", "amount", "timezone"));
map.put("dateDiff", mapArgRef().forOperator("$dateDiff").mappingParametersTo("startDate", "endDate", "unit",
"timezone", "startOfWeek"));
map.put("dateTrunc", mapArgRef().forOperator("$dateTrunc").mappingParametersTo("date", "unit", "binSize",
"startOfWeek", "timezone"));
map.put("dayOfYear", singleArgRef().forOperator("$dayOfYear"));
map.put("dayOfMonth", singleArgRef().forOperator("$dayOfMonth"));
map.put("dayOfWeek", singleArgRef().forOperator("$dayOfWeek"));
map.put("year", singleArgRef().forOperator("$year"));
map.put("month", singleArgRef().forOperator("$month"));
map.put("week", singleArgRef().forOperator("$week"));
map.put("hour", singleArgRef().forOperator("$hour"));
map.put("minute", singleArgRef().forOperator("$minute"));
map.put("second", singleArgRef().forOperator("$second"));
map.put("millisecond", singleArgRef().forOperator("$millisecond"));
map.put("dateToString", mapArgRef().forOperator("$dateToString") //
.mappingParametersTo("format", "date"));
map.put("dateFromString", mapArgRef().forOperator("$dateFromString") //
.mappingParametersTo("dateString", "format", "timezone", "onError", "onNull"));
map.put("dateFromParts", mapArgRef().forOperator("$dateFromParts").mappingParametersTo("year", "month", "day",
"hour", "minute", "second", "millisecond", "timezone"));
map.put("isoDateFromParts", mapArgRef().forOperator("$dateFromParts").mappingParametersTo("isoWeekYear", "isoWeek",
"isoDayOfWeek", "hour", "minute", "second", "millisecond", "timezone"));
map.put("dateToParts", mapArgRef().forOperator("$dateToParts") //
.mappingParametersTo("date", "timezone", "iso8601"));
map.put("isoDayOfWeek", singleArgRef().forOperator("$isoDayOfWeek"));
map.put("isoWeek", singleArgRef().forOperator("$isoWeek"));
map.put("isoWeekYear", singleArgRef().forOperator("$isoWeekYear"));
map.put("tsIncrement", singleArgRef().forOperator("$tsIncrement"));
// CONDITIONAL OPERATORS
map.put("cond", mapArgRef().forOperator("$cond") //
.mappingParametersTo("if", "then", "else"));
map.put("ifNull", arrayArgRef().forOperator("$ifNull"));
// GROUP OPERATORS
map.put("sum", arrayArgRef().forOperator("$sum"));
map.put("avg", arrayArgRef().forOperator("$avg"));
map.put("first", singleArgRef().forOperator("$first"));
map.put("last", singleArgRef().forOperator("$last"));
map.put("max", arrayArgRef().forOperator("$max"));
map.put("min", arrayArgRef().forOperator("$min"));
map.put("push", singleArgRef().forOperator("$push"));
map.put("addToSet", singleArgRef().forOperator("$addToSet"));
map.put("stdDevPop", arrayArgRef().forOperator("$stdDevPop"));
map.put("stdDevSamp", arrayArgRef().forOperator("$stdDevSamp"));
map.put("covariancePop", arrayArgRef().forOperator("$covariancePop"));
map.put("covarianceSamp", arrayArgRef().forOperator("$covarianceSamp"));
map.put("bottom", mapArgRef().forOperator("$bottom") //
.mappingParametersTo("output", "sortBy"));
map.put("bottomN", mapArgRef().forOperator("$bottomN") //
.mappingParametersTo("n", "output", "sortBy"));
map.put("firstN", mapArgRef().forOperator("$firstN") //
.mappingParametersTo("n", "input"));
map.put("lastN", mapArgRef().forOperator("$lastN") //
.mappingParametersTo("n", "input"));
map.put("top", mapArgRef().forOperator("$top") //
.mappingParametersTo("output", "sortBy"));
map.put("topN", mapArgRef().forOperator("$topN") //
.mappingParametersTo("n", "output", "sortBy"));
map.put("maxN", mapArgRef().forOperator("$maxN") //
.mappingParametersTo("n", "input"));
map.put("minN", mapArgRef().forOperator("$minN") //
.mappingParametersTo("n", "input"));
// TYPE OPERATORS
map.put("type", singleArgRef().forOperator("$type"));
// OBJECT OPERATORS
map.put("objectToArray", singleArgRef().forOperator("$objectToArray"));
map.put("mergeObjects", arrayArgRef().forOperator("$mergeObjects"));
map.put("getField", mapArgRef().forOperator("$getField").mappingParametersTo("field", "input"));
map.put("setField", mapArgRef().forOperator("$setField").mappingParametersTo("field", "value", "input"));
// CONVERT OPERATORS
map.put("convert", mapArgRef().forOperator("$convert") //
.mappingParametersTo("input", "to", "onError", "onNull"));
map.put("toBool", singleArgRef().forOperator("$toBool"));
map.put("toDate", singleArgRef().forOperator("$toDate"));
map.put("toDecimal", singleArgRef().forOperator("$toDecimal"));
map.put("toDouble", singleArgRef().forOperator("$toDouble"));
map.put("toInt", singleArgRef().forOperator("$toInt"));
map.put("toLong", singleArgRef().forOperator("$toLong"));
map.put("toObjectId", singleArgRef().forOperator("$toObjectId"));
map.put("toString", singleArgRef().forOperator("$toString"));
map.put("degreesToRadians", singleArgRef().forOperator("$degreesToRadians"));
FUNCTIONS = Collections.unmodifiableMap(map);
}
MethodReferenceNode(MethodReference reference, ExpressionState state) {
super(reference, state);
}
/**
* Return the {@link AggregationMethodReference}.
*
* @return can be {@literal null}.
* @since 1.10
*/
@Nullable
public AggregationMethodReference getMethodReference() {
String name = getName();
String methodName = name.substring(0, name.indexOf('('));
return FUNCTIONS.get(methodName);
}
/**
* @author Christoph Strobl
* @since 1.10
*/
public static final class AggregationMethodReference {
private final @Nullable String mongoOperator;
private final @Nullable ArgumentType argumentType;
private final @Nullable String[] argumentMap;
/**
* Creates new {@link AggregationMethodReference}.
*
* @param mongoOperator can be {@literal null}.
* @param argumentType can be {@literal null}.
* @param argumentMap can be {@literal null}.
*/
private AggregationMethodReference(@Nullable String mongoOperator, @Nullable ArgumentType argumentType,
@Nullable String[] argumentMap) {
this.mongoOperator = mongoOperator;
this.argumentType = argumentType;
this.argumentMap = argumentMap;
}
/**
* Get the MongoDB specific operator.
*
* @return can be {@literal null}.
*/
@Nullable
public String getMongoOperator() {
return this.mongoOperator;
}
/**
* Get the {@link ArgumentType} used by the MongoDB.
*
* @return never {@literal null}.
*/
@Nullable
public ArgumentType getArgumentType() {
return this.argumentType;
}
/**
* Get the property names in order order of appearance in resulting operation.
*
* @return never {@literal null}.
*/
public String[] getArgumentMap() {
return argumentMap != null ? argumentMap : new String[] {};
}
/**
* Create a new {@link AggregationMethodReference} for a {@link ArgumentType#SINGLE} argument.
*
* @return never {@literal null}.
*/
static AggregationMethodReference singleArgRef() {
return new AggregationMethodReference(null, ArgumentType.SINGLE, null);
}
/**
* Create a new {@link AggregationMethodReference} for an {@link ArgumentType#ARRAY} argument.
*
* @return never {@literal null}.
*/
static AggregationMethodReference arrayArgRef() {
return new AggregationMethodReference(null, ArgumentType.ARRAY, null);
}
/**
* Create a new {@link AggregationMethodReference} for a {@link ArgumentType#MAP} argument.
*
* @return never {@literal null}.
*/
static AggregationMethodReference mapArgRef() {
return new AggregationMethodReference(null, ArgumentType.MAP, null);
}
/**
* Create a new {@link AggregationMethodReference} for a {@link ArgumentType#EMPTY_DOCUMENT} argument.
*
* @return never {@literal null}.
* @since 3.3
*/
static AggregationMethodReference emptyRef() {
return new AggregationMethodReference(null, ArgumentType.EMPTY_DOCUMENT, null);
}
/**
* Create a new {@link AggregationMethodReference} for a given {@literal aggregationExpressionOperator} reusing
* previously set arguments.
*
* @param aggregationExpressionOperator should not be {@literal null}.
* @return never {@literal null}.
*/
AggregationMethodReference forOperator(String aggregationExpressionOperator) {
return new AggregationMethodReference(aggregationExpressionOperator, argumentType, argumentMap);
}
/**
* Create a new {@link AggregationMethodReference} for mapping actual parameters within the AST to the given
* {@literal aggregationExpressionProperties} reusing previously set arguments. <br />
* <strong>NOTE:</strong> Can only be applied to {@link AggregationMethodReference} of type
* {@link ArgumentType#MAP}.
*
* @param aggregationExpressionProperties should not be {@literal null}.
* @return never {@literal null}.
* @throws IllegalArgumentException
*/
AggregationMethodReference mappingParametersTo(String... aggregationExpressionProperties) {
Assert.isTrue(ObjectUtils.nullSafeEquals(argumentType, ArgumentType.MAP),
"Parameter mapping can only be applied to AggregationMethodReference with MAPPED ArgumentType");
return new AggregationMethodReference(mongoOperator, argumentType, aggregationExpressionProperties);
}
/**
* The actual argument type to use when mapping parameters to MongoDB specific format.
*
* @author Christoph Strobl
* @since 1.10
*/
public enum ArgumentType {
SINGLE, ARRAY, MAP, EMPTY_DOCUMENT
}
}
}