Add support for $degreesToRadians aggregation operator.
Closes: #3714 Original pull request: #3755.
This commit is contained in:
committed by
Mark Paluch
parent
2a3a4cf030
commit
ec16b873b7
@@ -231,6 +231,16 @@ public class ConvertOperators {
|
||||
return ToString.toString(valueObject());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link AggregationExpression} for {@code $degreesToRadians} that converts an input value measured in degrees to radians.\
|
||||
*
|
||||
* @return new instance of {@link DegreesToRadians}.
|
||||
* @since 3.3
|
||||
*/
|
||||
public DegreesToRadians convertDegreesToRadians() {
|
||||
return DegreesToRadians.degreesToRadians(valueObject());
|
||||
}
|
||||
|
||||
private Convert createConvert() {
|
||||
return usesFieldRef() ? Convert.convertValueOf(fieldReference) : Convert.convertValueOf(expression);
|
||||
}
|
||||
@@ -692,4 +702,52 @@ public class ConvertOperators {
|
||||
return "$toString";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link AggregationExpression} for {@code $degreesToRadians} that converts an input value measured in degrees to radians.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 3.3
|
||||
*/
|
||||
public static class DegreesToRadians extends AbstractAggregationExpression {
|
||||
|
||||
private DegreesToRadians(Object value) {
|
||||
super(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of {@link DegreesToRadians} that converts the value of the given field, measured in degrees, to radians.
|
||||
*
|
||||
* @param fieldName must not be {@literal null}.
|
||||
* @return new instance of {@link DegreesToRadians}.
|
||||
*/
|
||||
public static DegreesToRadians degreesToRadiansOf(String fieldName) {
|
||||
return degreesToRadians(Fields.field(fieldName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of {@link DegreesToRadians} that converts the result of the given {@link AggregationExpression expression}, measured in degrees, to radians.
|
||||
*
|
||||
* @param expression must not be {@literal null}.
|
||||
* @return new instance of {@link DegreesToRadians}.
|
||||
*/
|
||||
public static DegreesToRadians degreesToRadiansOf(AggregationExpression expression) {
|
||||
return degreesToRadians(expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of {@link DegreesToRadians} that converts the given value, measured in degrees, to radians.
|
||||
*
|
||||
* @param value must not be {@literal null}.
|
||||
* @return new instance of {@link DegreesToRadians}.
|
||||
*/
|
||||
public static DegreesToRadians degreesToRadians(Object value) {
|
||||
return new DegreesToRadians(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMongoMethod() {
|
||||
return "$degreesToRadians";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,6 +199,7 @@ public class MethodReferenceNode extends ExpressionNode {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -222,4 +222,11 @@ public class ConvertOperatorsUnitTests {
|
||||
assertThat(ConvertOperators.valueOf(EXPRESSION).convertToString().toDocument(Aggregation.DEFAULT_CONTEXT))
|
||||
.isEqualTo(Document.parse("{ $toString: " + EXPRESSION_STRING + " } "));
|
||||
}
|
||||
|
||||
@Test // GH-3714
|
||||
void degreesToRadiansUsingFieldReference() {
|
||||
|
||||
assertThat(ConvertOperators.valueOf("angle_a").convertDegreesToRadians().toDocument(Aggregation.DEFAULT_CONTEXT))
|
||||
.isEqualTo(Document.parse("{ $degreesToRadians : \"$angle_a\"}"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -941,6 +941,11 @@ public class SpelExpressionTransformerUnitTests {
|
||||
assertThat(transform("round(field, 2)")).isEqualTo(Document.parse("{ \"$round\" : [\"$field\", 2]}"));
|
||||
}
|
||||
|
||||
@Test // GH-3714
|
||||
void shouldRenderDegreesToRadians() {
|
||||
assertThat(transform("degreesToRadians(angle_a)")).isEqualTo(Document.parse("{ \"$degreesToRadians\" : \"$angle_a\"}"));
|
||||
}
|
||||
|
||||
@Test // GH-3712
|
||||
void shouldRenderCovariancePop() {
|
||||
assertThat(transform("covariancePop(field1, field2)"))
|
||||
|
||||
Reference in New Issue
Block a user