Move Expr operator one level up.

The Expr operator should be held within ExpressionOperators not its factory.

See #4139
Original pull request: #4182.
This commit is contained in:
Christoph Strobl
2022-09-26 10:04:43 +02:00
committed by Mark Paluch
parent 9217821472
commit 79f05c3d7f
2 changed files with 64 additions and 63 deletions

View File

@@ -92,68 +92,8 @@ public class EvaluationOperators {
* @return new instance of {@link Expr}.
*/
public LastObservationCarriedForward locf() {
return usesFieldRef() ? LastObservationCarriedForward.locfValueOf(fieldReference) : LastObservationCarriedForward.locfValueOf(expression);
}
/**
* Allows the use of aggregation expressions within the query language.
*/
public static class Expr extends AbstractAggregationExpression {
private Expr(Object value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$expr";
}
/**
* Creates new {@link Expr}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link Expr}.
*/
public static Expr valueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new Expr(Fields.field(fieldReference));
}
/**
* Creates new {@link Expr}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Expr}.
*/
public static Expr valueOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new Expr(expression);
}
/**
* Creates {@code $expr} as {@link CriteriaDefinition}.
*
* @return the {@link CriteriaDefinition} from this expression.
*/
public CriteriaDefinition toCriteriaDefinition(AggregationOperationContext context) {
Document criteriaObject = toDocument(context);
return new CriteriaDefinition() {
@Override
public Document getCriteriaObject() {
return criteriaObject;
}
@Override
public String getKey() {
return getMongoMethod();
}
};
}
return usesFieldRef() ? LastObservationCarriedForward.locfValueOf(fieldReference)
: LastObservationCarriedForward.locfValueOf(expression);
}
private boolean usesFieldRef() {
@@ -161,6 +101,67 @@ public class EvaluationOperators {
}
}
/**
* Allows the use of aggregation expressions within the query language.
*/
public static class Expr extends AbstractAggregationExpression {
private Expr(Object value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$expr";
}
/**
* Creates new {@link Expr}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link Expr}.
*/
public static Expr valueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new Expr(Fields.field(fieldReference));
}
/**
* Creates new {@link Expr}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Expr}.
*/
public static Expr valueOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new Expr(expression);
}
/**
* Creates {@code $expr} as {@link CriteriaDefinition}.
*
* @return the {@link CriteriaDefinition} from this expression.
*/
public CriteriaDefinition toCriteriaDefinition(AggregationOperationContext context) {
Document criteriaObject = toDocument(context);
return new CriteriaDefinition() {
@Override
public Document getCriteriaObject() {
return criteriaObject;
}
@Override
public String getKey() {
return getMongoMethod();
}
};
}
}
/**
* Sets {@literal null} and missing values to the last non-null value.
*

View File

@@ -1416,7 +1416,7 @@ public class QueryMapperUnitTests {
@Test // GH-3790
void shouldAcceptExprAsCriteriaDefinition() {
EvaluationOperators.EvaluationOperatorFactory.Expr expr = EvaluationOperators
EvaluationOperators.Expr expr = EvaluationOperators
.valueOf(ConditionalOperators.ifNull("customizedField").then(true)).expr();
Query query = query(