Skip to content

Commit a63db55

Browse files
christophstroblmp911de
authored andcommitted
Add missing aggregation system variables.
Move inner class SystemVariable to upper level and add missing values (NOW, CLUSTER_TIME, DECEND, PRUNE, KEEP & SEARCH_META) Original pull request: #4176. Closes #4145
1 parent 68ab74a commit a63db55

File tree

7 files changed

+119
-50
lines changed

7 files changed

+119
-50
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Map;
2525

2626
import org.bson.Document;
27-
import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable;
2827
import org.springframework.util.Assert;
2928
import org.springframework.util.ObjectUtils;
3029

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

-45
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
3636
import org.springframework.data.mongodb.core.query.NearQuery;
3737
import org.springframework.data.mongodb.core.query.SerializationUtils;
38-
import org.springframework.lang.Nullable;
3938
import org.springframework.util.Assert;
4039

4140
/**
@@ -766,48 +765,4 @@ public Document toDocument(String inputCollectionName, AggregationOperationConte
766765
public String toString() {
767766
return SerializationUtils.serializeToJsonSafely(toDocument("__collection__", DEFAULT_CONTEXT));
768767
}
769-
770-
/**
771-
* Describes the system variables available in MongoDB aggregation framework pipeline expressions.
772-
*
773-
* @author Thomas Darimont
774-
* @author Christoph Strobl
775-
* @see <a href="https://docs.mongodb.com/manual/reference/aggregation-variables">Aggregation Variables</a>.
776-
*/
777-
enum SystemVariable {
778-
779-
ROOT, CURRENT, REMOVE;
780-
781-
private static final String PREFIX = "$$";
782-
783-
/**
784-
* Return {@literal true} if the given {@code fieldRef} denotes a well-known system variable, {@literal false}
785-
* otherwise.
786-
*
787-
* @param fieldRef may be {@literal null}.
788-
* @return {@literal true} if the given field refers to a {@link SystemVariable}.
789-
*/
790-
public static boolean isReferingToSystemVariable(@Nullable String fieldRef) {
791-
792-
if (fieldRef == null || !fieldRef.startsWith(PREFIX) || fieldRef.length() <= 2) {
793-
return false;
794-
}
795-
796-
int indexOfFirstDot = fieldRef.indexOf('.');
797-
String candidate = fieldRef.substring(2, indexOfFirstDot == -1 ? fieldRef.length() : indexOfFirstDot);
798-
799-
for (SystemVariable value : values()) {
800-
if (value.name().equals(candidate)) {
801-
return true;
802-
}
803-
}
804-
805-
return false;
806-
}
807-
808-
@Override
809-
public String toString() {
810-
return PREFIX.concat(name());
811-
}
812-
}
813768
}

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public AggregationField(String name, @Nullable String target) {
245245

246246
private static String cleanUp(String source) {
247247

248-
if (Aggregation.SystemVariable.isReferingToSystemVariable(source)) {
248+
if (SystemVariable.isReferingToSystemVariable(source)) {
249249
return source;
250250
}
251251

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public Object getValue(AggregationOperationContext context) {
510510
return value;
511511
}
512512

513-
if (Aggregation.SystemVariable.isReferingToSystemVariable(reference)) {
513+
if (SystemVariable.isReferingToSystemVariable(reference)) {
514514
return reference;
515515
}
516516

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ private Object renderFieldValue(AggregationOperationContext context) {
13951395
// implicit reference or explicit include?
13961396
if (value == null || Boolean.TRUE.equals(value)) {
13971397

1398-
if (Aggregation.SystemVariable.isReferingToSystemVariable(field.getTarget())) {
1398+
if (SystemVariable.isReferingToSystemVariable(field.getTarget())) {
13991399
return field.getTarget();
14001400
}
14011401

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import org.springframework.lang.Nullable;
19+
20+
/**
21+
* Describes the system variables available in MongoDB aggregation framework pipeline expressions.
22+
*
23+
* @author Thomas Darimont
24+
* @author Christoph Strobl
25+
* @see <a href="https://docs.mongodb.com/manual/reference/aggregation-variables">Aggregation Variables</a>.
26+
*/
27+
public enum SystemVariable {
28+
29+
/**
30+
* Variable for the current datetime.
31+
*
32+
* @since 4.0
33+
*/
34+
NOW,
35+
36+
/**
37+
* Variable for the current timestamp.
38+
*
39+
* @since 4.0
40+
*/
41+
CLUSTER_TIME,
42+
43+
/**
44+
* Variable that references the root document.
45+
*/
46+
ROOT,
47+
48+
/**
49+
* Variable that references the start of the field path being processed.
50+
*/
51+
CURRENT,
52+
53+
/**
54+
* Variable that evaluates to a missing value.
55+
*/
56+
REMOVE,
57+
58+
/**
59+
* One of the allowed results of a {@literal $redact} expression
60+
*
61+
* @since 4.0
62+
*/
63+
DESCEND,
64+
65+
/**
66+
* One of the allowed results of a {@literal $redact} expression
67+
*
68+
* @since 4.0
69+
*/
70+
PRUNE,
71+
/**
72+
* One of the allowed results of a {@literal $redact} expression
73+
*
74+
* @since 4.0
75+
*/
76+
KEEP,
77+
78+
/**
79+
* A variable that stores the metadata results of an Atlas Search query.
80+
*
81+
* @since 4.0
82+
*/
83+
SEARCH_META;
84+
85+
private static final String PREFIX = "$$";
86+
87+
/**
88+
* Return {@literal true} if the given {@code fieldRef} denotes a well-known system variable, {@literal false}
89+
* otherwise.
90+
*
91+
* @param fieldRef may be {@literal null}.
92+
* @return {@literal true} if the given field refers to a {@link SystemVariable}.
93+
*/
94+
public static boolean isReferingToSystemVariable(@Nullable String fieldRef) {
95+
96+
if (fieldRef == null || !fieldRef.startsWith(PREFIX) || fieldRef.length() <= 2) {
97+
return false;
98+
}
99+
100+
int indexOfFirstDot = fieldRef.indexOf('.');
101+
String candidate = fieldRef.substring(2, indexOfFirstDot == -1 ? fieldRef.length() : indexOfFirstDot);
102+
103+
for (SystemVariable value : values()) {
104+
if (value.name().equals(candidate)) {
105+
return true;
106+
}
107+
}
108+
109+
return false;
110+
}
111+
112+
@Override
113+
public String toString() {
114+
return PREFIX.concat(name());
115+
}
116+
}

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ObjectOperatorsUnitTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.bson.Document;
2121
import org.junit.jupiter.api.Test;
22-
import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable;
2322
import org.springframework.data.mongodb.core.aggregation.ObjectOperators.MergeObjects;
2423

2524
/**

0 commit comments

Comments
 (0)