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
This commit is contained in:
Christoph Strobl
2022-09-20 14:22:07 +02:00
committed by Mark Paluch
parent 68ab74a5bf
commit a63db5586c
7 changed files with 119 additions and 50 deletions

View File

@@ -24,7 +24,6 @@ import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

View File

@@ -35,7 +35,6 @@ import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.SerializationUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -766,48 +765,4 @@ public class Aggregation {
public String toString() {
return SerializationUtils.serializeToJsonSafely(toDocument("__collection__", DEFAULT_CONTEXT));
}
/**
* Describes the system variables available in MongoDB aggregation framework pipeline expressions.
*
* @author Thomas Darimont
* @author Christoph Strobl
* @see <a href="https://docs.mongodb.com/manual/reference/aggregation-variables">Aggregation Variables</a>.
*/
enum SystemVariable {
ROOT, CURRENT, REMOVE;
private static final String PREFIX = "$$";
/**
* Return {@literal true} if the given {@code fieldRef} denotes a well-known system variable, {@literal false}
* otherwise.
*
* @param fieldRef may be {@literal null}.
* @return {@literal true} if the given field refers to a {@link SystemVariable}.
*/
public static boolean isReferingToSystemVariable(@Nullable String fieldRef) {
if (fieldRef == null || !fieldRef.startsWith(PREFIX) || fieldRef.length() <= 2) {
return false;
}
int indexOfFirstDot = fieldRef.indexOf('.');
String candidate = fieldRef.substring(2, indexOfFirstDot == -1 ? fieldRef.length() : indexOfFirstDot);
for (SystemVariable value : values()) {
if (value.name().equals(candidate)) {
return true;
}
}
return false;
}
@Override
public String toString() {
return PREFIX.concat(name());
}
}
}

View File

@@ -245,7 +245,7 @@ public final class Fields implements Iterable<Field> {
private static String cleanUp(String source) {
if (Aggregation.SystemVariable.isReferingToSystemVariable(source)) {
if (SystemVariable.isReferingToSystemVariable(source)) {
return source;
}

View File

@@ -510,7 +510,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
return value;
}
if (Aggregation.SystemVariable.isReferingToSystemVariable(reference)) {
if (SystemVariable.isReferingToSystemVariable(reference)) {
return reference;
}

View File

@@ -1395,7 +1395,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
// implicit reference or explicit include?
if (value == null || Boolean.TRUE.equals(value)) {
if (Aggregation.SystemVariable.isReferingToSystemVariable(field.getTarget())) {
if (SystemVariable.isReferingToSystemVariable(field.getTarget())) {
return field.getTarget();
}

View File

@@ -0,0 +1,116 @@
/*
* Copyright 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
*
* http://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.aggregation;
import org.springframework.lang.Nullable;
/**
* Describes the system variables available in MongoDB aggregation framework pipeline expressions.
*
* @author Thomas Darimont
* @author Christoph Strobl
* @see <a href="https://docs.mongodb.com/manual/reference/aggregation-variables">Aggregation Variables</a>.
*/
public enum SystemVariable {
/**
* Variable for the current datetime.
*
* @since 4.0
*/
NOW,
/**
* Variable for the current timestamp.
*
* @since 4.0
*/
CLUSTER_TIME,
/**
* Variable that references the root document.
*/
ROOT,
/**
* Variable that references the start of the field path being processed.
*/
CURRENT,
/**
* Variable that evaluates to a missing value.
*/
REMOVE,
/**
* One of the allowed results of a {@literal $redact} expression
*
* @since 4.0
*/
DESCEND,
/**
* One of the allowed results of a {@literal $redact} expression
*
* @since 4.0
*/
PRUNE,
/**
* One of the allowed results of a {@literal $redact} expression
*
* @since 4.0
*/
KEEP,
/**
* A variable that stores the metadata results of an Atlas Search query.
*
* @since 4.0
*/
SEARCH_META;
private static final String PREFIX = "$$";
/**
* Return {@literal true} if the given {@code fieldRef} denotes a well-known system variable, {@literal false}
* otherwise.
*
* @param fieldRef may be {@literal null}.
* @return {@literal true} if the given field refers to a {@link SystemVariable}.
*/
public static boolean isReferingToSystemVariable(@Nullable String fieldRef) {
if (fieldRef == null || !fieldRef.startsWith(PREFIX) || fieldRef.length() <= 2) {
return false;
}
int indexOfFirstDot = fieldRef.indexOf('.');
String candidate = fieldRef.substring(2, indexOfFirstDot == -1 ? fieldRef.length() : indexOfFirstDot);
for (SystemVariable value : values()) {
if (value.name().equals(candidate)) {
return true;
}
}
return false;
}
@Override
public String toString() {
return PREFIX.concat(name());
}
}

View File

@@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.*;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable;
import org.springframework.data.mongodb.core.aggregation.ObjectOperators.MergeObjects;
/**